mv x/<tool> => xtool/<tool>

This commit is contained in:
xushiwei
2024-05-11 05:27:38 +08:00
parent 67896c63a7
commit cd266213ce
25 changed files with 13 additions and 13 deletions

119
xtool/nm/index.go Normal file
View File

@@ -0,0 +1,119 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nm
import (
"bytes"
"crypto/md5"
"encoding/base64"
"log"
"os"
"path/filepath"
"strings"
)
type IndexBuilder struct {
nm *Cmd
}
func NewIndexBuilder(nm *Cmd) *IndexBuilder {
return &IndexBuilder{nm}
}
func (p *IndexBuilder) Index(fromDir []string, toDir string, progress func(path string)) error {
for _, dir := range fromDir {
if dir == "" {
continue
}
if e := p.IndexDir(dir, toDir, progress); e != nil {
if !os.IsNotExist(e) {
log.Println(e)
}
}
}
return nil
}
func (p *IndexBuilder) IndexDir(fromDir, toDir string, progress func(path string)) error {
if abs, e := filepath.Abs(fromDir); e == nil {
fromDir = abs
}
return filepath.WalkDir(fromDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
fname := d.Name()
switch filepath.Ext(fname) {
case ".a", ".dylib", ".tbd", ".so", ".dll", ".lib":
progress(path)
hash := md5.Sum([]byte(path))
hashStr := base64.RawURLEncoding.EncodeToString(hash[:])
outFile := filepath.Join(toDir, strings.TrimPrefix(fname, "lib")+hashStr+".pub")
e := p.IndexFile(path, outFile)
if e != nil {
log.Println(e)
}
}
return nil
})
}
func (p *IndexBuilder) IndexFile(arFile, outFile string) (err error) {
items, err := p.nm.List(arFile)
if err != nil {
if len(items) == 0 {
return
}
}
var b bytes.Buffer
b.WriteString("nm ")
b.WriteString(arFile)
b.WriteByte('\n')
nbase := b.Len()
for _, item := range items {
if item.File != "" {
b.WriteString("file ")
b.WriteString(item.File)
b.WriteByte('\n')
}
for _, sym := range item.Symbols {
switch sym.Type {
case Text, Data, BSS, Rodata, 'S', 'C', 'W', 'A':
b.WriteByte(byte(sym.Type))
b.WriteByte(' ')
b.WriteString(sym.Name)
b.WriteByte('\n')
case Undefined, LocalText, LocalData, LocalBSS, LocalASym, 'I', 'i', 'a', 'w':
/*
if sym.Type != Undefined && strings.Contains(sym.Name, "fprintf") {
log.Printf("skip symbol type %c: %s\n", sym.Type, sym.Name)
}
*/
default:
log.Printf("unknown symbol type %c: %s\n", sym.Type, sym.Name)
}
}
}
buf := b.Bytes()
if len(buf) <= nbase {
return
}
return os.WriteFile(outFile, buf, 0666)
}

208
xtool/nm/nm.go Normal file
View File

@@ -0,0 +1,208 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nm
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
var (
errInvalidOutput = errors.New("invalid nm output")
)
// -----------------------------------------------------------------------------
// Cmd represents a nm command.
type Cmd struct {
app string
}
// New creates a new nm command.
func New(app string) *Cmd {
if app == "" {
app = "nm"
}
return &Cmd{app}
}
// -----------------------------------------------------------------------------
// SymbolType represents a symbol type.
type SymbolType uint8
const (
Undefined = SymbolType('U') // Undefined
Text = SymbolType('T') // Text (code) section symbol
Data = SymbolType('D') // Data (global var) section symbol
Rodata = SymbolType('R') // Read-only data (rodata) section symbol
BSS = SymbolType('B') // BSS (uninitialized global var) section symbol
LocalText = SymbolType('t') // Local text (code) section symbol
LocalData = SymbolType('d') // Local data (local var) section symbol
LocalBSS = SymbolType('b') // Local BSS (uninitialized local var) section symbol
LocalASym = SymbolType('s') // Local symbol in an assembler source file
)
// Symbol represents a symbol in an object file.
type Symbol struct {
Name string // symbol name
Addr uint64 // symbol address
Type SymbolType // symbol type
FAddr bool // address is valid
}
// ObjectFile represents an object file.
type ObjectFile struct {
File string // file name
Symbols []*Symbol // symbols
}
// List lists symbols in an archive file.
func (p *Cmd) List(arfile string) (items []*ObjectFile, err error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command(p.app, arfile)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
e := cmd.Run()
if stderr.Len() > 0 {
listError(stderr.Bytes())
}
items, err = listOutput(stdout.Bytes())
if err == nil {
err = e
}
return
}
func listError(data []byte) {
sep := []byte{'\n'}
nosym := []byte(": no symbols")
lines := bytes.Split(data, sep)
for _, line := range lines {
if len(line) == 0 || bytes.HasSuffix(line, nosym) {
continue
}
os.Stderr.Write(line)
os.Stderr.Write(sep)
}
}
func listOutput(data []byte) (items []*ObjectFile, err error) {
sep := []byte{'\n'}
item := &ObjectFile{}
lines := bytes.Split(data, sep)
for _, line := range lines {
if len(line) == 0 {
if item.File == "" && len(item.Symbols) > 0 {
items = append(items, item)
}
item = nil
continue
}
if item == nil {
s := string(line)
if strings.HasSuffix(s, ":") {
item = &ObjectFile{File: s[:len(s)-1]}
items = append(items, item)
continue
}
err = errInvalidOutput
return
}
if len(line) < 10 {
err = errInvalidOutput
return
}
var sym *Symbol
if is64bits(line) {
sym = &Symbol{
Name: string(line[19:]),
Type: SymbolType(line[17]),
}
if sym.FAddr = hasAddr(line); sym.FAddr {
sym.Addr = hexUint64(line)
}
} else {
sym = &Symbol{
Name: string(line[11:]),
Type: SymbolType(line[9]),
}
if sym.FAddr = hasAddr(line); sym.FAddr {
sym.Addr = uint64(hexUint32(line))
}
}
item.Symbols = append(item.Symbols, sym)
}
return
}
func hasAddr(line []byte) bool {
c := line[0]
return c != ' ' && c != '-'
}
func is64bits(line []byte) bool {
if line[0] != ' ' {
return line[8] != ' '
}
return line[9] == ' '
}
func hexUint64(b []byte) uint64 {
defer func() {
if e := recover(); e != nil {
fmt.Fprintln(os.Stderr, "-->", string(b))
panic(e)
}
}()
_ = b[15] // bounds check hint to compiler; see golang.org/issue/14808
return hex(b[15]) | hex(b[14])<<4 | hex(b[13])<<8 | hex(b[12])<<12 |
hex(b[11])<<16 | hex(b[10])<<20 | hex(b[9])<<24 | hex(b[8])<<28 |
hex(b[7])<<32 | hex(b[6])<<36 | hex(b[5])<<40 | hex(b[4])<<44 |
hex(b[3])<<48 | hex(b[2])<<52 | hex(b[1])<<56 | hex(b[0])<<60
}
func hexUint32(b []byte) uint64 {
defer func() {
if e := recover(); e != nil {
fmt.Fprintln(os.Stderr, "-->", string(b))
panic(e)
}
}()
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
return hex(b[7]) | hex(b[6])<<4 | hex(b[5])<<8 | hex(b[4])<<12 |
hex(b[3])<<16 | hex(b[2])<<20 | hex(b[1])<<24 | hex(b[0])<<28
}
func hex(b byte) uint64 {
return hexTable[b]
}
var hexTable = []uint64{
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
'5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15,
'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15,
}
// -----------------------------------------------------------------------------

129
xtool/nm/query.go Normal file
View File

@@ -0,0 +1,129 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nm
import (
"bufio"
"os"
"strings"
)
// MatchedItem represents a matched item
type MatchedItem struct {
ObjFile string
Symbol string
Type SymbolType
}
// MatchedFile represents a matched file
type MatchedFile struct {
ArFile string
Items []*MatchedItem
}
// Query queries symbol in index files (allow wildcard).
func Query(dir string, query string) (files []*MatchedFile, err error) {
fis, err := os.ReadDir(dir)
if err != nil {
return
}
dir += "/"
for _, fi := range fis {
if fi.IsDir() {
continue
}
idxFile := fi.Name()
if !strings.HasSuffix(idxFile, ".pub") {
continue
}
files = queryIndex(files, dir+fi.Name(), query)
}
return
}
func queryIndex(files []*MatchedFile, idxFile, query string) []*MatchedFile {
f, err := os.Open(idxFile)
if err != nil {
return files
}
defer f.Close()
r := bufio.NewReader(f)
line, err := r.ReadString('\n')
if err != nil || !strings.HasPrefix(line, "nm ") {
return files
}
var items []*MatchedItem
arFile := line[3 : len(line)-1]
objFile := ""
query, flags := parseQuery(query)
for {
line, err = r.ReadString('\n')
if err != nil {
break
}
if strings.HasPrefix(line, "file ") {
objFile = line[5 : len(line)-1]
continue
}
typ := line[0]
sym := line[2 : len(line)-1]
if !match(sym, query, flags) {
continue
}
items = append(items, &MatchedItem{
ObjFile: objFile,
Symbol: sym,
Type: SymbolType(typ),
})
}
if len(items) > 0 {
files = append(files, &MatchedFile{ArFile: arFile, Items: items})
}
return files
}
const (
flagSuffix = 1 << iota
flagPrefix
)
func parseQuery(query string) (text string, flags int) {
if strings.HasSuffix(query, "*") {
query = query[:len(query)-1]
flags = flagPrefix
}
if strings.HasPrefix(query, "*") {
query = query[1:]
flags |= flagSuffix
}
text = query
return
}
func match(s, query string, flags int) bool {
switch flags {
case 0:
return s == query
case flagPrefix:
return strings.HasPrefix(s, query)
case flagSuffix:
return strings.HasSuffix(s, query)
default:
return strings.Contains(s, query)
}
}