Cannot separate assembly into different source files

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
IsaccBarker
Posts: 8
Joined: Tue Jul 26, 2022 4:20 pm

Cannot separate assembly into different source files

Post by IsaccBarker »

Hello!
Just as a preface I've spent the last several hours looking at linker script documentation and examples, but to no avail.
I'm currently writing a bootloader and have gotten so far as to start writing the code to look through a FAT16 filesystem to load a file that contains the rest of my bootloader. The boot process is as follows: stage one, which resides in the boot sector, loads stage two, which resides in the bootstrap section of the FAT16 filesystem, which loads the binary containing the rest of the bootloader.
Stage one, which loads stage two, works just fine. I write an MBR partition table to a raw disk image (using fdisk) and then use dd to insert the stage one binary into the sector where the MBR partition table is.
Stage one consists of multiple source files. Keep in mind stage one works perfectly, reading from the partition table and jumping to stage two :D .
I'm not including the stage one code here, as I don't think it's relevant. Below are my two assembly files for stage two and the linker script.

Code: Select all

# linker.ld
OUTPUT_FORMAT(binary)
ENTRY(_start)

SECTIONS
{
	. = 0x7E3E; /* 0x7E00 (load location) + 0x003E (offset to boot code). */

	.text : {
		*(.text*)
	}
}

Code: Select all

# bootstrap.S
.code16

.text
    .global _start

_start:
    call foo

    hlt

Code: Select all

# fat.S
.text
    .global foo

foo:
    mov $0x2, %dx

    cli
    jmp .

Code: Select all

# Build
x86_64-elf-as -o bootstrap.o bootstrap.S
x86_64-elf-as -o fat.o fat.S
x86_64-elf-gcc -T linker.ld -o bootstrap.bin -ffreestanding -O2 -nostdlib bootloader.o fat.o -lgcc

# Make the disk
dd if=/dev/zero of=$(_ROOT)/target/lasv.dd bs=1M count=16
sfdisk lasv.dd < .sfdiskrc

# Make filesystem
sudo losetup -o $$[2048*512] --sizelimit $$[8*1024*1024] -f lasv.dd
sudo mkfs.fat -v -F 12 -n "BOOT" /dev/loop0

# Insert bootstrap code into FAT16
sudo dd if=bootstrap.bin of=/dev/loop0 conv=notrunc bs=1 count=448 seek=62
When I run the created media in QEMU, and get the content of the register dx (seen in fat.S), it returns a garbage value nowhere near 0x2. It also populates the entire edx register, even though I only moved dx. I'm almost positive that this is an error in my build process/linking as if I move everything in stage two into one file, all is well.

If you need/want to build the project, you can find the code here
Hello!
User avatar
IsaccBarker
Posts: 8
Joined: Tue Jul 26, 2022 4:20 pm

Re: Cannot separate assembly into different source files

Post by IsaccBarker »

Figured it out a bit ago. Nothing to do with the linking/building process, I simply forgot to add .code16 to the top of my source file :)

Sorry about that.
Hello!
Post Reply