Page 1 of 1

[solved] linker script : trying to merge asm and C code

Posted: Fri Apr 08, 2022 3:55 pm
by mtbro
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: Select all

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: Select all

         .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

Re: linker script : trying to merge asm and C code

Posted: Fri Apr 08, 2022 9:52 pm
by Octocontrabass
mtbro wrote:

Code: Select all

	. = 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: Select all

        movl $0x9FFFC, %esp
SMM code accesses the EBDA while your OS is running. Placing your stack in the EBDA is a bad idea.

Re: linker script : trying to merge asm and C code

Posted: Sat Apr 09, 2022 2:07 am
by mtbro
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).

Re: linker script : trying to merge asm and C code

Posted: Sat Apr 09, 2022 12:40 pm
by mtbro
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.