Skip to content

Commit effac9b

Browse files
committed
Create hsdis backend using LLVM
1 parent 03c2b73 commit effac9b

File tree

6 files changed

+406
-8
lines changed

6 files changed

+406
-8
lines changed

make/Hsdis.gmk

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ ifeq ($(call isTargetOs, windows), true)
4646

4747
$(eval $(call DefineNativeToolchain, TOOLCHAIN_MINGW, \
4848
CC := $(MINGW_BASE)-gcc, \
49+
CXX := $(MINGW_BASE)-g++, \
4950
LD := $(MINGW_BASE)-ld, \
5051
OBJCOPY := $(MINGW_BASE)-objcopy, \
5152
RC := $(RC), \
@@ -78,19 +79,40 @@ else
7879
HSDIS_TOOLCHAIN_CFLAGS := $(CFLAGS_JDKLIB)
7980
HSDIS_TOOLCHAIN_LDFLAGS := $(LDFLAGS_JDKLIB)
8081
HSDIS_TOOLCHAIN_LIBS := -ldl
82+
83+
ifeq ($(HSDIS_BACKEND), llvm)
84+
# Use C++ instead of C
85+
HSDIS_TOOLCHAIN_CFLAGS := $(CXXFLAGS_JDKLIB)
86+
HSDIS_TOOLCHAIN := TOOLCHAIN_LINK_CXX
87+
endif
88+
endif
89+
90+
ifeq ($(HSDIS_BACKEND), llvm)
91+
ifeq ($(call isTargetOs, linux), true)
92+
LLVM_OS := pc-linux-gnu
93+
else ifeq ($(call isTargetOs, macosx), true)
94+
LLVM_OS := apple-darwin
95+
else ifeq ($(call isTargetOs, windows), true)
96+
LLVM_OS := pc-windows-msvc
97+
else
98+
$(error No support for LLVM on this platform)
99+
endif
100+
101+
HSDIS_CFLAGS += -DLLVM_DEFAULT_TRIPLET='"$(OPENJDK_TARGET_CPU)-$(LLVM_OS)"'
81102
endif
82103

83104

84105
$(eval $(call SetupJdkLibrary, BUILD_HSDIS, \
85106
NAME := hsdis, \
86-
SRC := $(TOPDIR)/src/utils/hsdis, \
107+
SRC := $(TOPDIR)/src/utils/hsdis/$(HSDIS_BACKEND), \
108+
EXTRA_HEADER_DIRS := $(TOPDIR)/src/utils/hsdis, \
87109
TOOLCHAIN := $(HSDIS_TOOLCHAIN), \
88110
OUTPUT_DIR := $(HSDIS_OUTPUT_DIR), \
89111
OBJECT_DIR := $(HSDIS_OUTPUT_DIR), \
90112
DISABLED_WARNINGS_gcc := undef format-nonliteral sign-compare, \
91113
DISABLED_WARNINGS_clang := undef format-nonliteral, \
92114
CFLAGS := $(HSDIS_TOOLCHAIN_CFLAGS) $(HSDIS_CFLAGS), \
93-
LDFLAGS := $(HSDIS_TOOLCHAIN_LDFLAGS) $(SHARED_LIBRARY_FLAGS), \
115+
LDFLAGS := $(HSDIS_TOOLCHAIN_LDFLAGS) $(HSDIS_LDFLAGS) $(SHARED_LIBRARY_FLAGS), \
94116
LIBS := $(HSDIS_LIBS) $(HSDIS_TOOLCHAIN_LIBS), \
95117
))
96118

make/autoconf/jdk-options.m4

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ AC_DEFUN([JDKOPT_BUILD_BINUTILS],
788788
AC_DEFUN_ONCE([JDKOPT_SETUP_HSDIS],
789789
[
790790
AC_ARG_WITH([hsdis], [AS_HELP_STRING([--with-hsdis],
791-
[what hsdis backend to use ('none', 'binutils') @<:@none@:>@])])
791+
[what hsdis backend to use ('none', 'binutils', 'llvm') @<:@none@:>@])])
792792
793793
AC_ARG_WITH([binutils], [AS_HELP_STRING([--with-binutils],
794794
[where to find the binutils files needed for hsdis/binutils])])
@@ -824,6 +824,7 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_HSDIS],
824824
test -e $BINUTILS_DIR/libiberty/libiberty.a; then
825825
AC_MSG_RESULT([$BINUTILS_DIR])
826826
HSDIS_CFLAGS="-I$BINUTILS_DIR/include -I$BINUTILS_DIR/bfd -DLIBARCH_$OPENJDK_TARGET_CPU_LEGACY_LIB"
827+
HSDIS_LDFLAGS=""
827828
HSDIS_LIBS="$BINUTILS_DIR/bfd/libbfd.a $BINUTILS_DIR/opcodes/libopcodes.a $BINUTILS_DIR/libiberty/libiberty.a $BINUTILS_DIR/zlib/libz.a"
828829
else
829830
AC_MSG_RESULT([invalid])
@@ -837,12 +838,27 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_HSDIS],
837838
AC_MSG_NOTICE([--with-binutils to point to a pre-built binutils installation.])
838839
AC_MSG_ERROR([Cannot continue])
839840
fi
841+
elif test "x$with_hsdis" = xllvm; then
842+
HSDIS_BACKEND=llvm
843+
AC_MSG_RESULT(['llvm'])
844+
# Macs with homebrew has llvm in /usr/local/opt
845+
UTIL_LOOKUP_PROGS(LLVM_CONFIG, llvm-config, [$PATH:/usr/local/opt/llvm/bin])
846+
if test "x$LLVM_CONFIG" = x; then
847+
AC_MSG_NOTICE([Cannot locate llvm-config which is needed for hsdis/llvm. Try using LLVM_CONFIG=<path>.])
848+
AC_MSG_ERROR([Cannot continue])
849+
fi
850+
851+
# We need the LLVM flags and libs, and llvm-config provides them for us.
852+
HSDIS_CFLAGS=`$LLVM_CONFIG --cflags`
853+
HSDIS_LDFLAGS=`$LLVM_CONFIG --ldflags`
854+
HSDIS_LIBS=`$LLVM_CONFIG --libs $OPENJDK_TARGET_CPU_ARCH ${OPENJDK_TARGET_CPU_ARCH}disassembler`
840855
else
841856
AC_MSG_RESULT([invalid])
842857
AC_MSG_ERROR([Incorrect hsdis backend "$with_hsdis"])
843858
fi
844859
845860
AC_SUBST(HSDIS_BACKEND)
846861
AC_SUBST(HSDIS_CFLAGS)
862+
AC_SUBST(HSDIS_LDFLAGS)
847863
AC_SUBST(HSDIS_LIBS)
848864
])

make/autoconf/spec.gmk.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ ALLOW_ABSOLUTE_PATHS_IN_OUTPUT := @ALLOW_ABSOLUTE_PATHS_IN_OUTPUT@
361361

362362
HSDIS_BACKEND := @HSDIS_BACKEND@
363363
HSDIS_CFLAGS := @HSDIS_CFLAGS@
364+
HSDIS_LDFLAGS := @HSDIS_LDFLAGS@
364365
HSDIS_LIBS := @HSDIS_LIBS@
365366

366367
# The boot jdk to use. This is overridden in bootcycle-spec.gmk. Make sure to keep
File renamed without changes.

src/utils/hsdis/hsdis.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -72,9 +72,14 @@
7272
#ifndef SHARED_TOOLS_HSDIS_H
7373
#define SHARED_TOOLS_HSDIS_H
7474

75+
#ifdef __cplusplus
76+
extern "C"
77+
{
78+
#endif
79+
7580
extern
76-
#ifdef DLL_EXPORT
77-
DLL_EXPORT
81+
#ifdef _WIN32
82+
__declspec(dllexport)
7883
#endif
7984
void* decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
8085
unsigned char* buffer, uintptr_t length,
@@ -87,8 +92,8 @@ void* decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
8792

8893
/* This is the compatability interface for older versions of hotspot */
8994
extern
90-
#ifdef DLL_ENTRY
91-
DLL_ENTRY
95+
#ifdef _WIN32
96+
__declspec(dllexport)
9297
#endif
9398
void* decode_instructions(void* start_pv, void* end_pv,
9499
void* (*event_callback)(void*, const char*, void*),
@@ -115,4 +120,9 @@ typedef void* (*decode_func_stype) (void* start_pv, void* end_pv,
115120
decode_instructions_printf_callback_ftype printf_callback,
116121
void* printf_stream,
117122
const char* options);
123+
124+
#ifdef __cplusplus
125+
}
126+
#endif
127+
118128
#endif /* SHARED_TOOLS_HSDIS_H */

0 commit comments

Comments
 (0)