goassemblystatic-linking

Use Go runtime with -buildmode=c-archive


Is it possible to have Go code use the Go runtime with this buildmode?
-buildmode=c-archive

I am building my Go code as a static library and linking it with .o files that were compiled from native assembly code. The project launches in the assembly code and calls into the Go code. Then the Go code starts executing in runtime.rt0_go to start the process of initializing the runtime.

Near the end of runtime.main in the proc.go file, the code exits the Go runtime before running main.main. Since isarchive is true we just return out of runtime.main.

    if isarchive || islibrary {
        return
    }

    fn := main_main
    fn() 

So, I'm wondering if it's possible to use Go runtime features when compiling as an archive file. If it is, I think I may be using the wrong method to accomplish my goal.


Solution

  • This method of using Go runtime features in Go code is viable. I learned that isarchive can be modified with Go assembly. I did this before calling runtime.rt0_go.

    The Go assembly to change isarchive looks like this:

    MOVD    $0,     R0
    MOVB    R0,     runtime·isarchive(SB)
    

    Since isarchive is now 0, the code enters main.main.