Skip to content

Commit

Permalink
internal/symtab: improve handling of address-less symbols
Browse files Browse the repository at this point in the history
The symtab package had ad hoc handling of symbols without addresses
from a while ago. The object loaders now report whether a symbol has a
meaningful address or not. Dramatically simplify the address lookup
logic.
  • Loading branch information
aclements committed Jul 1, 2020
1 parent 8277c4b commit 3450f1d
Showing 1 changed file with 5 additions and 32 deletions.
37 changes: 5 additions & 32 deletions internal/symtab/symtab.go
Expand Up @@ -25,29 +25,15 @@ func NewTable(symbols obj.Symbols) *Table {
}

// Put syms in address order for fast address lookup.
addr := make([]obj.SymID, len(syms))
for i := range addr {
addr[i] = obj.SymID(i)
var addr []obj.SymID
for i := range syms {
if syms[i].HasAddr {
addr = append(addr, obj.SymID(i))
}
}
sort.Slice(addr, func(i, j int) bool {
si, sj := &syms[addr[i]], &syms[addr[j]]

// Put undefined symbols before defined symbols so we
// can trim them off.
//
// TODO: Strip using HasAddr and remove that check in
// Addr.
cati, catj := 0, 0
if si.Kind == obj.SymUndef {
cati = -1
}
if sj.Kind == obj.SymUndef {
catj = -1
}
if cati != catj {
return cati < catj
}

// Sort by symbol address.
vi, vj := si.Value, sj.Value
if vi != vj {
Expand All @@ -58,11 +44,6 @@ func NewTable(symbols obj.Symbols) *Table {
return si.Name < sj.Name
})

// Trim undefined symbols.
for len(addr) > 0 && syms[addr[0]].Kind == obj.SymUndef {
addr = addr[1:]
}

// Create name map for fast name lookup.
name := make(map[string]obj.SymID)
for i, s := range syms {
Expand Down Expand Up @@ -103,14 +84,6 @@ func (t *Table) Addr(addr uint64) (obj.SymID, bool) {
if sym.Value > addr {
break
}
if !sym.HasAddr {
// This symbol's value isn't an address. For
// example, this is an absolute symbol or a
// TLS symbol.
//
// TODO: Strip these in NewTable.
continue
}
if best == -1 && addr < sym.Value+sym.Size {
best = i + j
}
Expand Down

0 comments on commit 3450f1d

Please sign in to comment.