Using go build to cross compile a kernel file and link it to the loader

248 views
Skip to first unread message

saurav deshpande

unread,
Aug 29, 2020, 9:32:08 AM8/29/20
to golang-nuts
Hello, I was trying to write a basic kernel in golang. Before actually implementing anything in the kernel.go file I wanted to see how go build cross compiler can be used.
I am currently using :
go version go1.15  linux/amd64

I wrote 3 files: a loader (boot.asm)file, a linker(linker.ld) file and an kernel(kernel.go) file.
I want to run the kernel on virtual machine, arch: linux-32bit.
I need step wise help to successfully running this kernel. 
how to compile it with go build and generate a ".o" file,then using linker.ld to link boot.s and kernel.o.
I tired the makefile format of gopher os but didnt get anywhere, so thought if someone could help me from scratch.
Thank you.

Amnon

unread,
Aug 29, 2020, 11:34:13 AM8/29/20
to golang-nuts
Go is not the best language to write a kernel on. But some people have done it as an intellectual exercise.
Look at https://github.com/ycoroneos/G.E.R.T
Or https://github.com/f-secure-foundry/tamago
Or read https://speakerdeck.com/achilleasa/bare-metal-gophers-can-you-write-an-os-kernel-in-go

It is quite a bit of work. But you will learn a fair bit along the way.

Nasir Hussain

unread,
Aug 29, 2020, 4:01:58 PM8/29/20
to Amnon, golang-nuts
I would like to +1 Amnon here,

Although, If you're looking forward to implementing a Kernel in Go, there is a project called GopherOS (https://github.com/gopher-os/gopher-os/) that can be of help to you.

In order to generate the .o file, you can refer to the make file here: https://github.com/gopher-os/gopher-os/blob/master/Makefile#L56 and additionally use the make file for building and loading your Kernel too.

Best,

--
Nasir Hussain

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7d6979b3-d9ed-481b-b47f-fcf83eae4092o%40googlegroups.com.

saurav deshpande

unread,
Aug 30, 2020, 4:29:16 PM8/30/20
to golang-nuts
i watched a video of gopher-os and looked through it's github repository for ways to extract the object files needed and tried his command as follows, but i'm getting these errors which i can't resolve:

GOARCH=386 GOOS=linux go build -n 2>&1 | sed -e "1s|^|set -e\n|" -e "1s|^|export GOOS=linux\n|" -e "1s|^|export GOARCH=386\n|" -e "1s|^|export CGO_ENABLED=0\n|" -e "1s|^|WORK='./build'\n|" -e "1s|^|alias pack='go tool pack'\n|" -e "/^mv/d" -e "s|-extld|-tmpdir='./build' -linkmode=external -extldflags='-nostdlib' -extld|g" | sh 2>&1 | sed -e "s/^/ | /g"

Errors/Warnings:

 | loadinternal: cannot find runtime/cgo
 | /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000008049000

The above command is able to produce an object file in the ./build directory i specified, i can link it without any errors, but when i try to run the executable it doesn't work.

i know there is nothing wrong with the linker script because i have created object files using the gccgo cross-compiler, and they work.
i wish to use the extra set of features the 'go build' cross-compiler is able to provide that gccgo cross-compiler cannot.

Reply all
Reply to author
Forward
0 new messages