Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug/elf: (*Section).Data() returns non-zero data for SHT_NOBITS section #18667

Closed
minux opened this issue Jan 15, 2017 · 7 comments
Closed
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@minux
Copy link
Member

minux commented Jan 15, 2017

(*Section).Data fails to take section.Type == Type:elf.SHT_NOBITS into account, and will happily return anything in the file at the section file offset.

// x.go
package main

import (
	"debug/elf"
	"fmt"
	"os"
)

func main() {
	f, err := elf.Open(os.Args[1])
	if err != nil {
		panic(err)
	}
	sect := f.Section(".bss")
	fmt.Printf("%#v\n", *sect)
	data, err := sect.Data()
	if err != nil {
		panic(err)
	}
	fmt.Printf("data = %d bytes\n%q\n", len(data), data)
}

On certain ELF files, debug/elf will output non-zero data for .bss section.
For example, compile the following assembly file as a shared library:

.bss
.comm d, 10
$ cc -o a.so a.c -shared
$ go run x.go a.so
elf.Section{SectionHeader:elf.SectionHeader{Name:".bss", Type:elf.SHT_NOBITS, Flags:elf.SHF_WRITE+elf.SHF_ALLOC, Addr:0x201030, Offset:0x1030, Size:0x20, Link:0x0, Info:0x0, Addralign:0x10, Entsize:0x0, FileSize:0x20}, ReaderAt:(*io.SectionReader)(0xc420016870), sr:(*io.SectionReader)(0xc420016870), compressionType:0, compressionOffset:0}
data = 32 bytes
"GCC: .....\x00\x00.s"

This is because the .comment section happens to be after the .bss section.

Perhaps this is a user error, but I'm not entirely sure. And if it is indeed a bug of debug/elf, I'm not sure whether we should make it return make([]byte, section.Size) or just an error indicating that the section doesn't have file data. I'm leaning towards returning a zero byte slice.

/cc @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

I would be inclined to return an error of the Data method. A SHT_NOBITS section has no data. I don't think that programs that look at ELF data should be encouraged to disregard the section flags.

That said, I would reconsider if that choice breaks real programs that seem otherwise correct.

@ianlancetaylor ianlancetaylor added this to the Go1.9 milestone Jan 16, 2017
@minux
Copy link
Member Author

minux commented Jan 16, 2017 via email

@bradfitz bradfitz added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jun 13, 2017
@bradfitz bradfitz modified the milestones: Go1.10, Go1.9 Jun 13, 2017
@bradfitz
Copy link
Contributor

Bumping to Go 1.10 for lack of activity.

@ianlancetaylor ianlancetaylor added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Jun 26, 2017
@rsc
Copy link
Contributor

rsc commented Jun 26, 2017

If we don't return an error we have to decide between returning []byte{} and make([]byte, Size). Better to return an error and make the user understand what's going on.

@kakkoyun
Copy link

kakkoyun commented Oct 15, 2021

Hello there, I have come across a case where https://pkg.go.dev/debug/elf#File.DWARF fails because of this issue. The ELF file that I have, have a section Name: .debug_gdb_scripts Size: 40 B Type: SHT_NOBITS and a call to elf.DWARF() fails. Is there a time plan to fix this? Happy to contribute, by the way, if you're open to it. cc @seankhliao

@ZekeLu
Copy link
Contributor

ZekeLu commented Sep 10, 2022

If we don't return an error we have to decide between returning []byte{} and make([]byte, Size). Better to return an error and make the user understand what's going on.

It's implemented in https://go.dev/cl/375216 as to read from a zeroReader (in essence the same as returning make([]byte, Size)). I think this issue can be closed now.

@ianlancetaylor
Copy link
Contributor

Thanks.

@golang golang locked and limited conversation to collaborators Sep 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants