Skip to content

Commit

Permalink
parsing call functions to derive import/export
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 Oct 20, 2021
1 parent ea3bfd2 commit 6cc5a2d
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 84 deletions.
23 changes: 15 additions & 8 deletions corpus/corpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,18 @@ func (c *Corpus) Parse(f *file.File) {

switch symbol.GetType() {
case "STT_FUNC":
entry, ok := lookup["functions"][symbol.GetName()]
if !ok {
continue

// First look if it's a call site
entry, ok := lookup["calls"][symbol.GetName()]
if ok {
c.parseFunction(f, symbol, &entry, true)
} else {
entry, ok := lookup["functions"][symbol.GetName()]
if !ok {
continue
}
c.parseFunction(f, symbol, &entry, false)
}
c.parseFunction(f, symbol, &entry)

case "STT_OBJECT":

Expand All @@ -123,11 +130,11 @@ func (c *Corpus) Parse(f *file.File) {
}

// parse a dynamic function symbol
func (c *Corpus) parseFunction(f *file.File, symbol file.Symbol, entry *file.DwarfEntry) {
func (c *Corpus) parseFunction(f *file.File, symbol file.Symbol, entry *file.DwarfEntry, isCallSite bool) {

switch f.GoArch() {
case "amd64":
newFunction := x86_64.ParseFunction(f, symbol, entry, c.Disasm)
newFunction := x86_64.ParseFunction(f, symbol, entry, c.Disasm, isCallSite)
loc := map[string]descriptor.LocationDescription{}
loc["function"] = newFunction
c.Locations = append(c.Locations, loc)
Expand All @@ -141,8 +148,8 @@ func (c *Corpus) parseVariable(f *file.File, symbol file.Symbol, entry *file.Dwa

switch f.GoArch() {
case "amd64":
// Don't allow variables without name or type
variable := x86_64.ParseVariable(f, symbol, entry)
// Don't allow variables without name or type (variables cannot be call sites)
variable := x86_64.ParseVariable(f, symbol, entry, false)
if !reflect.DeepEqual(variable, descriptor.VariableDescription{}) {
loc := map[string]descriptor.LocationDescription{}
loc["variable"] = variable
Expand Down
61 changes: 38 additions & 23 deletions descriptor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Parameter interface {
GetName() string
GetLocation() string
GetType() string
GetDirection() string
}

// A General Location description holds a variable or function
Expand All @@ -18,6 +19,7 @@ type LocationDescription interface{}
type FunctionDescription struct {
Parameters []Parameter `json:"parameters,omitempty"`
Name string `json:"name"`
Direction string `json:"direction,omitempty"`
Type string `json:"type"`
}

Expand Down Expand Up @@ -71,13 +73,22 @@ func (f QualifiedParameter) GetType() string { return f.Type }
func (f BasicParameter) GetType() string { return f.Type }
func (f EnumParameter) GetType() string { return f.Type }

func (f FunctionParameter) GetDirection() string { return f.Direction }
func (f StructureParameter) GetDirection() string { return f.Direction }
func (f PointerParameter) GetDirection() string { return f.Direction }
func (f ArrayParameter) GetDirection() string { return f.Direction }
func (f QualifiedParameter) GetDirection() string { return f.Direction }
func (f BasicParameter) GetDirection() string { return f.Direction }
func (f EnumParameter) GetDirection() string { return f.Direction }

type StructureParameter struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Class string `json:"class,omitempty"`
Size int64 `json:"size,omitempty"`
Location string `json:"location,omitempty"`
Fields []Parameter `json:"fields,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Class string `json:"class,omitempty"`
Size int64 `json:"size,omitempty"`
Direction string `json:"direction,omitempty"`
Location string `json:"location,omitempty"`
Fields []Parameter `json:"fields,omitempty"`
}

type PointerParameter struct {
Expand All @@ -92,13 +103,14 @@ type PointerParameter struct {
}

type ArrayParameter struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Class string `json:"class,omitempty"`
Size int64 `json:"size,omitempty"`
Length int64 `json:"count,omitempty"`
Location string `json:"location,omitempty"`
ItemType Parameter `json:"items_type,omitemtpy"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Class string `json:"class,omitempty"`
Size int64 `json:"size,omitempty"`
Length int64 `json:"count,omitempty"`
Location string `json:"location,omitempty"`
Direction string `json:"direction,omitempty"`
ItemType Parameter `json:"items_type,omitemtpy"`
}

type EnumParameter struct {
Expand All @@ -108,24 +120,27 @@ type EnumParameter struct {
Size int64 `json:"size,omitempty"`
Location string `json:"location,omitempty"`
Length int `json:"count,omitempty"`
Direction string `json:"direction,omitempty"`
Constants map[string]int64 `json:"constants,omitemtpy"`
}

// QualifiedParameter and BasicParameter are the same, but we are modeling after debug/dwarf
type QualifiedParameter struct {
Name string `json:"name,omitempty"`
Class string `json:"class,omitempty"`
Type string `json:"type,omitempty"`
Location string `json:"location,omitempty"`
Size int64 `json:"size,omitempty"`
Name string `json:"name,omitempty"`
Class string `json:"class,omitempty"`
Type string `json:"type,omitempty"`
Direction string `json:"direction,omitempty"`
Location string `json:"location,omitempty"`
Size int64 `json:"size,omitempty"`
}

type BasicParameter struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Class string `json:"class,omitempty"`
Location string `json:"location,omitempty"`
Size int64 `json:"size,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Class string `json:"class,omitempty"`
Location string `json:"location,omitempty"`
Direction string `json:"direction,omitempty"`
Size int64 `json:"size,omitempty"`
}

// A Variable description is general and can also describe an underlying type
Expand Down
3 changes: 1 addition & 2 deletions parsers/file/dwarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ func (f *FunctionEntry) GetComponents() []Component {

}

// Get the Return value - the "type" of
// TODO can we use := entry.Val(dwarf.AttrVarParam)
// Get the Return value - for a library this is the only export (unless a call site)
returnType, err := GetUnderlyingType(f.Entry, f.Data)
if returnType != nil && err != nil {
comps = append(comps, Component{Name: "return", Type: returnType.Common().Name,
Expand Down

0 comments on commit 6cc5a2d

Please sign in to comment.