Skip to content

Commit 46a49eb

Browse files
committed
cmd/internal/objfile: align the load address of ELF binary
The ELF ABI just requires that the address and the offset of a segment are congruent modulo the alignment, but does not require the start address to be aligned. While usually the segment's start address is aligned, apparently the zig linker generates binary with unaligned address. At the run time, the memory mapping that contains the segment starts at an aligned address (rounding down). Use the aligned address for the load address, which matches the mapping. Apparently this is what the pprof library expects. Fixes #59466. Change-Id: Ife78909b20b7bc975ac4c76f2c5f5db325ddec9b Reviewed-on: https://go-review.googlesource.com/c/go/+/483035 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Than McIntosh <[email protected]> Run-TryBot: Cherry Mui <[email protected]>
1 parent 0d3c23f commit 46a49eb

File tree

1 file changed

+6
-1
lines changed
  • src/cmd/internal/objfile

1 file changed

+6
-1
lines changed

src/cmd/internal/objfile/elf.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ func (f *elfFile) goarch() string {
134134
func (f *elfFile) loadAddress() (uint64, error) {
135135
for _, p := range f.elf.Progs {
136136
if p.Type == elf.PT_LOAD && p.Flags&elf.PF_X != 0 {
137-
return p.Vaddr, nil
137+
// The memory mapping that contains the segment
138+
// starts at an aligned address. Apparently this
139+
// is what pprof expects, as it uses this and the
140+
// start address of the mapping to compute PC
141+
// delta.
142+
return p.Vaddr - p.Vaddr%p.Align, nil
138143
}
139144
}
140145
return 0, fmt.Errorf("unknown load address")

0 commit comments

Comments
 (0)