Building HSDIS in Cygwin
Hsdis is an externally loadable disassembler plugin. It lets you see which assembly instructions the JVM generates for your Java code. On 64-bit Windows, it is a binary called hsdis-amd64.dll (and hsdis-i386.dll on 32-bit platforms). This binary needs to be in the same directory as jvm.dll. Some good resources out there on building the hsdis binary for the OpenJDK include:
- Building hsdis for OpenJDK 15 – Gunnar Morling
- How to build hsdis-amd64.dll? | It’s All Relative (jpbempel.github.io)
- How to build hsdis-amd64.dll and hsdis-i386.dll on Windows (dropzone.nfshost.com)
For Cygwin, the latter resource (from 2012?) is all we need. I like that Gunnar’s blog post covered how to use hsdis after building it so this writeup aims to combine both blogs into a simple Cygwin install-build-disassemble set of instructions.
Building hsdis for 64-bit JVMs
- Install Cygwin with the gcc-core, make, and mingw64-x86_64-gcc-core packages by launching the setup executable using this command (no need to bother selecting packages in the UI since you have already specified them on the command line)
setup-x86_64.exe -P gcc-core -P mingw64-x86_64-gcc-core -P make
- Launch the Cygwin64 terminal
- Clone the OpenJDK repo to get the hsdis sources (if you have not yet set up a Windows OpenJDK Development Environment).
mkdir ~/repos
cd ~/repos
git clone https://github.com/openjdk/jdk
- Run these commands to download GNU binutils and build hsdis (Update 2022-01-07: version downgraded to 2.36 to avoid build failures investigated in Fixing Hsdis Compile Failure in GNU binutils).
cd ~
curl -Lo binutils-2.36.tar.gz https://ftp.gnu.org/gnu/binutils/binutils-2.36.tar.gz
tar xvf binutils-2.36.tar.gz
cd ~/repos/jdk/src/utils/hsdis
make OS=Linux MINGW=x86_64-w64-mingw32 BINUTILS=~/binutils-2.36
- Copy the hsdis binary to the locally built java bin folder
cp src/utils/hsdis/build/Linux-amd64/hsdis-amd64.dll build/windows-x86_64-server-release/jdk/bin/
Testing hsdis
I have created a basic substitution cipher, which we can compile and disassemble using the commands below. Note that these commands save the .java file to a temp folder to make cleanup much easier. Also note the redirection to a file since the output can be voluminous.
cd build/windows-x86_64-server-release/jdk/bin
mkdir -p temp
cd temp
curl -Lo BasicSubstitutionCipher.java https://raw.githubusercontent.com/swesonga/scratchpad/main/apps/crypto/substitution-cipher/BasicSubstitutionCipher.java
../javac BasicSubstitutionCipher.java
../java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:+LogCompilation BasicSubstitutionCipher > BasicSubstitutionCipher.disassembled.txt
Once the disassembly completes, we can view the instructions generated in the BasicSubstitutionCipher.disassembled.txt file.
One open question in this setup is why the installed GNU binutils cannot be used to build hsdis. Seems strange to have to build them from source when the binutils Cygwin package was also installed in step 1 above.
![Cygwin Setup - Select Packages
Select packages to install
Full
Pack age
base-cyg',vin
cygnin-debuginfo
cygwin-devel
cygwin-doc
cygwin-x-doc
cygwin32
cygwin32-binutiIs
cygwin32-binutiIs-debuginfo
cygwin32-defaufMıantfest
cygwi n 32-gcc-core
cygwi n 32-gcc-debuginfo
cyg n 32-gcc*oftran
cygwin32-gcc-g++
cygwin32-gettext
cygwin324ibbfd
cygwin324ibiconv
cygwin324ibtooI
cygwin32%N32apiheadem
cygwi n -ıuntime
cygwin32-zIib
Korg -serv Sh repon er
Korg -serv er-cygwin shrepoft
Side obsolete packages
Src7 Categofies
Uninstall
Skip
31.7-1
3.1 .ı-ı
3.12-1
3.1.5-ı
3.1.6-ı
3.1.7-ı
Size
O
kep Best
Descfiption
O
Jest
332-1
321-1
331-0,1.9814cfd8f693 (Test)
331-0.2.6c1f49f83fde (Test)
331- ı
33.1-1
Keep
Reinstall
L] Lhils
Debug
2,931k
Intial base installation helper scıipt
LIIBtion engine
or cyg',vın
pment files
%fıc documentation including man pages
yectfic document ation
t toolchain
ygwin 3%tt toolchain
or cygwin32-binutiIs
ication manifest for Cygwin32 toolchain
Win 32btt toolchain (C, OpenMP)
or cygwin32-gcc
Win 32btt toolchain (Fortran)
Win 32bt toolchain
t for Cygwin 32bt toolchain
Nin 32btt toolchain
gwin 3%tt toolchain
gwin 3%tt toolchain
,gwin 3%tt toolchain
veadem for Cygwin 3hit toolchain
libs for Cygwin 3hit toolchain
in 32btt toolchain
A breakpad crashrepofter for Cygwin
Debug info for xorg-servercygwincrashrepofter](https://blog.swesonga.org/wp-content/uploads/2021/11/image-1.png)

