ebitengine/purego

Static linking on arm but dynamic on x86

jhiemstrawisc opened this issue · 8 comments

I noticed recently that compiling your libc example has different behavior on arm64 than it does on x86 -- on arm, it produces a static binary, but on x86 it's dynamically linked, even with CGO_ENABLED=0.

Is this intended? I initially observed similar behavior after using purego in another project, where my x86 go binaries broke because they switched from static to dynamic.

Which OS, go version, purego version are you using?

Amd64, and arm64 should be dynamically linked. How are you determining they are static?

@jhiemstrawisc is this still an issue? If not, I'd like to close it.

Closing as there hasn't been a response and this doesn't appear to actually be an issue.

I also noticed this issue. Using the ldd command, it can be seen that dynamic libraries are linked in amd64, while this situation does not exist in arm64

Can you provide a minimal reproducible code? Both architectures should be dynamically linked.

I am cross compiling linux/arm64 on linux/amd64, do I don‘t know if it is related to this?

main.go

package main

import (
	"github.com/ebitengine/purego"
)

func openLibrary(name string) (uintptr, error) {
	return purego.Dlopen(name, purego.RTLD_NOW|purego.RTLD_GLOBAL)
}

// CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o puts_amd64 main.go
// CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o puts_arm64 main.go
func main() {
	libc, err := openLibrary("libc.so.6")
	if err != nil {
		panic(err)
	}
	var puts func(string)
	purego.RegisterLibFunc(&puts, libc, "puts")
	puts("Calling C from Go without Cgo!")
}

go build

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o puts_amd64 main.go
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o puts_arm64 main.go

result

puts_amd64:
        linux-vdso.so.1 (0x00007ffc642cd000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f629ad48000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f629a957000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f629a738000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f629af4c000)
puts_arm64:
        not a dynamic executable

I don't have an amd64 computer but on my arm64 linux it was amd64 that gave a not a dynamic executable. I think it's a limitation of the natively installed version of ldd that only supports whatever architecture the system is.

desktop:~/purego/examples/libc$ CGO_ENABLED=0 GOARCH=arm64 go build -o libc_arm64
desktop:~/purego/examples/libc$ CGO_ENABLED=0 GOARCH=amd64 go build -o libc_amd64
desktop:~/purego/examples/libc$ ldd libc_arm64
	linux-vdso.so.1 (0x0000ffffa0d00000)
	libdl.so.2 => /lib/aarch64-linux-gnu/libdl.so.2 (0x0000ffffa0ca0000)
	libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000ffffa0af0000)
	libpthread.so.0 => /lib/aarch64-linux-gnu/libpthread.so.0 (0x0000ffffa0ad0000)
	/lib/ld-linux-aarch64.so.1 (0x0000ffffa0cc7000)
desktop:~/purego/examples/libc$ ldd libc_amd64
	not a dynamic executable