pkujhd / goloader

load and run golang code at runtime.
Apache License 2.0
497 stars 58 forks source link

Import external package #49

Closed Atsika closed 2 years ago

Atsika commented 2 years ago

Hi,

I'm trying to compile a Go object that imports package golang.org/x/sys/windows.

However I'm running in the following error:

custom.go:7:2: could not import "golang.org/x/sys/windows": file not found

Here is my code :

package main

import (
    "fmt"

    "golang.org/x/sys/windows"
)

func main() {
    peb := windows.RtlGetCurrentPeb()
    fmt.Println("Image base address:", peb.ImageBaseAddress)
}

Here is the output of go version so you can replicate:

go version go1.17 windows/amd64

Is there any special procedure to compile Go object files that import external packages ?

Thanks in advance.

pkujhd commented 2 years ago

@Atsika you may used -I argument gived import search path for go tool compile

Atsika commented 2 years ago

It doesn't fix the issue. First, I retrieved the package using:

go get  golang.org/x/sys/windows

Then I tried to compile using the argument you specified:

 PS> go tool compile -I C:\Users\user\go\pkg\mod\ .\custom.go
.\custom.go:6:2: could not import "golang.org/x/sys/windows": file not found

And as you can see, on the following screenshot, package is present: image

pkujhd commented 2 years ago

it need a .a lib so, you need use command go tool compile -I C:\Users\user\go\pkg\windows_amd64 .\custom.go it is in readme examples.

go tool compile -I $GOPATH/pkg/`go env GOOS`_`go env GOARCH`/ -o test.o test1.go test2.go
Atsika commented 2 years ago

I've read the README.md, however it's still unclear for me. The command you suggested me won't work since this folder doesn't exist there. Go is installed under C:\Program Files\Go\ on my VM, so the path would be : C:\Program Files\Go\pkg\windows_amd64

Trying go tool compile -I C:\Program Files\Go\pkg\windows_amd64 .\custom.go gives the same error:

PS> go tool compile -I "C:\Program Files\Go\pkg\windows_amd64" .\custom.go
.\custom.go:6:2: could not import "golang.org/x/sys/windows": file not found

If I understand well, I need to compile the golang.org/x/sys/windows first and then my object file. Unfortunatly, I don't understand how to compile the windows package.

pkujhd commented 2 years ago

@Atsika if you do not find $GOPATH/pkg/windows_amd64/golang.org/x/sys/windows, you can copy golang.org/x/sys/windows source code into your $GOPATH/src; then exec go install golang.org/x/sys/windows

Atsika commented 2 years ago

What should be the output of go install golang.org/x/sys/windows ?

image

pkujhd commented 2 years ago

It will be install windows.a in $GOPATH/pkg/windows_amd64

mkdir -p D:\Data\Code\go\pkg\windows_amd64\golang.org\x\sys\
cp $WORK\b001\_pkg_.a D:\Data\Code\go\pkg\windows_amd64\golang.org\x\sys\windows.a
rm -r $WORK\b001\

D:\Data\Code\go\src>go install -a -x -v golang.org\x\sys\windows
Atsika commented 2 years ago

Hey, this worked ! thank you for your support :smiley:

Does my loader need to register every external function I want to use ? Or is there a way to compile statically the external package in my object file ?

pkujhd commented 2 years ago

@Atsika if the function is used in your loader, it doesn‘t need to import.
in addition, you can load a library at the same times like examples