100N/A/*
2273N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
100N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
100N/A *
100N/A * This code is free software; you can redistribute it and/or modify it
100N/A * under the terms of the GNU General Public License version 2 only, as
100N/A * published by the Free Software Foundation.
100N/A *
100N/A * This code is distributed in the hope that it will be useful, but WITHOUT
100N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
100N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
100N/A * version 2 for more details (a copy is included in the LICENSE file that
100N/A * accompanied this code).
100N/A *
100N/A * You should have received a copy of the GNU General Public License version
100N/A * 2 along with this work; if not, write to the Free Software Foundation,
100N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
100N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
100N/A *
100N/A */
100N/A
100N/A/* hsdis.c -- dump a range of addresses as native instructions
100N/A This implements the plugin protocol required by the
100N/A HotSpot PrintAssembly option.
100N/A*/
100N/A
100N/A#include "hsdis.h"
100N/A
100N/A#include <sysdep.h>
100N/A#include <libiberty.h>
100N/A#include <bfd.h>
100N/A#include <dis-asm.h>
720N/A#include <inttypes.h>
100N/A
100N/A#ifndef bool
100N/A#define bool int
100N/A#define true 1
100N/A#define false 0
100N/A#endif /*bool*/
100N/A
100N/A/* short names for stuff in hsdis.h */
100N/Atypedef decode_instructions_event_callback_ftype event_callback_t;
100N/Atypedef decode_instructions_printf_callback_ftype printf_callback_t;
100N/A
100N/A/* disassemble_info.application_data object */
100N/Astruct hsdis_app_data {
100N/A /* the arguments to decode_instructions */
100N/A uintptr_t start; uintptr_t end;
100N/A event_callback_t event_callback; void* event_stream;
100N/A printf_callback_t printf_callback; void* printf_stream;
100N/A bool losing;
100N/A
100N/A /* the architecture being disassembled */
100N/A const char* arch_name;
100N/A const bfd_arch_info_type* arch_info;
100N/A
100N/A /* the disassembler we are going to use: */
100N/A disassembler_ftype dfn;
100N/A struct disassemble_info dinfo; /* the actual struct! */
100N/A
100N/A char mach_option[64];
100N/A char insn_options[256];
100N/A};
100N/A
100N/A#define DECL_APP_DATA(dinfo) \
100N/A struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data
100N/A
100N/A#define DECL_EVENT_CALLBACK(app_data) \
100N/A event_callback_t event_callback = (app_data)->event_callback; \
100N/A void* event_stream = (app_data)->event_stream
100N/A
100N/A#define DECL_PRINTF_CALLBACK(app_data) \
100N/A printf_callback_t printf_callback = (app_data)->printf_callback; \
100N/A void* printf_stream = (app_data)->printf_stream
100N/A
100N/A
100N/Astatic void print_help(struct hsdis_app_data* app_data,
100N/A const char* msg, const char* arg);
100N/Astatic void setup_app_data(struct hsdis_app_data* app_data,
100N/A const char* options);
100N/Astatic const char* format_insn_close(const char* close,
100N/A disassemble_info* dinfo,
100N/A char* buf, size_t bufsize);
100N/A
100N/Avoid*
100N/A#ifdef DLL_ENTRY
100N/A DLL_ENTRY
100N/A#endif
100N/Adecode_instructions(void* start_pv, void* end_pv,
100N/A event_callback_t event_callback_arg, void* event_stream_arg,
100N/A printf_callback_t printf_callback_arg, void* printf_stream_arg,
100N/A const char* options) {
100N/A struct hsdis_app_data app_data;
100N/A memset(&app_data, 0, sizeof(app_data));
100N/A app_data.start = (uintptr_t) start_pv;
100N/A app_data.end = (uintptr_t) end_pv;
100N/A app_data.event_callback = event_callback_arg;
100N/A app_data.event_stream = event_stream_arg;
100N/A app_data.printf_callback = printf_callback_arg;
100N/A app_data.printf_stream = printf_stream_arg;
100N/A
100N/A setup_app_data(&app_data, options);
100N/A char buf[128];
100N/A
100N/A {
100N/A /* now reload everything from app_data: */
100N/A DECL_EVENT_CALLBACK(&app_data);
100N/A DECL_PRINTF_CALLBACK(&app_data);
100N/A uintptr_t start = app_data.start;
100N/A uintptr_t end = app_data.end;
100N/A uintptr_t p = start;
100N/A
100N/A (*event_callback)(event_stream, "insns", (void*)start);
100N/A
100N/A (*event_callback)(event_stream, "mach name='%s'",
100N/A (void*) app_data.arch_info->printable_name);
100N/A if (app_data.dinfo.bytes_per_line != 0) {
100N/A (*event_callback)(event_stream, "format bytes-per-line='%p'/",
100N/A (void*)(intptr_t) app_data.dinfo.bytes_per_line);
100N/A }
100N/A
100N/A while (p < end && !app_data.losing) {
100N/A (*event_callback)(event_stream, "insn", (void*) p);
100N/A
100N/A /* reset certain state, so we can read it with confidence */
100N/A app_data.dinfo.insn_info_valid = 0;
100N/A app_data.dinfo.branch_delay_insns = 0;
100N/A app_data.dinfo.data_size = 0;
100N/A app_data.dinfo.insn_type = 0;
100N/A
100N/A int size = (*app_data.dfn)((bfd_vma) p, &app_data.dinfo);
100N/A
100N/A if (size > 0) p += size;
100N/A else app_data.losing = true;
100N/A
100N/A const char* insn_close = format_insn_close("/insn", &app_data.dinfo,
100N/A buf, sizeof(buf));
100N/A (*event_callback)(event_stream, insn_close, (void*) p);
100N/A
100N/A /* follow each complete insn by a nice newline */
100N/A (*printf_callback)(printf_stream, "\n");
100N/A }
100N/A
100N/A (*event_callback)(event_stream, "/insns", (void*) p);
100N/A return (void*) p;
100N/A }
100N/A}
100N/A
100N/A/* take the address of the function, for luck, and also test the typedef: */
100N/Aconst decode_instructions_ftype decode_instructions_address = &decode_instructions;
100N/A
100N/Astatic const char* format_insn_close(const char* close,
100N/A disassemble_info* dinfo,
100N/A char* buf, size_t bufsize) {
100N/A if (!dinfo->insn_info_valid)
100N/A return close;
100N/A enum dis_insn_type itype = dinfo->insn_type;
100N/A int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
100N/A if ((itype == dis_nonbranch && (dsize | delays) == 0)
100N/A || (strlen(close) + 3*20 > bufsize))
100N/A return close;
100N/A
100N/A const char* type = "unknown";
100N/A switch (itype) {
100N/A case dis_nonbranch: type = NULL; break;
100N/A case dis_branch: type = "branch"; break;
100N/A case dis_condbranch: type = "condbranch"; break;
100N/A case dis_jsr: type = "jsr"; break;
100N/A case dis_condjsr: type = "condjsr"; break;
100N/A case dis_dref: type = "dref"; break;
100N/A case dis_dref2: type = "dref2"; break;
100N/A }
100N/A
100N/A strcpy(buf, close);
100N/A char* p = buf;
100N/A if (type) sprintf(p += strlen(p), " type='%s'", type);
100N/A if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize);
100N/A if (delays) sprintf(p += strlen(p), " delay='%d'", delays);
100N/A return buf;
100N/A}
100N/A
100N/A/* handler functions */
100N/A
100N/Astatic int
100N/Ahsdis_read_memory_func(bfd_vma memaddr,
100N/A bfd_byte* myaddr,
100N/A unsigned int length,
100N/A struct disassemble_info* dinfo) {
100N/A uintptr_t memaddr_p = (uintptr_t) memaddr;
100N/A DECL_APP_DATA(dinfo);
100N/A if (memaddr_p + length > app_data->end) {
100N/A /* read is out of bounds */
100N/A return EIO;
100N/A } else {
100N/A memcpy(myaddr, (bfd_byte*) memaddr_p, length);
100N/A return 0;
100N/A }
100N/A}
100N/A
100N/Astatic void
100N/Ahsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
100N/A /* the actual value to print: */
100N/A void* addr_value = (void*) (uintptr_t) vma;
100N/A DECL_APP_DATA(dinfo);
100N/A DECL_EVENT_CALLBACK(app_data);
100N/A
100N/A /* issue the event: */
100N/A void* result =
100N/A (*event_callback)(event_stream, "addr/", addr_value);
100N/A if (result == NULL) {
100N/A /* event declined */
100N/A generic_print_address(vma, dinfo);
100N/A }
100N/A}
100N/A
100N/A
100N/A/* configuration */
100N/A
100N/Astatic void set_optional_callbacks(struct hsdis_app_data* app_data);
100N/Astatic void parse_caller_options(struct hsdis_app_data* app_data,
100N/A const char* caller_options);
100N/Astatic const char* native_arch_name();
100N/Astatic enum bfd_endian native_endian();
100N/Astatic const bfd_arch_info_type* find_arch_info(const char* arch_nane);
100N/Astatic bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
100N/A /* to avoid malloc: */
100N/A bfd* empty_bfd, bfd_target* empty_xvec);
100N/Astatic void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
100N/A void *stream,
100N/A fprintf_ftype fprintf_func,
100N/A bfd* bfd,
100N/A char* disassembler_options);
100N/Astatic void parse_fake_insn(disassembler_ftype dfn,
100N/A struct disassemble_info* dinfo);
100N/A
100N/Astatic void setup_app_data(struct hsdis_app_data* app_data,
100N/A const char* caller_options) {
100N/A /* Make reasonable defaults for null callbacks.
100N/A A non-null stream for a null callback is assumed to be a FILE* for output.
100N/A Events are rendered as XML.
100N/A */
100N/A set_optional_callbacks(app_data);
100N/A
100N/A /* Look into caller_options for anything interesting. */
100N/A if (caller_options != NULL)
100N/A parse_caller_options(app_data, caller_options);
100N/A
100N/A /* Discover which architecture we are going to disassemble. */
100N/A app_data->arch_name = &app_data->mach_option[0];
100N/A if (app_data->arch_name[0] == '\0')
100N/A app_data->arch_name = native_arch_name();
100N/A app_data->arch_info = find_arch_info(app_data->arch_name);
100N/A
100N/A /* Make a fake bfd to hold the arch. and byteorder info. */
100N/A struct {
100N/A bfd_target empty_xvec;
100N/A bfd empty_bfd;
100N/A } buf;
100N/A bfd* native_bfd = get_native_bfd(app_data->arch_info,
100N/A /* to avoid malloc: */
100N/A &buf.empty_bfd, &buf.empty_xvec);
100N/A init_disassemble_info_from_bfd(&app_data->dinfo,
100N/A app_data->printf_stream,
100N/A app_data->printf_callback,
100N/A native_bfd,
100N/A app_data->insn_options);
100N/A
100N/A /* Finish linking together the various callback blocks. */
100N/A app_data->dinfo.application_data = (void*) app_data;
100N/A app_data->dfn = disassembler(native_bfd);
100N/A app_data->dinfo.print_address_func = hsdis_print_address_func;
100N/A app_data->dinfo.read_memory_func = hsdis_read_memory_func;
100N/A
100N/A if (app_data->dfn == NULL) {
100N/A const char* bad = app_data->arch_name;
100N/A static bool complained;
100N/A if (bad == &app_data->mach_option[0])
100N/A print_help(app_data, "bad mach=%s", bad);
100N/A else if (!complained)
100N/A print_help(app_data, "bad native mach=%s; please port hsdis to this platform", bad);
100N/A complained = true;
100N/A /* must bail out */
100N/A app_data->losing = true;
100N/A return;
100N/A }
100N/A
100N/A parse_fake_insn(app_data->dfn, &app_data->dinfo);
100N/A}
100N/A
100N/A
100N/A/* ignore all events, return a null */
100N/Astatic void* null_event_callback(void* ignore_stream, const char* ignore_event, void* arg) {
100N/A return NULL;
100N/A}
100N/A
100N/A/* print all events as XML markup */
100N/Astatic void* xml_event_callback(void* stream, const char* event, void* arg) {
100N/A FILE* fp = (FILE*) stream;
100N/A#define NS_PFX "dis:"
100N/A if (event[0] != '/') {
100N/A /* issue the tag, with or without a formatted argument */
100N/A fprintf(fp, "<"NS_PFX);
100N/A fprintf(fp, event, arg);
100N/A fprintf(fp, ">");
100N/A } else {
100N/A ++event; /* skip slash */
100N/A const char* argp = strchr(event, ' ');
100N/A if (argp == NULL) {
100N/A /* no arguments; just issue the closing tag */
100N/A fprintf(fp, "</"NS_PFX"%s>", event);
100N/A } else {
100N/A /* split out the closing attributes as <dis:foo_done attr='val'/> */
100N/A int event_prefix = (argp - event);
100N/A fprintf(fp, "<"NS_PFX"%.*s_done", event_prefix, event);
100N/A fprintf(fp, argp, arg);
100N/A fprintf(fp, "/></"NS_PFX"%.*s>", event_prefix, event);
100N/A }
100N/A }
100N/A return NULL;
100N/A}
100N/A
100N/Astatic void set_optional_callbacks(struct hsdis_app_data* app_data) {
100N/A if (app_data->printf_callback == NULL) {
100N/A int (*fprintf_callback)(FILE*, const char*, ...) = &fprintf;
100N/A FILE* fprintf_stream = stdout;
100N/A app_data->printf_callback = (printf_callback_t) fprintf_callback;
100N/A if (app_data->printf_stream == NULL)
100N/A app_data->printf_stream = (void*) fprintf_stream;
100N/A }
100N/A if (app_data->event_callback == NULL) {
100N/A if (app_data->event_stream == NULL)
100N/A app_data->event_callback = &null_event_callback;
100N/A else
100N/A app_data->event_callback = &xml_event_callback;
100N/A }
100N/A
100N/A}
100N/A
100N/Astatic void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
100N/A char* iop_base = app_data->insn_options;
100N/A char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
100N/A char* iop = iop_base;
100N/A const char* p;
100N/A for (p = caller_options; p != NULL; ) {
100N/A const char* q = strchr(p, ',');
100N/A size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
100N/A if (plen == 4 && strncmp(p, "help", plen) == 0) {
100N/A print_help(app_data, NULL, NULL);
100N/A } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
100N/A char* mach_option = app_data->mach_option;
100N/A size_t mach_size = sizeof(app_data->mach_option);
100N/A mach_size -= 1; /*leave room for the null*/
100N/A if (plen > mach_size) plen = mach_size;
100N/A strncpy(mach_option, p, plen);
100N/A mach_option[plen] = '\0';
3227N/A } else if (plen > 6 && strncmp(p, "hsdis-", 6) == 0) {
100N/A // do not pass these to the next level
100N/A } else {
100N/A /* just copy it; {i386,sparc}-dis.c might like to see it */
100N/A if (iop > iop_base && iop < iop_limit) (*iop++) = ',';
100N/A if (iop + plen > iop_limit)
100N/A plen = iop_limit - iop;
100N/A strncpy(iop, p, plen);
100N/A iop += plen;
100N/A }
100N/A p = q;
100N/A }
100N/A}
100N/A
100N/Astatic void print_help(struct hsdis_app_data* app_data,
100N/A const char* msg, const char* arg) {
100N/A DECL_PRINTF_CALLBACK(app_data);
100N/A if (msg != NULL) {
100N/A (*printf_callback)(printf_stream, "hsdis: ");
100N/A (*printf_callback)(printf_stream, msg, arg);
100N/A (*printf_callback)(printf_stream, "\n");
100N/A }
100N/A (*printf_callback)(printf_stream, "hsdis output options:\n");
100N/A if (printf_callback == (printf_callback_t) &fprintf)
100N/A disassembler_usage((FILE*) printf_stream);
100N/A else
100N/A disassembler_usage(stderr); /* better than nothing */
100N/A (*printf_callback)(printf_stream, " mach=<arch> select disassembly mode\n");
100N/A#if defined(LIBARCH_i386) || defined(LIBARCH_amd64)
100N/A (*printf_callback)(printf_stream, " mach=i386 select 32-bit mode\n");
100N/A (*printf_callback)(printf_stream, " mach=x86-64 select 64-bit mode\n");
100N/A (*printf_callback)(printf_stream, " suffix always print instruction suffix\n");
100N/A#endif
100N/A (*printf_callback)(printf_stream, " help print this message\n");
100N/A}
100N/A
100N/A
100N/A/* low-level bfd and arch stuff that binutils doesn't do for us */
100N/A
100N/Astatic const bfd_arch_info_type* find_arch_info(const char* arch_name) {
100N/A const bfd_arch_info_type* arch_info = bfd_scan_arch(arch_name);
100N/A if (arch_info == NULL) {
100N/A extern const bfd_arch_info_type bfd_default_arch_struct;
100N/A arch_info = &bfd_default_arch_struct;
100N/A }
100N/A return arch_info;
100N/A}
100N/A
100N/Astatic const char* native_arch_name() {
720N/A const char* res = NULL;
720N/A#ifdef LIBARCH_i386
720N/A res = "i386";
720N/A#endif
100N/A#ifdef LIBARCH_amd64
100N/A res = "i386:x86-64";
100N/A#endif
100N/A#ifdef LIBARCH_sparc
100N/A res = "sparc:v8plusb";
100N/A#endif
100N/A#ifdef LIBARCH_sparcv9
100N/A res = "sparc:v9b";
100N/A#endif
100N/A if (res == NULL)
720N/A res = "architecture not set in Makefile!";
100N/A return res;
100N/A}
100N/A
100N/Astatic enum bfd_endian native_endian() {
100N/A int32_t endian_test = 'x';
100N/A if (*(const char*) &endian_test == 'x')
100N/A return BFD_ENDIAN_LITTLE;
100N/A else
100N/A return BFD_ENDIAN_BIG;
100N/A}
100N/A
100N/Astatic bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
100N/A bfd* empty_bfd, bfd_target* empty_xvec) {
100N/A memset(empty_bfd, 0, sizeof(*empty_bfd));
100N/A memset(empty_xvec, 0, sizeof(*empty_xvec));
100N/A empty_xvec->flavour = bfd_target_unknown_flavour;
100N/A empty_xvec->byteorder = native_endian();
100N/A empty_bfd->xvec = empty_xvec;
100N/A empty_bfd->arch_info = arch_info;
100N/A return empty_bfd;
100N/A}
100N/A
100N/Astatic int read_zero_data_only(bfd_vma ignore_p,
100N/A bfd_byte* myaddr, unsigned int length,
100N/A struct disassemble_info *ignore_info) {
100N/A memset(myaddr, 0, length);
100N/A return 0;
100N/A}
100N/Astatic int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
100N/A return 0;
100N/A}
100N/A
100N/A/* Prime the pump by running the selected disassembler on a null input.
100N/A This forces the machine-specific disassembler to divulge invariant
100N/A information like bytes_per_line.
100N/A */
100N/Astatic void parse_fake_insn(disassembler_ftype dfn,
100N/A struct disassemble_info* dinfo) {
100N/A typedef int (*read_memory_ftype)
100N/A (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
100N/A struct disassemble_info *info);
100N/A read_memory_ftype read_memory_func = dinfo->read_memory_func;
100N/A fprintf_ftype fprintf_func = dinfo->fprintf_func;
100N/A
100N/A dinfo->read_memory_func = &read_zero_data_only;
100N/A dinfo->fprintf_func = &print_to_dev_null;
100N/A (*dfn)(0, dinfo);
100N/A
100N/A // put it back:
100N/A dinfo->read_memory_func = read_memory_func;
100N/A dinfo->fprintf_func = fprintf_func;
100N/A}
100N/A
100N/Astatic void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
100N/A void *stream,
100N/A fprintf_ftype fprintf_func,
100N/A bfd* abfd,
100N/A char* disassembler_options) {
100N/A init_disassemble_info(dinfo, stream, fprintf_func);
100N/A
100N/A dinfo->flavour = bfd_get_flavour(abfd);
100N/A dinfo->arch = bfd_get_arch(abfd);
100N/A dinfo->mach = bfd_get_mach(abfd);
100N/A dinfo->disassembler_options = disassembler_options;
100N/A dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
100N/A dinfo->skip_zeroes = sizeof(void*) * 2;
100N/A dinfo->skip_zeroes_at_end = sizeof(void*)-1;
100N/A dinfo->disassembler_needs_relocs = FALSE;
100N/A
100N/A if (bfd_big_endian(abfd))
100N/A dinfo->display_endian = dinfo->endian = BFD_ENDIAN_BIG;
100N/A else if (bfd_little_endian(abfd))
100N/A dinfo->display_endian = dinfo->endian = BFD_ENDIAN_LITTLE;
100N/A else
100N/A dinfo->endian = native_endian();
100N/A
100N/A disassemble_init_for_target(dinfo);
100N/A}