Go Bare Bones: Difference between revisions

no edit summary
[unchecked revision][unchecked revision]
mNo edit summary
No edit summary
Line 12:
:''Main article: [[GCC Cross-Compiler]], [[Why do I need a Cross Compiler?]]
 
The first thing you should do is set up a GCCGoGCC Cross Compiler that supports Go. To do this read and follow [[GCC Cross-Compiler]] to the letter with one exception. When configuring the build for GCC we need to enable Go to get the i686-elf-gccgo compiler.
 
So instead of using: <source lang="bash">../gcc-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers</source> We use: <source lang="bash">../gcc-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++,go --without-headers</source>
Line 229:
 
Compile using: <source lang="bash">i686-elf-gccgo -static -Werror -nostdlib -nostartfiles -nodefaultlibs -c terminal.go -o terminal.go.o</source>
 
== How imported packages are found ==
 
When you import a package with gccgo it will look for the import data in the following files and will use the first one it finds.
 
* <package name>.gox
* <package name>.so
* <package name>.a
* <package name>.o
 
InA our.gox examplefile it will notcontains findjust the import data as we name our output file terminal.go.o instead of terminal.go to showIf you howwanted to solve an issue like this. To solveextract it wefrom can easily extract the import data fromour terminal.go.o into a newobject file calledwe terminal.gox usinguse: <source lang="bash">i686-elf-objcopy -j .go_export terminal.go.o terminal.gox</source>
 
== Writing a kernel in Go ==
 
Now we create the file kernel.go that containdcontains the Main() function called from our bootstrap assembly we've already created and compiled above. It will import the terminal package we also created & compiled and extracted the import data file from. Then we use that package to print our text to the screen.
 
<source lang="go">package kernel
Line 316 ⟶ 318:
<source lang="bash">i686-elf-gcc -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot.o terminal.go.o kernel.go.o -lgcc</source>
 
The file myos.bin is now your kernel. Note that we are linking against [[libgcc]], which implements various runtime routines that your cross-compiler depends on. Leaving it out will give you problems in the future.
 
== Testing your operating system (QEMU) ==
 
Virtual Machines are very useful for developmentdeveloping operating systems, as they allow you to quickly test your code and have access to the source code during the execution. Otherwise, you would be in for an endless cycle of reboots that would only annoy you. They start very quickly, especially combined with small operating systems such as ours.
 
QEMU supports booting multiboot kernels directly without using a bootable medium:
Anonymous user