Skip to content

Commit

Permalink
adding final tweaks to parsing
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Sep 30, 2021
1 parent a79c0ed commit e8f6648
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
54 changes: 44 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This is mostly for fun - I wanted to see how hard it would be to parse a binary
with Go. Right now we use the same logic as objdump to load it, and then print
Symbols (and I found an entry to where the Dwarf is).

![img/smeagle.gif](img/smeagle.gif)

🚧️ **under development** 🚧️

## Usage
Expand All @@ -15,15 +17,55 @@ $ make
$ go run main.go parse libtest.so
```
```
{"library":"libtest.so","functions":[{"name":"__printf_chk"},{"parameters":[{"name":"a","type":"long int","sizes":8},{"name":"b","type":"long int","sizes":8},{"name":"c","type":"long int","sizes":8},{"name":"d","type":"long int","sizes":8},{"name":"e","type":"long int","sizes":8},{"name":"f","type":"__int128","sizes":16}],"name":"bigcall"}]}
{"library":"libtest.so","functions":[{"name":"__printf_chk","type":"Function"},{"parameters":[{"type":"long int","size":8},{"type":"long int","size":8},{"type":"long int","size":8},{"type":"long int","size":8},{"type":"long int","size":8},{"type":"__int128","size":16}],"name":"bigcall","type":"Function"}]}
```

or print pretty:

```
$ go run main.go parse libtest.so --pretty
```

```
{
"library": "libtest.so",
"functions": [
{
"name": "__printf_chk",
"type": "Function"
},
{
"parameters": [
{
"type": "long int",
"size": 8
},
{
"type": "long int",
"size": 8
},
{
"type": "long int",
"size": 8
},
{
"type": "long int",
"size": 8
},
{
"type": "long int",
"size": 8
},
{
"type": "__int128",
"size": 16
}
],
"name": "bigcall",
"type": "Function"
}
]
}
```
Note that this library is under development, so stay tuned!

## Background
Expand All @@ -46,11 +88,3 @@ d.StructCache[t.StructName] = t
```

And then used in [parsers/x86_64/parse.go](parsers/x86_64/parse.go) to match a typedef (which only has name and type string) to a fully parsed struct (a struct, union, or class).


## TODO

- add variable parsing
- add allocator to get allocations
- need to get registers / locations for each type

Binary file added img/smeagle.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 9 additions & 13 deletions parsers/x86_64/parse.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package x86_64

import (
"github.com/vsoch/gosmeagle/descriptor"
"github.com/vsoch/gosmeagle/parsers/file"
"github.com/vsoch/gosmeagle/pkg/debug/dwarf"
"log"
"reflect"
"strings"

"github.com/vsoch/gosmeagle/descriptor"
"github.com/vsoch/gosmeagle/parsers/file"
"github.com/vsoch/gosmeagle/pkg/debug/dwarf"
)

// ParseFunction parses a function parameters
Expand Down Expand Up @@ -64,10 +65,9 @@ func ParseParameter(c file.Component, d *dwarf.Data, symbol file.Symbol, indirec
return ParseStructure(convert, d, symbol, indirections, seen, a)
case "Array":
return ParseArray(c, d, symbol, indirections, seen, a)
case "Function":
// TODO need to debug if this should be here
return descriptor.BasicParameter{}
case "", "Undefined":

// A nested function here appears to be anonymous (e.g., { Function -1 func(*char) void})
case "", "Undefined", "Function":
return nil
default:
log.Fatalf("Unparsed parameter class", c.Class)
Expand Down Expand Up @@ -172,12 +172,8 @@ func ParseArray(c file.Component, d *dwarf.Data, symbol file.Symbol, indirection
arrayClass := ClassifyArray(convert, &seenComponent, indirections)
loc := a.GetRegisterString(arrayClass.Lo, arrayClass.Hi, seenComponent.Size, seenComponent.Class)

// TODO need to debug why this can be nil (shouldn't be)
if underlyingType != nil {
return descriptor.ArrayParameter{Length: convert.Count, Name: convert.CommonType.Name, Type: convert.Type.String(),
Size: convert.Count * underlyingType.GetSize(), Class: "Array", ItemType: underlyingType, Location: loc}
}
return descriptor.ArrayParameter{}
return descriptor.ArrayParameter{Length: convert.Count, Name: convert.CommonType.Name, Type: convert.Type.String(),
Size: convert.Count * seenComponent.Size, Class: "Array", ItemType: underlyingType, Location: loc}
}

// ParseStructure parses a structure type
Expand Down

0 comments on commit e8f6648

Please sign in to comment.