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

Open
minux opened this issue Jan 15, 2017 · 4 comments
Open

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

minux opened this issue Jan 15, 2017 · 4 comments
Labels
Milestone

Comments

@minux
Copy link
Member

@minux 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

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jan 16, 2017

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

This comment has been minimized.

Copy link
Member Author

@minux minux commented Jan 16, 2017

@bradfitz bradfitz modified the milestones: Go1.10, Go1.9 Jun 13, 2017
@bradfitz

This comment has been minimized.

Copy link
Contributor

@bradfitz bradfitz commented Jun 13, 2017

Bumping to Go 1.10 for lack of activity.

@rsc

This comment has been minimized.

Copy link
Contributor

@rsc 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.

@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 22, 2017
@gopherbot gopherbot modified the milestones: Go1.11, Unplanned May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.