flat assembler
Message board for the users of flat assembler.

Index > Main > [ FIXED and correct format : COFF/MSCOFF]Mixing Fasm and GCC

Author
Thread Post new topic Reply to topic
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit 26 Mar 2008, 18:36
Hello,

i want to know how to mix a fasm code ( cpu_vendor function ) with a C code compiled by GCC ( the kernel code ) .

Here's the fasm code :

Code:
format coff
public get_vendor

get_vendor:
pusha
xor eax,eax ;   eax <== 0 : get vendor ID
cpuid
mov eax, [esp+36]
mov [eax], ebx
mov [eax+4], edx
mov [eax+8], ecx
mov byte [eax+12],0
popa
xor eax,eax
ret

    


And here's my kernel code :

Code:
...
void clrscr ();
void print (char*);
extern void get_vendor (char *buffer);

int kernel_main(void)
{
  char buffer[13];
    clrscr();
   print(">CPU Vendor : ");
   get_vendor(buffer); // HERE , THE KERNEL REBOOTS INFINITLY
  print(buffer);
      print("\n");
     while(1);
   return(0);
}
...
    


Who can help me please ? thanks =)


Last edited by White-spirit on 27 Mar 2008, 17:22; edited 1 time in total
Post 26 Mar 2008, 18:36
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20460
Location: In your JS exploiting you and your system
revolution 26 Mar 2008, 18:40
See here
Post 26 Mar 2008, 18:40
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 26 Mar 2008, 18:41
i suspect "ESP+36" is wrong, or it's some calling standard issue. Are you sure ccall is used - check out assembly listing of kernel_main().

by the way... since when does GCC use COFF format??? Doesn't it use ELF?
Post 26 Mar 2008, 18:41
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit 26 Mar 2008, 19:55
The main problem isn't the "undefined reference to 'get_vendor'" but it's that the kernel and the fasm code compiles fine and without error/warnings messages .
When i test the kernel with Bochs, it reboots after printing ">CPU Vendor : " .
And yes, i tested COFF, MS COFF and ELF formats ( i'm working on Cygwin ) but it's always the same .
I tested also :
Code:
format coff
public get_vendor as '_get_vendor'

get_vendor:
jmp $ ; DEBUG INSTRUCTION
pusha
xor eax,eax ;   eax <== 0 : get vendor ID
cpuid
mov eax, [esp+36]
mov [eax], ebx
mov [eax+4], edx
mov [eax+8], ecx
mov byte [eax+12],0
popa
xor eax,eax
ret     

to see if it's only a bad stack manipulation but it still reboot .
Help me please =)
Thanks .
Post 26 Mar 2008, 19:55
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 26 Mar 2008, 20:38
did you look at kernel_main() assembly listing, as I told you?
Post 26 Mar 2008, 20:38
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 26 Mar 2008, 22:25
vid wrote:
by the way... since when does GCC use COFF format??? Doesn't it use ELF?


I know DJGPP (alone) uses COFF, but I dunno about Cygwin or MingW, probably MS COFF (since I seem to remember hearing it didn't really completely support ELF).
Post 26 Mar 2008, 22:25
View user's profile Send private message Visit poster's website Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat 27 Mar 2008, 00:06
Maybe the bootloader didn't set SS equal to DS? gcc assumes a "flat" memory model, i.e. DS, ES, and SS all need to be the same segment or at least have the same base address. (It might also require that CS have the same base address; I'm not sure)

Edit: never mind, didn't see your second post.
Post 27 Mar 2008, 00:06
View user's profile Send private message Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit 27 Mar 2008, 12:19
I use a i586-elf cross gcc so yes , the output is ELF ^^'

And no , i don't think that there's a problem with my bootloader, it jumps fine to the kernel which prints "> CPU Vendor : " .
Post 27 Mar 2008, 12:19
View user's profile Send private message Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit 27 Mar 2008, 16:27
Hmm, i changed the compilation instructions from :
Code:
fasm bootsector.asm bootsector.bin
fasm cpuid.asm cpuid.o

gcc -ffreestanding -nostdinc -mno-stack-arg-probe -c kernel.c -o kernel.o

ld -i -e kernel_main -Ttext 0x1000 -o kernel cpuid.o kernel.o
objcopy -R .note -R .comment -S -O binary kernel kernel.bin
    

to
Code:
fasm bootsector.asm bootsector.bin
fasm cpuid.asm cpuid.o

gcc -ffreestanding -nostdinc -mno-stack-arg-probe -c kernel.c -o kernel.o

ld -e kernel_main -Ttext 0x1000 -o kernel cpuid.o kernel.o
ld -i -e kernel_main -Ttext 0x1000 -o kernel cpuid.o kernel.o
objcopy -R .note -R .comment -S -O binary kernel kernel.bin    


And now, ld shows me an error message like this instead of linking both files :
Code:
ld: Relocatable linking with relocations from format coff-i386 (cpuid.o) to form at elf32-i386 (kernel) is not supported
    


So it's an issue with the format ? if i change the fasm output format from "ms coff" to "elf", Bochs boots the kernel fine without crashing, but it executes the fasm program instead of loading the kernel ... it's a problem with the start address ?

Tell me if you need my bootloader's code .

Thanks .

PS : i forgot to tell that i've never mixed an assembler program with C x')
Post 27 Mar 2008, 16:27
View user's profile Send private message Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit 27 Mar 2008, 17:21
Sorry, it works fine with MS COFF ( and COFF ) , the only problem is that the bootloader loads only 2 sectors but my kernel is too big ( 10 sectors ) ^^"
Post 27 Mar 2008, 17:21
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 27 Mar 2008, 22:39
nice one Smile
have fun with OSdev in future Wink
Post 27 Mar 2008, 22:39
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.