cmd/link: zero elf addr for debug sections

The Addr should be zero if SHF_ALLOC is not set.

Update #51939

Change-Id: I030f6243d05efabe6b9ebf558e9c0201f7922d23
Reviewed-on: https://go-review.googlesource.com/c/go/+/395919
Trust: mzh <mzh@golangcn.org>
Run-TryBot: mzh <mzh@golangcn.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Meng Zhuo 2022-03-26 14:44:09 +08:00 committed by mzh
parent 8fefeabb35
commit 63169c8bdf
2 changed files with 33 additions and 3 deletions

View file

@ -469,3 +469,31 @@ func TestPIESize(t *testing.T) {
})
}
}
func TestIssue51939(t *testing.T) {
testenv.MustHaveGoBuild(t)
t.Parallel()
td := t.TempDir()
goFile := filepath.Join(td, "issue51939.go")
if err := os.WriteFile(goFile, []byte(goSource), 0444); err != nil {
t.Fatal(err)
}
outFile := filepath.Join(td, "issue51939.exe")
goTool := testenv.GoToolPath(t)
cmd := exec.Command(goTool, "build", "-o", outFile, goFile)
if out, err := cmd.CombinedOutput(); err != nil {
t.Logf("%s", out)
t.Fatal(err)
}
ef, err := elf.Open(outFile)
if err != nil {
t.Fatal(err)
}
for _, s := range ef.Sections {
if s.Flags&elf.SHF_ALLOC == 0 && s.Addr != 0 {
t.Errorf("section %s should not allocated with addr %x", s.Name, s.Addr)
}
}
}

View file

@ -1100,16 +1100,18 @@ func elfshbits(linkmode LinkMode, sect *sym.Section) *ElfShdr {
sh.Flags |= uint64(elf.SHF_TLS)
sh.Type = uint32(elf.SHT_NOBITS)
}
if linkmode != LinkExternal {
sh.Addr = sect.Vaddr
}
if strings.HasPrefix(sect.Name, ".debug") || strings.HasPrefix(sect.Name, ".zdebug") {
sh.Flags = 0
sh.Addr = 0
if sect.Compressed {
sh.Flags |= uint64(elf.SHF_COMPRESSED)
}
}
if linkmode != LinkExternal {
sh.Addr = sect.Vaddr
}
sh.Addralign = uint64(sect.Align)
sh.Size = sect.Length
if sect.Name != ".tbss" {