完美解决go报错:malformed DWARF TagVariable entry

今天在运行一个go项目,指定需要go1.13版本。在运行时报错:

malformed DWARF TagVariable entry

搜索后可知这是go语言官方的一个bug。具体可参考:

https://github.com/golang/go/issues/53000

在go1.18中修复了这个bug:

https://go-review.googlesource.com/c/go/+/455055

现在问题来了,项目指定go1.13,不允许升级,那该如何解决这个问呢。

首先在项目中全局搜索该报错,可以发现该报错出现在gcc.go这个文件中

case dwarf.TagVariable:
			name, _ := e.Val(dwarf.AttrName).(string)
			typOff, _ := e.Val(dwarf.AttrType).(dwarf.Offset)
			if name == "" || typOff == 0 {
				if e.Val(dwarf.AttrSpecification) != nil {
					// Since we are reading all the DWARF,
					// assume we will see the variable elsewhere.
					break
				}
				fatalf("malformed DWARF TagVariable entry")
			}

然后找到一个新版本的go环境可知:

		case dwarf.TagVariable:
			name, _ := e.Val(dwarf.AttrName).(string)
			// As of https://reviews.llvm.org/D123534, clang
			// now emits DW_TAG_variable DIEs that have
			// no name (so as to be able to describe the
			// type and source locations of constant strings)
			// like the second arg in the call below:
			//
			//     myfunction(42, "foo")
			//
			// If a var has no name we won't see attempts to
			// refer to it via "C.<name>", so skip these vars
			//
			// See issue 53000 for more context.
			if name == "" {
				break
			}
			typOff, _ := e.Val(dwarf.AttrType).(dwarf.Offset)
			if typOff == 0 {
				if e.Val(dwarf.AttrSpecification) != nil {
					// Since we are reading all the DWARF,
					// assume we will see the variable elsewhere.
					break
				}
				fatalf("malformed DWARF TagVariable entry")
			}

把上面改成下面,然后编译安装Go

# 下载Go 1.13源代码
wget https://dl.google.com/go/go1.13.src.tar.gz
 
# 解压源代码
tar -C /usr/local -xzf go1.13.src.tar.gz
 
# 进入Go源代码目录
cd /usr/local/go/
 
# 修改源代码文件,根据需求进行修改
# 比如,修改src/runtime/runtime.go文件中的某个函数
 
# 编译并安装Go
cd /usr/local/go/src
./make.bash
 
# 将新的Go添加到PATH环境变量
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile
source ~/.profile
 
# 验证Go版本
go version

问题解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值