[solved] linker script : trying to merge asm and C code
Posted: Fri Apr 08, 2022 3:55 pm
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:
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:
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);
}
}
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