Categories: Assembly, Cygwin

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:

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

  1. 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
  1. Launch the Cygwin64 terminal
  2. 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
  1. 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
  1. 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.


Categories: DevOps

Downgrading Cygwin

Last week I was trying to set up an OpenJDK build environment on my Windows 11 machine. Building the repo would fail due to segfaults when running make. The workaround is to downgrade Cygwin from 3.3 to 3.2:

  1. Launch the Cygwin setup program (setup-x86_64.exe).
  2. Change the View dropdown to Full.
  3. Enter cygwin into the search box then select the cygwin package..
  4. Open the cygwin package’s dropdown in the New column.
  5. Change the selection from Keep to the desired version as shown in the image below.
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
Cygwin version selection
  1. Click on the Next button then verify that the downgrade is to the desired version.
Cygwin Setup - Review and confirm changes 
Review coriirm 
Uninstall cygwin 3 3 2-1 (automatically added) 
Install cygwin 3 2 0-1 
c
Reviewing cygwin downgrade version

Finish the setup process then launch cygwin and verify that the version is correct by running:

cygcheck -c Cygwin
Verifying cygwin version after downgrading