OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 2:10 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Cannot separate assembly into different source files
PostPosted: Thu Jul 28, 2022 9:21 pm 
Offline
User avatar

Joined: Tue Jul 26, 2022 4:20 pm
Posts: 8
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:
# linker.ld
OUTPUT_FORMAT(binary)
ENTRY(_start)

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

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


Code:
# bootstrap.S
.code16

.text
    .global _start

_start:
    call foo

    hlt


Code:
# fat.S
.text
    .global foo

foo:
    mov $0x2, %dx

    cli
    jmp .


Code:
# 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!


Top
 Profile  
 
 Post subject: Re: Cannot separate assembly into different source files
PostPosted: Fri Jul 29, 2022 9:26 pm 
Offline
User avatar

Joined: Tue Jul 26, 2022 4:20 pm
Posts: 8
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Amazonbot [bot], Bing [Bot], DotBot [Bot], Google [Bot], Majestic-12 [Bot], mrjbom and 72 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group