Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions make/Hsdis.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ HSDIS_OUTPUT_DIR := $(SUPPORT_OUTPUTDIR)/hsdis

ifeq ($(call isTargetOs, windows), true)
INSTALLED_HSDIS_DIR := $(JDK_OUTPUTDIR)/bin
IMAGE_HSDIS_DIR := $(JDK_IMAGE_DIR)/bin

# On windows, we need to "fake" a completely different toolchain using gcc
# instead of the normal microsoft toolchain. This is quite hacky...
Expand All @@ -55,6 +56,7 @@ ifeq ($(call isTargetOs, windows), true)

$(eval $(call DefineNativeToolchain, TOOLCHAIN_MINGW, \
CC := $(MINGW_BASE)-gcc, \
CXX := $(MINGW_BASE)-g++, \
LD := $(MINGW_BASE)-ld, \
OBJCOPY := $(MINGW_BASE)-objcopy, \
RC := $(RC), \
Expand Down Expand Up @@ -101,24 +103,46 @@ ifeq ($(call isTargetOs, windows), true)
-lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32
else
INSTALLED_HSDIS_DIR := $(JDK_OUTPUTDIR)/lib
IMAGE_HSDIS_DIR := $(JDK_IMAGE_DIR)/lib

HSDIS_TOOLCHAIN := TOOLCHAIN_DEFAULT
HSDIS_TOOLCHAIN_CFLAGS := $(CFLAGS_JDKLIB)
HSDIS_TOOLCHAIN_LDFLAGS := $(LDFLAGS_JDKLIB)
HSDIS_TOOLCHAIN_LIBS := -ldl

ifeq ($(HSDIS_BACKEND), llvm)
# Use C++ instead of C
HSDIS_TOOLCHAIN_CFLAGS := $(CXXFLAGS_JDKLIB)
HSDIS_TOOLCHAIN := TOOLCHAIN_LINK_CXX
endif
endif

ifeq ($(HSDIS_BACKEND), llvm)
ifeq ($(call isTargetOs, linux), true)
LLVM_OS := pc-linux-gnu
else ifeq ($(call isTargetOs, macosx), true)
LLVM_OS := apple-darwin
else ifeq ($(call isTargetOs, windows), true)
LLVM_OS := pc-windows-msvc
else
$(error No support for LLVM on this platform)
endif

HSDIS_CFLAGS += -DLLVM_DEFAULT_TRIPLET='"$(OPENJDK_TARGET_CPU)-$(LLVM_OS)"'
endif


$(eval $(call SetupJdkLibrary, BUILD_HSDIS, \
NAME := hsdis, \
SRC := $(TOPDIR)/src/utils/hsdis, \
SRC := $(TOPDIR)/src/utils/hsdis/$(HSDIS_BACKEND), \
EXTRA_HEADER_DIRS := $(TOPDIR)/src/utils/hsdis, \
TOOLCHAIN := $(HSDIS_TOOLCHAIN), \
OUTPUT_DIR := $(HSDIS_OUTPUT_DIR), \
OBJECT_DIR := $(HSDIS_OUTPUT_DIR), \
DISABLED_WARNINGS_gcc := undef format-nonliteral sign-compare, \
DISABLED_WARNINGS_clang := undef format-nonliteral, \
CFLAGS := $(HSDIS_TOOLCHAIN_CFLAGS) $(HSDIS_CFLAGS), \
LDFLAGS := $(HSDIS_TOOLCHAIN_LDFLAGS) $(SHARED_LIBRARY_FLAGS), \
LDFLAGS := $(HSDIS_TOOLCHAIN_LDFLAGS) $(HSDIS_LDFLAGS) $(SHARED_LIBRARY_FLAGS), \
LIBS := $(HSDIS_LIBS) $(HSDIS_TOOLCHAIN_LIBS), \
))

Expand All @@ -129,13 +153,19 @@ TARGETS += build
INSTALLED_HSDIS_NAME := hsdis-$(OPENJDK_TARGET_CPU_LEGACY_LIB)$(SHARED_LIBRARY_SUFFIX)

INSTALLED_HSDIS := $(INSTALLED_HSDIS_DIR)/$(INSTALLED_HSDIS_NAME)
INSTALLED_HSDIS_IMAGE := $(IMAGE_HSDIS_DIR)/$(INSTALLED_HSDIS_NAME)

$(INSTALLED_HSDIS): $(BUILD_HSDIS_TARGET)
$(call LogWarn, NOTE: The resulting build might not be redistributable. Seek legal advice before distibuting.)
ifeq ($(HSDIS_BACKEND), binutils)
$(call LogWarn, NOTE: The resulting build might not be redistributable. Seek legal advice before distributing.)
endif
$(install-file)

$(INSTALLED_HSDIS_IMAGE): $(INSTALLED_HSDIS)
$(install-file)


install: $(INSTALLED_HSDIS)
install: $(INSTALLED_HSDIS_IMAGE)

TARGETS += install

Expand Down
34 changes: 33 additions & 1 deletion make/autoconf/jdk-options.m4
Original file line number Diff line number Diff line change
Expand Up @@ -811,14 +811,17 @@ AC_DEFUN([JDKOPT_BUILD_BINUTILS],
AC_DEFUN_ONCE([JDKOPT_SETUP_HSDIS],
[
AC_ARG_WITH([hsdis], [AS_HELP_STRING([--with-hsdis],
[what hsdis backend to use ('none', 'binutils') @<:@none@:>@])])
[what hsdis backend to use ('none', 'binutils', 'llvm') @<:@none@:>@])])

AC_ARG_WITH([binutils], [AS_HELP_STRING([--with-binutils],
[where to find the binutils files needed for hsdis/binutils])])

AC_ARG_WITH([binutils-src], [AS_HELP_STRING([--with-binutils-src],
[where to find the binutils source for building])])

AC_ARG_WITH([llvm], [AS_HELP_STRING([--with-llvm],
[where to find LLVM, needed for hsdis/llvm])])

AC_MSG_CHECKING([what hsdis backend to use])

if test "x$with_hsdis" = xyes; then
Expand Down Expand Up @@ -853,6 +856,7 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_HSDIS],
test -e $BINUTILS_DIR/opcodes/libopcodes.a && \
test -e $BINUTILS_DIR/libiberty/libiberty.a; then
HSDIS_CFLAGS="-I$BINUTILS_DIR/include -I$BINUTILS_DIR/bfd -DLIBARCH_$OPENJDK_TARGET_CPU_LEGACY_LIB"
HSDIS_LDFLAGS=""
HSDIS_LIBS="$BINUTILS_DIR/bfd/libbfd.a $BINUTILS_DIR/opcodes/libopcodes.a $BINUTILS_DIR/libiberty/libiberty.a $BINUTILS_DIR/zlib/libz.a"
fi
fi
Expand Down Expand Up @@ -888,12 +892,40 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_HSDIS],
fi
;;
esac
elif test "x$with_hsdis" = xllvm; then
HSDIS_BACKEND=llvm
AC_MSG_RESULT(['llvm'])

if test "x$with_llvm" != x; then
LLVM_DIR="$with_llvm"
fi

if test "x$LLVM_DIR" != x; then
UTIL_LOOKUP_PROGS(LLVM_CONFIG, llvm-config, [$LLVM_DIR/bin])
if test "x$LLVM_CONFIG" = x; then
AC_MSG_NOTICE([Cannot locate llvm-config in $LLVM_DIR. Check your --with-llvm argument.])
AC_MSG_ERROR([Cannot continue])
fi
else
# Macs with homebrew can have llvm in different places
UTIL_LOOKUP_PROGS(LLVM_CONFIG, llvm-config, [$PATH:/usr/local/opt/llvm/bin:/opt/homebrew/opt/llvm/bin])
if test "x$LLVM_CONFIG" = x; then
AC_MSG_NOTICE([Cannot locate llvm-config which is needed for hsdis/llvm. Try using --with-llvm=<LLVM home>.])
AC_MSG_ERROR([Cannot continue])
fi
fi

# We need the LLVM flags and libs, and llvm-config provides them for us.
HSDIS_CFLAGS=`$LLVM_CONFIG --cflags`
HSDIS_LDFLAGS=`$LLVM_CONFIG --ldflags`
HSDIS_LIBS=`$LLVM_CONFIG --libs $OPENJDK_TARGET_CPU_ARCH ${OPENJDK_TARGET_CPU_ARCH}disassembler`
else
AC_MSG_RESULT([invalid])
AC_MSG_ERROR([Incorrect hsdis backend "$with_hsdis"])
fi

AC_SUBST(HSDIS_BACKEND)
AC_SUBST(HSDIS_CFLAGS)
AC_SUBST(HSDIS_LDFLAGS)
AC_SUBST(HSDIS_LIBS)
])
1 change: 1 addition & 0 deletions make/autoconf/spec.gmk.in
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ ALLOW_ABSOLUTE_PATHS_IN_OUTPUT := @ALLOW_ABSOLUTE_PATHS_IN_OUTPUT@

HSDIS_BACKEND := @HSDIS_BACKEND@
HSDIS_CFLAGS := @HSDIS_CFLAGS@
HSDIS_LDFLAGS := @HSDIS_LDFLAGS@
HSDIS_LIBS := @HSDIS_LIBS@

# The boot jdk to use. This is overridden in bootcycle-spec.gmk. Make sure to keep
Expand Down
70 changes: 47 additions & 23 deletions src/utils/hsdis/README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.

The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -47,11 +47,41 @@ ________________________________________________________________________

The files in this directory are built independently of the HotSpot JVM.

* Building
hsdis is an interface exposed by Hotspot. There are several backends that
implement this interface, using different disassembly engines. Included in the
JDK is support for building hsdis with LLVM or GNU binutils.The interface is
fairly straightforward and easy to implement using other backends.

To build this project you need a copy of GNU binutils to build against. It is
known to work with binutils 2.37. Building against versions older than 2.29 is
not supported. Download a copy of the software from
* Building and installing

To compile hsdis, you need to activate hsdis support, and select the proper
backend to use. This is done with the configure switch --with-hsdis=<backend>,
where <backend> is either "llvm" or "binutils". For details, see the sections on
the respective backends below.

To build the hsdis library, run "make build-hsdis". This will build the library
in a separate directory, but not make it available to the JDK in the
configuration. To actually install it in the JDK, run "make install-hsdis".

NOTE: If you do this using the binutils backend, he resulting build may not be
distributable. Please get legal advice if you intend to distribute the result of
your build.

* Using the library

The hsdis library will be automatically loaded by Hotspot when you use the
diagnostic option "-XX:+PrintAssembly". Note that since this is a diagnostic
option, you need to unlock these first, so in practice you activate it using
"-XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly".

More information is available at the wiki
[https://wiki.openjdk.java.net/display/HotSpot/PrintAssembly].

* Building with binutils

To build this project using binutils you need a copy of GNU binutils to build
against. It is known to work with binutils 2.37. Building against versions older
than 2.29 is not supported. Download a copy of the software from
http://directory.fsf.org/project/binutils or one of its mirrors.

To build this library, you must enable building in configure by "bash configure
Expand All @@ -71,32 +101,26 @@ If you want to build hsdis with binutils provided by system
(e.g. binutils-devel from Fedora, binutils-dev from Ubuntu), you can pass
"--with-binutils=system". "system" is available on Linux only.

When you have created a proper configuration, you can then build the hsdis
library using "make build-hsdis".

* Building on Windows
* Building with binutils on Windows

On Windows, the normal Microsoft Visual Studio toolchain cannot build binutils.
Instead we need to use the mingw compiler. This is available as a cygwin
package. You need to install the "gcc-core" and "mingw64-x86_64-gcc-core"
packages (or "mingw64-i686-gcc-core", if you want the 32-bit version) and
"mingw64-x86_64-glib2.0".

* Installing
* Building with LLVM

To build the hsdis library, run "make build-hsdis". This will build the library
in a separate directory, but not make it available to the JDK in the
configuration. To actually install it in the JDK, run "make install-hsdis".
To build this project using LLVM you need to have LLVM installed. Typical ways
of installation can be "sudo apt install llvm" (on Debian and derivatives),
or "brew install llvm" (on macOS with Homebrew).

Note: The resulting build may not be distributable. Please get legal advice if
you intend to distribute the result of your build.
This has been tested with LLVM v13.0.0, but earlier (and later) versions are
also likely to work.

* Using the library
To build hsdis using LLVM, you must enable it in configure by "bash configure
--with-hsdis=llvm".

The hsdis library will be automatically loaded by Hotspot when you use the
diagnostic option "-XX:+PrintAssembly". Note that since this is a diagnostic
option, you need to unlock these first, so in practice you activate it using
"-XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly".

More information is available at the wiki
[https://wiki.openjdk.java.net/display/HotSpot/PrintAssembly].
If llvm-config is not in your path, you will need to specify the LLVM home using
"--with-llvm=<LLVM home>". If your llvm-config is in ~/my-llvm/bin, then you
should use "--with-llvm=~/my-llvm".
File renamed without changes.
20 changes: 15 additions & 5 deletions src/utils/hsdis/hsdis.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -72,9 +72,14 @@
#ifndef SHARED_TOOLS_HSDIS_H
#define SHARED_TOOLS_HSDIS_H

#ifdef __cplusplus
extern "C"
{
#endif

extern
#ifdef DLL_EXPORT
DLL_EXPORT
#ifdef _WIN32
__declspec(dllexport)
#endif
void* decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
unsigned char* buffer, uintptr_t length,
Expand All @@ -87,8 +92,8 @@ void* decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,

/* This is the compatability interface for older versions of hotspot */
extern
#ifdef DLL_ENTRY
DLL_ENTRY
#ifdef _WIN32
__declspec(dllexport)
#endif
void* decode_instructions(void* start_pv, void* end_pv,
void* (*event_callback)(void*, const char*, void*),
Expand All @@ -115,4 +120,9 @@ typedef void* (*decode_func_stype) (void* start_pv, void* end_pv,
decode_instructions_printf_callback_ftype printf_callback,
void* printf_stream,
const char* options);

#ifdef __cplusplus
}
#endif

#endif /* SHARED_TOOLS_HSDIS_H */
Loading