gpt4 book ai didi

go - "go build"后没有二进制符号

转载 作者:行者123 更新时间:2023-12-01 23:19:16 25 4
gpt4 key购买 nike

我正在关注 this article要了解 eBPF 跟踪的工作原理,第一步是识别函数的符号,示例代码可从此处获取:https://github.com/pixie-labs/pixie-demos/blob/main/simple-gotracing/app/app.go

但是,在构建之后,我无法找到该符号。 为什么会这样?

$ ls
go.mod main.go
$ grep func main.go
func computeE(iterations int64) float64 {
func main() {
http.HandleFunc("/e", func(w http.ResponseWriter, r *http.Request) {
$ go build
$ objdump --syms ./demowebservice | grep compute
0000000000840a40 g O .bss 0000000000000008 crypto/elliptic.p256Precomputed
00000000008704c0 g O .noptrbss 000000000000000c crypto/elliptic.precomputeOnce
$

Go 版本:-

$ go version
go version go1.16.5 linux/amd64

最佳答案

您的 computeE() 函数将被内联,因此函数名称将不会在可执行二进制文件中留下任何“标记”。您可以使用 go build -gcflags=-m 查看构建过程中正在内联的函数。

$ go build -gcflags=-m |& grep inlining
./main.go:24:17: inlining call to http.HandleFunc
./main.go:24:17: inlining call to http.(*ServeMux).HandleFunc
./main.go:43:12: inlining call to fmt.Printf
./main.go:44:28: inlining call to http.ListenAndServe
./main.go:46:13: inlining call to fmt.Printf
./main.go:40:53: inlining call to computeE <-- NOTE THIS

如果禁用内联:

//go:noinline
func computeE(iterations int64) float64 {
// ...
}

然后再次构建并检查:

$ go build -gcflags=-m |& grep inlining
./main.go:24:17: inlining call to http.HandleFunc
./main.go:24:17: inlining call to http.(*ServeMux).HandleFunc
./main.go:43:12: inlining call to fmt.Printf
./main.go:44:28: inlining call to http.ListenAndServe
./main.go:46:13: inlining call to fmt.Printf

$ objdump --syms ./demowebservice | grep compute

然后输出将是这样的:

000000000062a940 g     F .text  000000000000004c              main.computeE
0000000000840a40 g O .bss 0000000000000008 crypto/elliptic.p256Precomputed
00000000008704c0 g O .noptrbss 000000000000000c crypto/elliptic.precomputeOnce

参见相关:forbid inlining in golang

关于go - "go build"后没有二进制符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68321483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com