OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:23 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: [solved] linker script : trying to merge asm and C code
PostPosted: Fri Apr 08, 2022 3:55 pm 
Offline
Member
Member

Joined: Fri Apr 08, 2022 3:12 pm
Posts: 54
My pMBR boot code finds a boot partition, loads it to the memory and jumps there. I'm loading the boot partition at 0x7c00. With the help of the linker script I'm trying to move from assembler to a 32b C code. A20 gate and protected mode (no paging yet) is enabled. I just can't get the linker to link the symbols to a proper address. Whatever I do I mess it up one way or the other.

boot1 and libsa16 are compiled from assembly code. dummy is my attempt to link C code into it. I'm trying to link them together as they're coming (boot1, libsa16, dummy). Once I'm in 32b code in boot1 I'm trying to call a function from dummy (does a primitive print on screen ; that code works within boot1 asm code) - I'm not able to work it out. The best failed attempt I did was a successful/correct jump to a dummy code where offsets to string are not ok (by the load address 0x7c00).

My last failed attempt on a linker script:
Code:
OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
OUTPUT_ARCH(i386)

ENTRY(_start);
SECTIONS
{

   . = 0;
   .boot1 : {
      boot1.o(.text)
      libsa16.o(.text)
   }
   .data16 : {
      libsa16.o(.data)
      libsa16.o(.rodata)
      libsa16.o(.bss)
   }

   . = 0x7c00 + . ;
   .code32 : {
      dummy.o(.*)
   }


    /* Discard common unwanted/unneeded sections */
    /DISCARD/ : {
        *(.comment);
        *(.debug*);
        *(.note.gnu.build-id);
    }
}

Any hint to stir me to the right direction would be appreciated.

EDIT:

I've edited my post and updated the script to the version I was thinking is correct. I jump to the dummy_main properly but the offsets to strings are not correct.

The excerpt from the boot1.S is this:
Code:
         .code16
..
..
prepm32:
   /* prepare to jump to protected mode */
        cli
        lgdt (gdt_desc)

        movl %cr0, %eax                         /* enable protected mode */
        orl $1, %eax
        movl %eax, %cr0

        ljmp $8, $(LINKADDR + pmode32)          /* flush instruction cache and set the proper cs selector */

        .code32
pmode32:
        movl $0x10, %eax
        movw %ax, %ds
        movw %ax, %es
        movw %ax, %ss
        movl $0x9FFFC, %esp

        movl $(dummy_main), %eax
        addl $0x7c00, %eax
        call *%eax


Last edited by mtbro on Wed Apr 13, 2022 3:24 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: linker script : trying to merge asm and C code
PostPosted: Fri Apr 08, 2022 9:52 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
mtbro wrote:
Code:
   . = 0;

Are you using nonzero segment registers in real mode? I bet that's why you're having so much trouble with your linker script in the first place. The tools you're using assume a flat address space, where the segment base is (almost) always zero.

mtbro wrote:
Code:
        movl $0x9FFFC, %esp

SMM code accesses the EBDA while your OS is running. Placing your stack in the EBDA is a bad idea.


Top
 Profile  
 
 Post subject: Re: linker script : trying to merge asm and C code
PostPosted: Sat Apr 09, 2022 2:07 am 
Offline
Member
Member

Joined: Fri Apr 08, 2022 3:12 pm
Posts: 54
I've shared what I'm trying to do on github: https://github.com/martin-0/mios.

Yes I did set them to non-zero values depending on what I was doing. I benefit from that in mMBR boot code, I can't really give an argument why yes here in boot1.
Thanks for pointing that out, I'll have to put stack into lower address, probably below the bootcode then (07xc00).


Top
 Profile  
 
 Post subject: Re: linker script : trying to merge asm and C code
PostPosted: Sat Apr 09, 2022 12:40 pm 
Offline
Member
Member

Joined: Fri Apr 08, 2022 3:12 pm
Posts: 54
Thank you @Octocontrabass for kicking me to the right direction. Such a simple solution ; I was focusing so much on linker script that I didn't consider this.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 52 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