src/utils/hsdis/hsdis.c
author darcy
Thu, 03 Oct 2019 08:49:09 -0700
changeset 58456 9759972b4f1c
parent 51273 5064f30ac015
permissions -rw-r--r--
8231777: Remove extraneous @serial javadoc tag in NodeChangeEvent.java Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     1
/*
51273
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
     2
 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     4
 *
51202
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     5
 * The Universal Permissive License (UPL), Version 1.0
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     6
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     7
 * Subject to the condition set forth below, permission is hereby granted to
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     8
 * any person obtaining a copy of this software, associated documentation
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     9
 * and/or data (collectively the "Software"), free of charge and under any
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    10
 * and all copyright rights in the Software, and any and all patent rights
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    11
 * owned or freely licensable by each licensor hereunder covering either (i)
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    12
 * the unmodified Software as contributed to or provided by such licensor,
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    13
 * or (ii) the Larger Works (as defined below), to deal in both
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    14
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    15
 * (a) the Software, and
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    16
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    17
 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file
51271
b6e0bfe4a6ec 8208541: non-ASCII characters in hsdis UPL text
dbuck
parents: 51202
diff changeset
    18
 * if one is included with the Software (each a "Larger Work" to which the
51202
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    19
 * Software is contributed by such licensors),
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    20
 *
51202
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    21
 * without restriction, including without limitation the rights to copy,
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    22
 * create derivative works of, display, perform, and distribute the Software
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    23
 * and make, use, sell, offer for sale, import, export, have made, and have
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    24
 * sold the Software and the Larger Work(s), and to sublicense the foregoing
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    25
 * rights on either these or other terms.
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    26
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    27
 * This license is subject to the following condition:
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    28
 *
51202
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    29
 * The above copyright notice and either this complete permission notice or
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    30
 * at a minimum a reference to the UPL must be included in all copies or
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    31
 * substantial portions of the Software.
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    32
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    33
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    34
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    35
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    36
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    37
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    38
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    39
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    40
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    41
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    42
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    43
 * questions.
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    44
 *
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    45
 */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    46
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    47
/* hsdis.c -- dump a range of addresses as native instructions
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    48
   This implements the plugin protocol required by the
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    49
   HotSpot PrintAssembly option.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    50
*/
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    51
17618
882c7fe997eb 8012312: hsdis fails to compile with binutils-2.23.2
anoll
parents: 14384
diff changeset
    52
#include <config.h> /* required by bfd.h */
51273
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
    53
#include <errno.h>
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
    54
#include <inttypes.h>
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
    55
#include <string.h>
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
    56
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    57
#include <libiberty.h>
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    58
#include <bfd.h>
51273
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
    59
#include <bfdver.h>
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    60
#include <dis-asm.h>
51273
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
    61
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
    62
#include "hsdis.h"
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    63
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    64
#ifndef bool
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    65
#define bool int
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    66
#define true 1
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    67
#define false 0
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    68
#endif /*bool*/
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    69
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    70
/* short names for stuff in hsdis.h */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    71
typedef decode_instructions_event_callback_ftype  event_callback_t;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    72
typedef decode_instructions_printf_callback_ftype printf_callback_t;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    73
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    74
/* disassemble_info.application_data object */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    75
struct hsdis_app_data {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
    76
  /* virtual address of data */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
    77
  uintptr_t start_va, end_va;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
    78
  /* the instructions to be decoded */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
    79
  unsigned char* buffer;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
    80
  uintptr_t length;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    81
  event_callback_t  event_callback;  void* event_stream;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    82
  printf_callback_t printf_callback; void* printf_stream;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    83
  bool losing;
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
    84
  bool do_newline;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    85
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    86
  /* the architecture being disassembled */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    87
  const char* arch_name;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    88
  const bfd_arch_info_type* arch_info;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    89
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    90
  /* the disassembler we are going to use: */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    91
  disassembler_ftype      dfn;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    92
  struct disassemble_info dinfo; /* the actual struct! */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    93
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    94
  char mach_option[64];
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    95
  char insn_options[256];
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    96
};
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    97
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
    98
static void* decode(struct hsdis_app_data* app_data, const char* options);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
    99
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   100
#define DECL_APP_DATA(dinfo) \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   101
  struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   102
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   103
#define DECL_EVENT_CALLBACK(app_data) \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   104
  event_callback_t  event_callback = (app_data)->event_callback; \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   105
  void*             event_stream   = (app_data)->event_stream
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   106
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   107
#define DECL_PRINTF_CALLBACK(app_data) \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   108
  printf_callback_t  printf_callback = (app_data)->printf_callback; \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   109
  void*              printf_stream   = (app_data)->printf_stream
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   110
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   111
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   112
static void print_help(struct hsdis_app_data* app_data,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   113
                       const char* msg, const char* arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   114
static void setup_app_data(struct hsdis_app_data* app_data,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   115
                           const char* options);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   116
static const char* format_insn_close(const char* close,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   117
                                     disassemble_info* dinfo,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   118
                                     char* buf, size_t bufsize);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   119
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   120
void*
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   121
#ifdef DLL_ENTRY
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   122
  DLL_ENTRY
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   123
#endif
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   124
decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   125
                            unsigned char* buffer, uintptr_t length,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   126
                            event_callback_t  event_callback_arg,  void* event_stream_arg,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   127
                            printf_callback_t printf_callback_arg, void* printf_stream_arg,
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   128
                            const char* options, int newline) {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   129
  struct hsdis_app_data app_data;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   130
  memset(&app_data, 0, sizeof(app_data));
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   131
  app_data.start_va    = start_va;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   132
  app_data.end_va      = end_va;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   133
  app_data.buffer = buffer;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   134
  app_data.length = length;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   135
  app_data.event_callback  = event_callback_arg;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   136
  app_data.event_stream    = event_stream_arg;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   137
  app_data.printf_callback = printf_callback_arg;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   138
  app_data.printf_stream   = printf_stream_arg;
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   139
  app_data.do_newline = newline == 0 ? false : true;
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   140
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   141
  return decode(&app_data, options);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   142
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   143
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   144
/* This is the compatability interface for older version of hotspot */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   145
void*
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   146
#ifdef DLL_ENTRY
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   147
  DLL_ENTRY
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   148
#endif
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   149
decode_instructions(void* start_pv, void* end_pv,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   150
                    event_callback_t  event_callback_arg,  void* event_stream_arg,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   151
                    printf_callback_t printf_callback_arg, void* printf_stream_arg,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   152
                    const char* options) {
35542
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   153
  return decode_instructions_virtual((uintptr_t)start_pv,
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   154
                                     (uintptr_t)end_pv,
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   155
                                     (unsigned char*)start_pv,
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   156
                                     (uintptr_t)end_pv - (uintptr_t)start_pv,
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   157
                                     event_callback_arg,
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   158
                                     event_stream_arg,
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   159
                                     printf_callback_arg,
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   160
                                     printf_stream_arg,
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   161
                                     options, false);
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   162
}
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   163
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   164
static void* decode(struct hsdis_app_data* app_data, const char* options) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   165
  setup_app_data(app_data, options);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   166
  char buf[128];
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   167
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   168
  {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   169
    /* now reload everything from app_data: */
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   170
    DECL_EVENT_CALLBACK(app_data);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   171
    DECL_PRINTF_CALLBACK(app_data);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   172
    uintptr_t start = app_data->start_va;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   173
    uintptr_t end   = app_data->end_va;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   174
    uintptr_t p     = start;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   175
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   176
    (*event_callback)(event_stream, "insns", (void*)start);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   177
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   178
    (*event_callback)(event_stream, "mach name='%s'",
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   179
                      (void*) app_data->arch_info->printable_name);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   180
    if (app_data->dinfo.bytes_per_line != 0) {
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   181
      (*event_callback)(event_stream, "format bytes-per-line='%p'/",
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   182
                        (void*)(intptr_t) app_data->dinfo.bytes_per_line);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   183
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   184
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   185
    while (p < end && !app_data->losing) {
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   186
      (*event_callback)(event_stream, "insn", (void*) p);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   187
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   188
      /* reset certain state, so we can read it with confidence */
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   189
      app_data->dinfo.insn_info_valid    = 0;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   190
      app_data->dinfo.branch_delay_insns = 0;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   191
      app_data->dinfo.data_size          = 0;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   192
      app_data->dinfo.insn_type          = 0;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   193
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   194
      int size = (*app_data->dfn)((bfd_vma) p, &app_data->dinfo);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   195
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   196
      if (size > 0)  p += size;
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   197
      else           app_data->losing = true;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   198
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   199
      if (!app_data->losing) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   200
        const char* insn_close = format_insn_close("/insn", &app_data->dinfo,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   201
                                                   buf, sizeof(buf));
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   202
        (*event_callback)(event_stream, insn_close, (void*) p);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   203
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   204
        if (app_data->do_newline) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   205
          /* follow each complete insn by a nice newline */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   206
          (*printf_callback)(printf_stream, "\n");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   207
        }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   208
      }
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   209
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   210
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   211
    if (app_data->losing) (*event_callback)(event_stream, "/insns", (void*) p);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   212
    return (void*) p;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   213
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   214
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   215
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   216
/* take the address of the function, for luck, and also test the typedef: */
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   217
const decode_func_vtype decode_func_virtual_address = &decode_instructions_virtual;
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   218
const decode_func_stype decode_func_address = &decode_instructions;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   219
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   220
static const char* format_insn_close(const char* close,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   221
                                     disassemble_info* dinfo,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   222
                                     char* buf, size_t bufsize) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   223
  if (!dinfo->insn_info_valid)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   224
    return close;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   225
  enum dis_insn_type itype = dinfo->insn_type;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   226
  int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   227
  if ((itype == dis_nonbranch && (dsize | delays) == 0)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   228
      || (strlen(close) + 3*20 > bufsize))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   229
    return close;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   230
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   231
  const char* type = "unknown";
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   232
  switch (itype) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   233
  case dis_nonbranch:   type = NULL;         break;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   234
  case dis_branch:      type = "branch";     break;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   235
  case dis_condbranch:  type = "condbranch"; break;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   236
  case dis_jsr:         type = "jsr";        break;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   237
  case dis_condjsr:     type = "condjsr";    break;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   238
  case dis_dref:        type = "dref";       break;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   239
  case dis_dref2:       type = "dref2";      break;
35542
9dccb7f9f656 8071374: -XX:+PrintAssembly -XX:+PrintSignatureHandlers crash fastdebug VM with assert(limit == __null || limit <= nm->code_end()) in RelocIterator::initialize
vlivanov
parents: 35075
diff changeset
   240
  case dis_noninsn:     type = "noninsn";    break;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   241
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   242
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   243
  strcpy(buf, close);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   244
  char* p = buf;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   245
  if (type)    sprintf(p += strlen(p), " type='%s'", type);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   246
  if (dsize)   sprintf(p += strlen(p), " dsize='%d'", dsize);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   247
  if (delays)  sprintf(p += strlen(p), " delay='%d'", delays);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   248
  return buf;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   249
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   250
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   251
/* handler functions */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   252
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   253
static int
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   254
hsdis_read_memory_func(bfd_vma memaddr,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   255
                       bfd_byte* myaddr,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   256
                       unsigned int length,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   257
                       struct disassemble_info* dinfo) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   258
  DECL_APP_DATA(dinfo);
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   259
  /* convert the virtual address memaddr into an address within memory buffer */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   260
  uintptr_t offset = ((uintptr_t) memaddr) - app_data->start_va;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   261
  if (offset + length > app_data->length) {
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   262
    /* read is out of bounds */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   263
    return EIO;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   264
  } else {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   265
    memcpy(myaddr, (bfd_byte*) (app_data->buffer + offset), length);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   266
    return 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   267
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   268
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   269
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   270
static void
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   271
hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   272
  /* the actual value to print: */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   273
  void* addr_value = (void*) (uintptr_t) vma;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   274
  DECL_APP_DATA(dinfo);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   275
  DECL_EVENT_CALLBACK(app_data);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   276
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   277
  /* issue the event: */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   278
  void* result =
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   279
    (*event_callback)(event_stream, "addr/", addr_value);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   280
  if (result == NULL) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   281
    /* event declined */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   282
    generic_print_address(vma, dinfo);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   283
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   284
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   285
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   286
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   287
/* configuration */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   288
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   289
static void set_optional_callbacks(struct hsdis_app_data* app_data);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   290
static void parse_caller_options(struct hsdis_app_data* app_data,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   291
                                 const char* caller_options);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   292
static const char* native_arch_name();
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   293
static enum bfd_endian native_endian();
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   294
static const bfd_arch_info_type* find_arch_info(const char* arch_nane);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   295
static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   296
                           /* to avoid malloc: */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   297
                           bfd* empty_bfd, bfd_target* empty_xvec);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   298
static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   299
                                           void *stream,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   300
                                           fprintf_ftype fprintf_func,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   301
                                           bfd* bfd,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   302
                                           char* disassembler_options);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   303
static void parse_fake_insn(disassembler_ftype dfn,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   304
                            struct disassemble_info* dinfo);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   305
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   306
static void setup_app_data(struct hsdis_app_data* app_data,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   307
                           const char* caller_options) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   308
  /* Make reasonable defaults for null callbacks.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   309
     A non-null stream for a null callback is assumed to be a FILE* for output.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   310
     Events are rendered as XML.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   311
  */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   312
  set_optional_callbacks(app_data);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   313
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   314
  /* Look into caller_options for anything interesting. */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   315
  if (caller_options != NULL)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   316
    parse_caller_options(app_data, caller_options);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   317
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   318
  /* Discover which architecture we are going to disassemble. */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   319
  app_data->arch_name = &app_data->mach_option[0];
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   320
  if (app_data->arch_name[0] == '\0')
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   321
    app_data->arch_name = native_arch_name();
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   322
  app_data->arch_info = find_arch_info(app_data->arch_name);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   323
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   324
  /* Make a fake bfd to hold the arch. and byteorder info. */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   325
  struct {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   326
    bfd_target empty_xvec;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   327
    bfd        empty_bfd;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   328
  } buf;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   329
  bfd* native_bfd = get_native_bfd(app_data->arch_info,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   330
                                   /* to avoid malloc: */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   331
                                   &buf.empty_bfd, &buf.empty_xvec);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   332
  init_disassemble_info_from_bfd(&app_data->dinfo,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   333
                                 app_data->printf_stream,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   334
                                 app_data->printf_callback,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   335
                                 native_bfd,
22822
696e77cc8e7b 8019926: PPC64 (part 106): Make hsdis build and work on Linux/PPC64
simonis
parents: 17618
diff changeset
   336
                                 /* On PowerPC we get warnings, if we pass empty options */
696e77cc8e7b 8019926: PPC64 (part 106): Make hsdis build and work on Linux/PPC64
simonis
parents: 17618
diff changeset
   337
                                 (caller_options == NULL) ? NULL : app_data->insn_options);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   338
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   339
  /* Finish linking together the various callback blocks. */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   340
  app_data->dinfo.application_data = (void*) app_data;
51273
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
   341
  app_data->dfn = disassembler(bfd_get_arch(native_bfd),
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
   342
                               bfd_big_endian(native_bfd),
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
   343
                               bfd_get_mach(native_bfd),
5064f30ac015 8191006: hsdis disassembler plugin does not compile with binutils 2.29+
dbuck
parents: 51271
diff changeset
   344
                               native_bfd);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   345
  app_data->dinfo.print_address_func = hsdis_print_address_func;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   346
  app_data->dinfo.read_memory_func = hsdis_read_memory_func;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   348
  if (app_data->dfn == NULL) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   349
    const char* bad = app_data->arch_name;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   350
    static bool complained;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   351
    if (bad == &app_data->mach_option[0])
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   352
      print_help(app_data, "bad mach=%s", bad);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   353
    else if (!complained)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   354
      print_help(app_data, "bad native mach=%s; please port hsdis to this platform", bad);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   355
    complained = true;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   356
    /* must bail out */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   357
    app_data->losing = true;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   358
    return;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   359
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   360
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   361
  parse_fake_insn(app_data->dfn, &app_data->dinfo);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   362
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   363
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   364
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   365
/* ignore all events, return a null */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   366
static void* null_event_callback(void* ignore_stream, const char* ignore_event, void* arg) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   367
  return NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   368
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   369
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   370
/* print all events as XML markup */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   371
static void* xml_event_callback(void* stream, const char* event, void* arg) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   372
  FILE* fp = (FILE*) stream;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   373
#define NS_PFX "dis:"
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   374
  if (event[0] != '/') {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   375
    /* issue the tag, with or without a formatted argument */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   376
    fprintf(fp, "<"NS_PFX);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   377
    fprintf(fp, event, arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   378
    fprintf(fp, ">");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   379
  } else {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   380
    ++event;                    /* skip slash */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   381
    const char* argp = strchr(event, ' ');
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   382
    if (argp == NULL) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   383
      /* no arguments; just issue the closing tag */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   384
      fprintf(fp, "</"NS_PFX"%s>", event);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   385
    } else {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   386
      /* split out the closing attributes as <dis:foo_done attr='val'/> */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   387
      int event_prefix = (argp - event);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   388
      fprintf(fp, "<"NS_PFX"%.*s_done", event_prefix, event);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   389
      fprintf(fp, argp, arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   390
      fprintf(fp, "/></"NS_PFX"%.*s>", event_prefix, event);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   391
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   392
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   393
  return NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   394
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   395
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   396
static void set_optional_callbacks(struct hsdis_app_data* app_data) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   397
  if (app_data->printf_callback == NULL) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   398
    int (*fprintf_callback)(FILE*, const char*, ...) = &fprintf;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   399
    FILE* fprintf_stream = stdout;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   400
    app_data->printf_callback = (printf_callback_t) fprintf_callback;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   401
    if (app_data->printf_stream == NULL)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   402
      app_data->printf_stream   = (void*)           fprintf_stream;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   403
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   404
  if (app_data->event_callback == NULL) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   405
    if (app_data->event_stream == NULL)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   406
      app_data->event_callback = &null_event_callback;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   407
    else
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   408
      app_data->event_callback = &xml_event_callback;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   409
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   410
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   411
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   412
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   413
static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   414
  char* iop_base = app_data->insn_options;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   415
  char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   416
  char* iop = iop_base;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   417
  const char* p;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   418
  for (p = caller_options; p != NULL; ) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   419
    const char* q = strchr(p, ',');
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   420
    size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   421
    if (plen == 4 && strncmp(p, "help", plen) == 0) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   422
      print_help(app_data, NULL, NULL);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   423
    } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   424
      char*  mach_option = app_data->mach_option;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   425
      size_t mach_size   = sizeof(app_data->mach_option);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   426
      mach_size -= 1;           /*leave room for the null*/
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   427
      if (plen > mach_size)  plen = mach_size;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   428
      strncpy(mach_option, p, plen);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   429
      mach_option[plen] = '\0';
11782
38cbdb77b3d1 7140985: HSDIS does not handle caller options correctly
kvn
parents: 8921
diff changeset
   430
    } else if (plen > 6 && strncmp(p, "hsdis-", 6) == 0) {
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   431
      // do not pass these to the next level
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   432
    } else {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   433
      /* just copy it; {i386,sparc}-dis.c might like to see it  */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   434
      if (iop > iop_base && iop < iop_limit)  (*iop++) = ',';
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   435
      if (iop + plen > iop_limit)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   436
        plen = iop_limit - iop;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   437
      strncpy(iop, p, plen);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   438
      iop += plen;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   439
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   440
    p = q;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   441
  }
30281
b1608535e50f 8076475: Misuses of strncpy/strncat
stuefe
parents: 29191
diff changeset
   442
  *iop = '\0';
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   443
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   444
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   445
static void print_help(struct hsdis_app_data* app_data,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   446
                       const char* msg, const char* arg) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   447
  DECL_PRINTF_CALLBACK(app_data);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   448
  if (msg != NULL) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   449
    (*printf_callback)(printf_stream, "hsdis: ");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   450
    (*printf_callback)(printf_stream, msg, arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   451
    (*printf_callback)(printf_stream, "\n");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   452
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   453
  (*printf_callback)(printf_stream, "hsdis output options:\n");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   454
  if (printf_callback == (printf_callback_t) &fprintf)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   455
    disassembler_usage((FILE*) printf_stream);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   456
  else
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   457
    disassembler_usage(stderr); /* better than nothing */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   458
  (*printf_callback)(printf_stream, "  mach=<arch>   select disassembly mode\n");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   459
#if defined(LIBARCH_i386) || defined(LIBARCH_amd64)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   460
  (*printf_callback)(printf_stream, "  mach=i386     select 32-bit mode\n");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   461
  (*printf_callback)(printf_stream, "  mach=x86-64   select 64-bit mode\n");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   462
  (*printf_callback)(printf_stream, "  suffix        always print instruction suffix\n");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   463
#endif
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   464
  (*printf_callback)(printf_stream, "  help          print this message\n");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   465
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   466
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   467
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   468
/* low-level bfd and arch stuff that binutils doesn't do for us */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   469
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   470
static const bfd_arch_info_type* find_arch_info(const char* arch_name) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   471
  const bfd_arch_info_type* arch_info = bfd_scan_arch(arch_name);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   472
  if (arch_info == NULL) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   473
    extern const bfd_arch_info_type bfd_default_arch_struct;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   474
    arch_info = &bfd_default_arch_struct;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   475
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   476
  return arch_info;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   477
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   478
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   479
static const char* native_arch_name() {
2564
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   480
  const char* res = NULL;
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   481
#ifdef LIBARCH_i386
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   482
  res = "i386";
2564
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   483
#endif
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   484
#ifdef LIBARCH_amd64
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   485
  res = "i386:x86-64";
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   486
#endif
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   487
#ifdef LIBARCH_sparc
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   488
  res = "sparc:v8plusb";
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   489
#endif
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   490
#ifdef LIBARCH_sparcv9
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   491
  res = "sparc:v9b";
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   492
#endif
35075
ca79cbf3f106 8073139: PPC64: User-visible arch directory and os.arch value on ppc64le cause issues with Java tooling
asmundak
parents: 30281
diff changeset
   493
#if  defined(LIBARCH_ppc64) || defined(LIBARCH_ppc64le)
22822
696e77cc8e7b 8019926: PPC64 (part 106): Make hsdis build and work on Linux/PPC64
simonis
parents: 17618
diff changeset
   494
  res = "powerpc:common64";
696e77cc8e7b 8019926: PPC64 (part 106): Make hsdis build and work on Linux/PPC64
simonis
parents: 17618
diff changeset
   495
#endif
46748
4aeae5b90e60 8185567: fix hsdis cpu to architecture mapping on various Linux platforms
dbuck
parents: 42062
diff changeset
   496
#ifdef LIBARCH_arm
4aeae5b90e60 8185567: fix hsdis cpu to architecture mapping on various Linux platforms
dbuck
parents: 42062
diff changeset
   497
  res = "arm";
4aeae5b90e60 8185567: fix hsdis cpu to architecture mapping on various Linux platforms
dbuck
parents: 42062
diff changeset
   498
#endif
29191
07d26bb7e4bc 8072698: Add AArch64 support to hsdis
aph
parents: 22872
diff changeset
   499
#ifdef LIBARCH_aarch64
07d26bb7e4bc 8072698: Add AArch64 support to hsdis
aph
parents: 22872
diff changeset
   500
  res = "aarch64";
07d26bb7e4bc 8072698: Add AArch64 support to hsdis
aph
parents: 22872
diff changeset
   501
#endif
42062
473286891dd8 8166560: [s390] Basic enablement of s390 port.
goetz
parents: 35542
diff changeset
   502
#ifdef LIBARCH_s390x
473286891dd8 8166560: [s390] Basic enablement of s390 port.
goetz
parents: 35542
diff changeset
   503
  res = "s390:64-bit";
473286891dd8 8166560: [s390] Basic enablement of s390 port.
goetz
parents: 35542
diff changeset
   504
#endif
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   505
  if (res == NULL)
2564
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   506
    res = "architecture not set in Makefile!";
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   507
  return res;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   508
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   509
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   510
static enum bfd_endian native_endian() {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   511
  int32_t endian_test = 'x';
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   512
  if (*(const char*) &endian_test == 'x')
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   513
    return BFD_ENDIAN_LITTLE;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   514
  else
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   515
    return BFD_ENDIAN_BIG;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   516
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   517
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   518
static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   519
                           bfd* empty_bfd, bfd_target* empty_xvec) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   520
  memset(empty_bfd,  0, sizeof(*empty_bfd));
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   521
  memset(empty_xvec, 0, sizeof(*empty_xvec));
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   522
  empty_xvec->flavour = bfd_target_unknown_flavour;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   523
  empty_xvec->byteorder = native_endian();
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   524
  empty_bfd->xvec = empty_xvec;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   525
  empty_bfd->arch_info = arch_info;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   526
  return empty_bfd;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   527
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   528
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   529
static int read_zero_data_only(bfd_vma ignore_p,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   530
                               bfd_byte* myaddr, unsigned int length,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   531
                               struct disassemble_info *ignore_info) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   532
  memset(myaddr, 0, length);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   533
  return 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   534
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   535
static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   536
  return 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   537
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   538
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   539
/* Prime the pump by running the selected disassembler on a null input.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   540
   This forces the machine-specific disassembler to divulge invariant
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   541
   information like bytes_per_line.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   542
 */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   543
static void parse_fake_insn(disassembler_ftype dfn,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   544
                            struct disassemble_info* dinfo) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   545
  typedef int (*read_memory_ftype)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   546
    (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   547
     struct disassemble_info *info);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   548
  read_memory_ftype read_memory_func = dinfo->read_memory_func;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   549
  fprintf_ftype     fprintf_func     = dinfo->fprintf_func;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   550
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   551
  dinfo->read_memory_func = &read_zero_data_only;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   552
  dinfo->fprintf_func     = &print_to_dev_null;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   553
  (*dfn)(0, dinfo);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   554
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 11782
diff changeset
   555
  /* put it back */
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   556
  dinfo->read_memory_func = read_memory_func;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   557
  dinfo->fprintf_func     = fprintf_func;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   558
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   559
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   560
static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   561
                                           void *stream,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   562
                                           fprintf_ftype fprintf_func,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   563
                                           bfd* abfd,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   564
                                           char* disassembler_options) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   565
  init_disassemble_info(dinfo, stream, fprintf_func);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   566
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   567
  dinfo->flavour = bfd_get_flavour(abfd);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   568
  dinfo->arch = bfd_get_arch(abfd);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   569
  dinfo->mach = bfd_get_mach(abfd);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   570
  dinfo->disassembler_options = disassembler_options;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   571
  dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   572
  dinfo->skip_zeroes = sizeof(void*) * 2;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   573
  dinfo->skip_zeroes_at_end = sizeof(void*)-1;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   574
  dinfo->disassembler_needs_relocs = FALSE;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   575
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   576
  if (bfd_big_endian(abfd))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   577
    dinfo->display_endian = dinfo->endian = BFD_ENDIAN_BIG;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   578
  else if (bfd_little_endian(abfd))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   579
    dinfo->display_endian = dinfo->endian = BFD_ENDIAN_LITTLE;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   580
  else
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   581
    dinfo->endian = native_endian();
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   582
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   583
  disassemble_init_for_target(dinfo);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   584
}