Assemble 32 bit mode instructions in elf64 format with GCC (gas/as)

Viewed 75

I'm trying to port an OS development tutorial from NASM Intel syntax to GAS (gcc/as).

I translated to AT&T syntax with no problems, except for one directive: bits 32. This is necessary because the processor is still in protected mode (32 bit mode) at the multiboot entry point, but we're building a 64-bit kernel as a 64-bit ELF executable.

With NASM, I would put the bits 32 directive at the beginning of my file (boot.asm), and then assemble with: nasm -f elf64 boot.asm -o boot.o


I found online that i could replace bits 32 directive with the -m32 option for GCC (equivalent to as --32):

gcc -c -m32 boot.S -o boot.o

However this turns the output format to elf32, when I wanted elf64 to link with compiler-generated 64-bit code, since the entry point switches to long mode.

How can I solve this? Is there any other way to use 32 bit instructions without -m32 or a way to specify the mode separately from the output format with GCC and/or the GNU assembler?

link of the tutorial: https://os.phil-opp.com/multiboot-kernel/

1 Answers

I found what I was missing:

The bits 32 directive should simply be replaced with .code32 and it works.

Related