jacksonbenete
jacksonbenete

Reputation: 159

Programming Assembly in (WSL2) x86_64 Unix can't make 32-bit syscall?

I'm trying to set up an Assembly Programming Environment on Windows. I do have Linux installed, but for curiosity sake, and also studying purposes for learning WSL, I'm trying to make it run through WSL2 on Windows 10.

I've tried to setup both Alpine and Ubuntu (base) installing only the following tools:

bash bash-doc bash-completion
util-linux pciutils usbutils coreutils binutils findutils grep

It was installed with either apk add or apt install.

Then I've wrote the following example from Programming Ground Up:

.section .data

.section .text
.globl _start

_start:
  movl $1, %eax
  movl $0, %ebx
  int $0x80

I did the compilation and link without any fancy flags, just the minimum like in the book example.

as exit.s -o exit.o
ld exit.o -o exit

I know that those calls like int $0x80 depends on architecture, operation system, etc.

It's returning Segmentation fault when I try to run it on the WSL image.

Searching around I've got to know that Segmentation fault is probably regarding the program not exiting the execution, like when a section doesn't have a ret statement. (In this case, int $0x80 should be enough for activating the kernel and returning/exiting by calling the value 1 set on ax register)

I thought it was because I'm trying to compile and run a 32bit ASM code in a 64bit machine. But, I did a ssh connection to a pub unix server, which is also a Ubuntu x86_64, and the same code could run without any problem.

Is it a WSL2 limitation? Or do this pubnix have something installed or configured for it to accept the 32bit code without any additional flag?

Upvotes: 0

Views: 1398

Answers (1)

jacksonbenete
jacksonbenete

Reputation: 159

The WSL2 do have support to 32-bit libraries and system calls.

So it's a nice way of programming assembly on Windows without the need for dual-boot and assembly emulators.

But it is a mistake to assume that since WSL2 is installed, it's also being used.

  • If you don't have WSL2 installed you need to install it.

If you already have WSL2 installed, you can either convert a WSL1 image to WSL2, or set the WSL version to the version you want (WSL2) to be used when creating/importing your next image.

(In my case, Docker Desktop for Windows automatically installed WSL2 for me)

  • You can check if your image is WSL1 or WSL2 with:
wsl -l -v
  • You can create a WSL2 image by setting WSL2 to be used.
wsl --set-default-version 2

Everytime you create a new image or import, it will be set as WSL2. If you want to go back to WSL1 you can wsl --set-default-version 1.

Now you can create a new image as usual.

  • If your image is version 1 already, you can convert it to version 2 with:
wsl --set-version image_name 2

(Converting an image may take a long time)

  • Instead, you can export your current image, and import/create it again as a WSL2 version, so you can keep both version if needed.
wsl --export current-image image-backup.tar
mkdir new-folder
wsl --import newimg new-folder image-backup.tar

(Exporting and Importing will keep your $HOME and files, it's like making a backup of the entire image)

Upvotes: 2

Related Questions