Skip to content

Commit

Permalink
fix: follow the official objfile implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Jan 25, 2024
1 parent a913262 commit d0c7c9d
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,57 @@ func (e *elfFile) GetFile() *os.File {
return e.osFile
}

func (e *elfFile) getSymbolData(start, end string) ([]byte, error) {
elfSyms, err := e.file.Symbols()
if err != nil {
return nil, err
}
var addr, eaddr uint64
for _, s := range elfSyms {
if s.Name == start {
addr = s.Value
} else if s.Name == end {
eaddr = s.Value
}
if addr != 0 && eaddr != 0 {
break
}
}
if addr == 0 || eaddr < addr {
return nil, fmt.Errorf("could not find the symbol %s", start)
}
size := eaddr - addr
data := make([]byte, size)
for _, prog := range e.file.Progs {
if prog.Vaddr <= addr && addr+size-1 <= prog.Vaddr+prog.Filesz-1 {
if _, err := prog.ReadAt(data, int64(addr-prog.Vaddr)); err != nil {
return nil, fmt.Errorf("could not read the symbol data: %w", err)
}
return data, nil
}
}
return nil, fmt.Errorf("could not find the symbol %s", start)
}

func (e *elfFile) getPCLNTab() (*gosym.Table, error) {
pclnSection := e.file.Section(".gopclntab")
if pclnSection == nil {
// No section found. Check if the PIE section exist instead.
pclnSection = e.file.Section(".data.rel.ro.gopclntab")
}
if pclnSection == nil {
return nil, fmt.Errorf("no gopclntab section found")
}
var pclndat []byte
var err error

pclndat, err := pclnSection.Data()
if err != nil {
return nil, fmt.Errorf("could not get the data for the pclntab: %w", err)
if pclnSection == nil {
pclndat, err = e.getSymbolData("runtime.pclntab", "runtime.epclntab")
if err != nil {
return nil, fmt.Errorf("could not get the pclntab data: %w", err)
}
} else {
pclndat, err = pclnSection.Data()
if err != nil {
return nil, fmt.Errorf("could not get the data for the pclntab: %w", err)
}
}

var pcln *gosym.LineTable
Expand Down

0 comments on commit d0c7c9d

Please sign in to comment.