Skip to content

Commit

Permalink
feat: amake json parsing cleaner + reduce API froth
Browse files Browse the repository at this point in the history
  • Loading branch information
actuallyachraf committed Jun 2, 2021
1 parent 67a2161 commit da2a45f
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 15 deletions.
29 changes: 29 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}"
},
{
"name": "Attach to Process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": 0
},
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/saferwall/elf"
)


func main() {

p, err := elf.New("/bin/ls")
Expand All @@ -40,14 +41,15 @@ func main() {
if err != nil {
panic(err)
}
jsonFile, err := json.MarshalIndent(p.F.ELFBin64, "", " ")
jsonFile, err := p.DumpJSON()
if err != nil {
panic(err)
}
fmt.Println(string(jsonFile))
fmt.Println(jsonFile)
}



```

## Docs & API
Expand Down
5 changes: 2 additions & 3 deletions cmd/elfdump.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"fmt"

"github.com/saferwall/elf"
Expand All @@ -18,9 +17,9 @@ func main() {
if err != nil {
panic(err)
}
jsonFile, err := json.MarshalIndent(p.F.ELFBin64, "", " ")
jsonFile, err := p.DumpJSON()
if err != nil {
panic(err)
}
fmt.Println(string(jsonFile))
fmt.Println(jsonFile)
}
17 changes: 11 additions & 6 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,21 @@ type Symbol struct {
Library string `json:"symbol_library"`
}

// ELFSymbols represents all symbol data.
type ELFSymbols struct {
NamedSymbols []Symbol `json:",omitempty"`
GNUVersion []GNUVersion `json:",omitempty"`
GNUVersionSym []byte `json:",omitempty"`
}

// File is an in-memory iterable representation of a raw elf binary.
// this is merely used to ease the use of the package as a library
// and allow feature modification and rebuilding of ELF files.
type File struct {
FileHeader
ELFBin32
ELFBin64
NamedSymbols []Symbol
GNUVersion []GNUVersion
GNUVersionSym []byte
FileHeader `json:",omitempty"`
ELFBin32 `json:",omitempty"`
ELFBin64 `json:",omitempty"`
ELFSymbols `json:",omitempty"`
}

func NewBinaryFile() *File {
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module github.com/saferwall/elf
go 1.15

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/saferwall/binstream v0.1.1
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -9,9 +10,11 @@ github.com/saferwall/binstream v0.1.1/go.mod h1:RRSF+ePir1XKbQF4BlnShbs6u1PI0io9
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d h1:jbzgAvDZn8aEnytae+4ou0J0GwFZoHR0hOrTg4qH8GA=
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549 h1:OL5GcZ2XPkte3dpfuFQ9o884vrE3BZQhajdntNMruv4=
golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
53 changes: 53 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
package elf

import (
"encoding/json"
"errors"
"strings"
)

// DumpJSON marshals the entire binary representation into JSON Format.
func (p *Parser) DumpJSON() (string, error) {

var jsonOutput strings.Builder

switch p.F.Class() {
case ELFCLASS32:
jsonBin, err := json.MarshalIndent(p.F.ELFBin32, "", " ")
if err != nil {
return "", err
}
jsonSymbols, err := json.MarshalIndent(p.F.ELFSymbols, "", " ")
if err != nil {
return "", err
}
_, err = jsonOutput.Write(jsonBin)
if err != nil {
return "", err
}
_, err = jsonOutput.Write(jsonSymbols)
if err != nil {
return "", err
}
return jsonOutput.String(), nil
case ELFCLASS64:
jsonBin, err := json.MarshalIndent(p.F.ELFBin64, "", " ")
if err != nil {
return "", err
}
jsonSymbols, err := json.MarshalIndent(p.F.ELFSymbols, "", " ")
if err != nil {
return "", err
}
_, err = jsonOutput.Write(jsonBin)
if err != nil {
return "", err
}
_, err = jsonOutput.Write(jsonSymbols)
if err != nil {
return "", err
}
return jsonOutput.String(), nil
default:
return "", errors.New("unsupported ELF Class")
}
}
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func (p *Parser) getSymbols64(typ SectionType) ([]Symbol, []byte, error) {
i++
}
err = p.ParseGNUVersionTable(strdata)
if err != nil {
if err == nil {
for i := range namedSymbols {
namedSymbols[i].Library, namedSymbols[i].Version = p.gnuVersion(i)
}
Expand Down

0 comments on commit da2a45f

Please sign in to comment.