Page 1 of 1

Problems with creating a flat binary for x64 using mingw-w64

Posted: Wed Jan 30, 2013 5:21 am
by scippie
Hi,

Every time I want to start my OS development, I start using some assembler and I create great boot-code. But then, when I see how much work it will be to continue in assembler, I want to get started in C and get very very VERY frustrated that I can't get to an x64-flat-binary, and I give up. Sad, because I think I have great ideas for my OS.

So, can someone please help me get started. I really want to do this in Windows.

I installed MinGW-w64 on my system.
I created a test.c file which will just print out a text to 0xb8000.
The code compiles correctly and links to some kind of binary file which at first glance looks ok, but... ever since I added the function to write text to the 0xb8000 address, it seems that the binary file does not contain the text I am trying to output. The text is of course a constant and I think it is in another segment or something, but I don't know enough about that and I just want everything to be in one continuous block of binary.

This is the C-code:

Code: Select all

void write_string(int colour, const char *string)
{
	volatile char *video = (volatile char*)0xB8000;
	while (*string != 0)
	{
		*video++ = *string++;
		*video++ = colour;
	}
}

int start(void)
{
	write_string(0xF0, "This is a text");
	
mylabel:
  goto mylabel;
}
And this is how I change it to a flat binary:

Code: Select all

C:\MinGW-w64\bin\x86_64-w64-mingw32-gcc.exe -Wall -pedantic-errors test.c -o test.exe -nostdlib -ffreestanding -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -Wl,-Ttext=0x20000
C:\MinGW-w64\x86_64-w64-mingw32\bin\objcopy -O binary -j .text test.exe test.bin
The .exe file contains the "This is a text", but the flat binary doesn't anymore. I would like the text to be there right after the code. How can I do this?

I'm an experienced programmer, but I don't have any experience with other compilers than Borland/Visual, so I'm quite sure I need to add some parameters to gcc or something.

Any help?

Re: Problems with creating a flat binary for x64 using mingw

Posted: Wed Jan 30, 2013 5:42 am
by iansjack
Use the Wiki, and Google, to read about using linker scripts with ld. That way you can control exactly what appears in the finished file, and what format it is.

Re: Problems with creating a flat binary for x64 using mingw

Posted: Wed Jan 30, 2013 5:52 am
by dozniak
scippie wrote:Any help?
man objcopy wrote: -j sectionname
--only-section=sectionname
Copy only the named section from the input file to the output file. This option may be given more than once. Note that using this option inappropriately may make the output file unusable.
You should know which section contains the text "This is a text".

Re: Problems with creating a flat binary for x64 using mingw

Posted: Wed Jan 30, 2013 6:18 am
by xenos
You really shouldn't use a Windows toolchain for osdev. I know that some people do it and are happy with this, but it may require a lot of experimenting and fiddling around with settings and command line parameters to get it working. So I wouldn't recommend it.

The recommended way is to build a GCC cross compiler first, in your case one with (for example) x86_64-elf target. (Even if you don't use ELF files - it will also produce flat binaries and is free of Linux or Windows dependencies, so you have a "bare metal" target, which is best suitable for an OS kernel.) Another advantage is that using a cross compiler is a "standard method" used by most people here, so you will more likely get support here if you go that way.

There's a detailed tutorial in the Wiki on how to build a GCC cross compiler.

Re: Problems with creating a flat binary for x64 using mingw

Posted: Mon Feb 04, 2013 3:19 am
by scippie
XenOS wrote:You really shouldn't use a Windows toolchain for osdev. I know that some people do it and are happy with this, but it may require a lot of experimenting and fiddling around with settings and command line parameters to get it working. So I wouldn't recommend it.
After being grumpy for some time with your answer :wink: I have decided to take your advice. But my linux knowledge is so far away so it took me some time. But I did it! I installed debian in a VM, followed the wiki tutorial and now my asm code is compiled with FASM, and my C code is compiled with x86_64-elf-gcc, and although nothing shows up on screen (but does hlt as instructed in the C code), it seems to produce a correct flat binary. Now only to find out why nothing shows on screen.

Thanks for pushing me :)

Re: Problems with creating a flat binary for x64 using mingw

Posted: Mon Feb 04, 2013 3:29 am
by Love4Boobies
He didn't mean that you should use Linux. He meant that you shouldn't use a toolchain that targets Windows. E.g., MinGW and MinGW-w64 can be used to build GCC cross-compilers and cross-Binutils. If you prefer to be even closer to the UNIX System, you could use SUA (the Microsoft POSIX subsystem) or Cygwin (which aims for Linux compatibility).

Re: Problems with creating a flat binary for x64 using mingw

Posted: Mon Feb 04, 2013 3:32 am
by scippie
Love4Boobies wrote:He didn't mean that you should use Linux. He meant that you shouldn't use a toolchain that targets Windows. E.g., MinGW and MinGW-w64 can be used to build GCC cross-compilers and cross-Binutils. If you prefer to be even closer to the UNIX System, you could use SUA (the Microsoft POSIX subsystem) or Cygwin (which aims for Linux compatibility).
You're right, I misinterpreted his answer. But this seems to work (almost) fine now.

Re: Problems with creating a flat binary for x64 using mingw

Posted: Mon Feb 04, 2013 5:21 am
by gravaera
scippie wrote:Hi,
...

Sad, because I think I have great ideas for my OS.
:cry:

Re: Problems with creating a flat binary for x64 using mingw

Posted: Mon Feb 04, 2013 5:23 am
by Love4Boobies
gravaera wrote:
scippie wrote:Hi,
...

Sad, because I think I have great ideas for my OS.
:cry:
:-({|=

Re: Problems with creating a flat binary for x64 using mingw

Posted: Mon Feb 04, 2013 5:24 am
by scippie
Love4Boobies wrote:
gravaera wrote:
scippie wrote:Hi,
...

Sad, because I think I have great ideas for my OS.
:cry:
:-({|=
Lawl...

Anyway, I have just fixed the problem, my C code runs perfectly now!
Maybe the sadness will now change into the greatest OS ever! :P

Re: Problems with creating a flat binary for x64 using mingw

Posted: Mon Feb 04, 2013 6:48 am
by xenos
scippie wrote:Maybe the sadness will now change into the greatest OS ever! :P
Oh, wonderful, yet another "greatest OS ever" :D

I'm glad to hear that it worked out in the end and now you have a working cross compiler. You will be even happier about this in the future :) As the others already mentioned, switching to Linux was not necessary, but it's not a bad thing at all. Many tutorials for osdevers are written with a focus on Linux users or simply use Linux tools.