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

Programming, for all ages and all languages.
Post Reply
scippie
Member
Member
Posts: 40
Joined: Wed Jun 27, 2012 3:57 am

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

Post 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?
User avatar
iansjack
Member
Member
Posts: 4686
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

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

Post 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".
Learn to read.
User avatar
xenos
Member
Member
Posts: 1118
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

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

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
scippie
Member
Member
Posts: 40
Joined: Wed Jun 27, 2012 3:57 am

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

Post 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 :)
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

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

Post 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).
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
scippie
Member
Member
Posts: 40
Joined: Wed Jun 27, 2012 3:57 am

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

Post 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.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

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

Post by gravaera »

scippie wrote:Hi,
...

Sad, because I think I have great ideas for my OS.
:cry:
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

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

Post by Love4Boobies »

gravaera wrote:
scippie wrote:Hi,
...

Sad, because I think I have great ideas for my OS.
:cry:
:-({|=
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
scippie
Member
Member
Posts: 40
Joined: Wed Jun 27, 2012 3:57 am

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

Post 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
User avatar
xenos
Member
Member
Posts: 1118
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

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

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply