我是一个新的golang。
我试图插入一些C函数调用(特别是m5ops)到golang的运行时(特别是src/proc.go)。
我尝试使用cgo,这导致将此导入添加到proc.go
的开头:
// #cgo CFLAGS: -I$(GEM5_PATH)/include -static
// #cgo LDFLAGS: -L$(GEM5_PATH)/util/m5/build/x86/out -lm5
// #include <gem5/m5ops.h>
import "C"
但是当我尝试使用./all.bash
重建时,脚本输出如下,然后退出:
./all.bash
Building Go cmd/dist using /usr/local/go. (go1.20.5 linux/amd64)
Building Go toolchain1 using /usr/local/go.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
go tool dist: /home/landerx/goroot/src/runtime/proc.go imports C
以上输出的更详细版本:
./all.bash -v
Building Go cmd/dist using /usr/local/go. (go1.20.5 linux/amd64)
cmd/dist
Building Go toolchain1 using /usr/local/go.
internal/goarch
internal/coverage/rtcov
internal/goos
internal/race
internal/unsafeheader
internal/goexperiment
unicode
runtime/internal/syscall
sync/atomic
runtime/internal/atomic
internal/cpu
unicode/utf8
internal/itoa
math/bits
internal/abi
runtime/internal/math
runtime/internal/sys
bootstrap/internal/goarch
bootstrap/internal/goexperiment
bootstrap/math/bits
encoding
bootstrap/internal/unsafeheader
bootstrap/internal/platform
bootstrap/internal/coverage
internal/bytealg
bootstrap/internal/abi
math
unicode/utf16
bootstrap/internal/goversion
internal/goversion
bootstrap/internal/race
bootstrap/cmd/compile/internal/test
runtime
internal/reflectlite
runtime/metrics
sync
internal/testlog
internal/godebug
bootstrap/internal/bisect
math/rand
errors
sort
bootstrap/sort
io
internal/oserror
internal/safefilepath
path
strconv
bootstrap/strconv
syscall
bootstrap/container/heap
hash
text/tabwriter
bytes
strings
hash/adler32
hash/fnv
hash/crc32
crypto
reflect
bootstrap/internal/types/errors
go/build/constraint
bootstrap/go/build/constraint
bufio
regexp/syntax
html
internal/syscall/execenv
internal/syscall/unix
time
regexp
context
io/fs
internal/poll
internal/fmtsort
bootstrap/internal/saferio
encoding/binary
os
bootstrap/cmd/internal/notsha256
encoding/base64
bootstrap/cmd/internal/sys
crypto/md5
bootstrap/internal/lazyregexp
internal/lazyregexp
path/filepath
fmt
os/exec
log
bootstrap/internal/buildcfg
flag
bootstrap/cmd/internal/src
text/scanner
bootstrap/cmd/internal/edit
bootstrap/cmd/internal/pkgpath
go/token
bootstrap/compress/flate
bootstrap/internal/zstd
go/doc/comment
bootstrap/debug/dwarf
encoding/json
runtime/debug
bootstrap/math/big
bootstrap/cmd/internal/bio
net/url
go/scanner
bootstrap/cmd/internal/objabi
bootstrap/cmd/compile/internal/abt
bootstrap/cmd/internal/goobj
bootstrap/cmd/internal/gcprog
bootstrap/cmd/internal/quoted
bootstrap/compress/zlib
encoding/hex
compress/flate
go/ast
internal/buildcfg
internal/goroot
bootstrap/cmd/internal/dwarf
runtime/trace
bootstrap/cmd/internal/archive
bootstrap/cmd/internal/obj
bootstrap/debug/macho
bootstrap/debug/elf
bootstrap/debug/pe
bootstrap/internal/xcoff
bootstrap/cmd/link/internal/dwtest
compress/gzip
bootstrap/internal/profile
runtime/pprof
go/doc
go/printer
go/internal/typeparams
go/parser
bootstrap/cmd/internal/codesign
bootstrap/go/constant
bootstrap/internal/pkgbits
bootstrap/cmd/compile/internal/syntax
bootstrap/cmd/link/internal/benchmark
bootstrap/cmd/compile/internal/pgo/internal/graph
go/build
go/format
bootstrap/cmd/cgo
bootstrap/cmd/internal/obj/loong64
bootstrap/cmd/internal/obj/mips
bootstrap/cmd/internal/obj/wasm
bootstrap/cmd/internal/obj/arm
bootstrap/cmd/internal/obj/arm64
bootstrap/cmd/internal/obj/s390x
bootstrap/cmd/internal/obj/riscv
bootstrap/cmd/internal/obj/ppc64
bootstrap/cmd/internal/obj/x86
bootstrap/cmd/asm/internal/flags
bootstrap/cmd/compile/internal/base
bootstrap/cmd/compile/internal/logopt
bootstrap/cmd/asm/internal/lex
bootstrap/cmd/link/internal/sym
bootstrap/cmd/link/internal/loader
bootstrap/cmd/compile/internal/bitvec
bootstrap/cmd/compile/internal/types
bootstrap/cmd/compile/internal/types2
bootstrap/cmd/link/internal/loadelf
bootstrap/cmd/link/internal/loadmacho
bootstrap/cmd/link/internal/loadpe
bootstrap/cmd/link/internal/loadxcoff
bootstrap/cmd/compile/internal/typebits
bootstrap/cmd/compile/internal/ir
bootstrap/cmd/asm/internal/arch
bootstrap/cmd/link/internal/ld
bootstrap/cmd/asm/internal/asm
bootstrap/cmd/asm
bootstrap/cmd/compile/internal/abi
bootstrap/cmd/compile/internal/deadcode
bootstrap/cmd/compile/internal/typecheck
bootstrap/cmd/compile/internal/objw
bootstrap/cmd/compile/internal/staticdata
bootstrap/cmd/compile/internal/compare
bootstrap/cmd/compile/internal/pgo
bootstrap/cmd/compile/internal/coverage
bootstrap/cmd/compile/internal/loopvar
bootstrap/cmd/compile/internal/importer
bootstrap/cmd/compile/internal/escape
bootstrap/cmd/compile/internal/inline
bootstrap/cmd/compile/internal/reflectdata
bootstrap/cmd/compile/internal/devirtualize
bootstrap/cmd/compile/internal/staticinit
bootstrap/cmd/compile/internal/ssa
bootstrap/cmd/link/internal/amd64
bootstrap/cmd/link/internal/loong64
bootstrap/cmd/link/internal/arm
bootstrap/cmd/link/internal/mips
bootstrap/cmd/link/internal/mips64
bootstrap/cmd/link/internal/arm64
bootstrap/cmd/link/internal/ppc64
bootstrap/cmd/link/internal/riscv64
bootstrap/cmd/link/internal/s390x
bootstrap/cmd/link/internal/wasm
bootstrap/cmd/link/internal/x86
bootstrap/cmd/link
bootstrap/cmd/compile/internal/liveness
bootstrap/cmd/compile/internal/ssagen
bootstrap/cmd/compile/internal/amd64
bootstrap/cmd/compile/internal/arm
bootstrap/cmd/compile/internal/arm64
bootstrap/cmd/compile/internal/dwarfgen
bootstrap/cmd/compile/internal/loong64
bootstrap/cmd/compile/internal/mips
bootstrap/cmd/compile/internal/mips64
bootstrap/cmd/compile/internal/wasm
bootstrap/cmd/compile/internal/s390x
bootstrap/cmd/compile/internal/riscv64
bootstrap/cmd/compile/internal/ppc64
bootstrap/cmd/compile/internal/walk
bootstrap/cmd/compile/internal/x86
bootstrap/cmd/compile/internal/noder
bootstrap/cmd/compile/internal/pkginit
bootstrap/cmd/compile/internal/gc
bootstrap/cmd/compile
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
runtime
go tool dist: /home/landerx/goroot/src/runtime/proc.go imports C
尝试了CGO_ENABLED=1
标志,似乎没有任何区别。
尝试只运行make.bash
,似乎没有任何区别。
我想知道这是否是一个依赖问题,但没有看到任何相关的消息,伪包cgo
,也没有看到任何错误,从我的vscode。
1条答案
按热度按时间cvxl0en21#
好吧...最后,让我来回答这个问题。
为什么它不工作
经过相当深入的调查,我发现Go(tool dist)似乎不允许在大多数Go内部源文件中导入cgo(
import "C"
)(链接)Walkarounds
仍然有一些walkarounds来实现效果,这是在go中使用m5ops。
1.通过全局变量传递函数指针
你可以在
proc.go
中定义全局函数指针,在你的go应用程序中,导入cgo,并将这些全局函数指针分配给你的m5ops函数。不支持调用C函数指针,所以你需要用一个golang函数 Package 它。2. Go Asm
Go在其内部源文件中使用了大量的Go Asm,以处理堆栈切换等。
另一方面,m5ops将在最后作为单个伪指令内联。
因此,遵循Go Asm的语法和函数绑定(发生在asm_amd64.s和stubs.go中),可以编写一个包含m5ops指令的asm函数,然后在
runtime.go
中的任何地方调用它。其他信息
runtime.go
中,当它切换到系统堆栈时,它就不能再执行文件(或包?),所以如果你试图将这些函数调用插入到将在系统堆栈上运行的代码中,方法1将不起作用。最后,我找到了第二种方法。