2024-09-25 14:59:02 +08:00
|
|
|
package symbol
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2024-10-08 11:17:33 +08:00
|
|
|
"fmt"
|
2024-09-25 14:59:02 +08:00
|
|
|
"os"
|
2024-10-08 11:17:33 +08:00
|
|
|
"path/filepath"
|
2024-10-15 23:08:20 +08:00
|
|
|
"runtime"
|
2024-09-25 14:59:02 +08:00
|
|
|
"strings"
|
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
|
|
"github.com/goplus/llgo/c"
|
|
|
|
|
"github.com/goplus/llgo/c/cjson"
|
|
|
|
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/config"
|
|
|
|
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/parse"
|
|
|
|
|
"github.com/goplus/llgo/chore/llcppg/types"
|
|
|
|
|
"github.com/goplus/llgo/xtool/nm"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-17 18:54:28 +08:00
|
|
|
type dbgFlags = int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
DbgConf dbgFlags = 1 << iota
|
|
|
|
|
DbgSymbol
|
|
|
|
|
|
|
|
|
|
DbgFlagAll = DbgConf | DbgSymbol
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
debugConf bool
|
|
|
|
|
debugSymbol bool
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func SetDebug(dbgFlags dbgFlags) {
|
|
|
|
|
debugConf = (dbgFlags & DbgConf) != 0
|
|
|
|
|
debugSymbol = (dbgFlags & DbgSymbol) != 0
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-15 23:08:20 +08:00
|
|
|
type LibConfig struct {
|
|
|
|
|
Paths []string
|
|
|
|
|
Names []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParseLibConfig(lib string) *LibConfig {
|
2024-10-08 11:17:33 +08:00
|
|
|
parts := strings.Fields(lib)
|
2024-10-15 23:08:20 +08:00
|
|
|
config := &LibConfig{}
|
2024-10-08 11:17:33 +08:00
|
|
|
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
if strings.HasPrefix(part, "-L") {
|
2024-10-15 23:08:20 +08:00
|
|
|
config.Paths = append(config.Paths, part[2:])
|
2024-10-08 11:17:33 +08:00
|
|
|
} else if strings.HasPrefix(part, "-l") {
|
2024-10-15 23:08:20 +08:00
|
|
|
config.Names = append(config.Names, part[2:])
|
2024-10-08 11:17:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-15 23:08:20 +08:00
|
|
|
return config
|
|
|
|
|
}
|
2024-10-08 11:17:33 +08:00
|
|
|
|
2024-10-15 23:08:20 +08:00
|
|
|
func GenDylibPaths(config *LibConfig, defaultPaths []string) ([]string, error) {
|
|
|
|
|
var foundPaths []string
|
|
|
|
|
var notFound []string
|
|
|
|
|
affix := ".dylib"
|
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
|
affix = ".so"
|
|
|
|
|
}
|
|
|
|
|
searchPaths := append(config.Paths, defaultPaths...)
|
|
|
|
|
for _, name := range config.Names {
|
|
|
|
|
var foundPath string
|
|
|
|
|
for _, path := range searchPaths {
|
|
|
|
|
dylibPath := filepath.Join(path, "lib"+name+affix)
|
|
|
|
|
if _, err := os.Stat(dylibPath); err == nil {
|
|
|
|
|
foundPath = dylibPath
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if foundPath != "" {
|
|
|
|
|
foundPaths = append(foundPaths, foundPath)
|
|
|
|
|
} else {
|
|
|
|
|
notFound = append(notFound, name)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-17 18:54:28 +08:00
|
|
|
if len(notFound) > 0 && debugSymbol {
|
2024-10-15 23:08:20 +08:00
|
|
|
fmt.Printf("Warning: Some libraries were not found: %s\n", strings.Join(notFound, ", "))
|
|
|
|
|
}
|
|
|
|
|
if len(foundPaths) == 0 {
|
|
|
|
|
return nil, fmt.Errorf("failed to find any libraries")
|
|
|
|
|
}
|
|
|
|
|
return foundPaths, nil
|
2024-10-08 11:17:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ParseDylibSymbols parses symbols from dynamic libraries specified in the lib string.
|
|
|
|
|
// It handles multiple libraries (e.g., -L/opt/homebrew/lib -llua -lm) and returns
|
|
|
|
|
// symbols if at least one library is successfully parsed. Errors from inaccessible
|
|
|
|
|
// libraries (like standard libs) are logged as warnings.
|
|
|
|
|
//
|
|
|
|
|
// Returns symbols and nil error if any symbols are found, or nil and error if none found.
|
|
|
|
|
func ParseDylibSymbols(lib string) ([]*nm.Symbol, error) {
|
2024-10-17 18:54:28 +08:00
|
|
|
if debugConf {
|
|
|
|
|
fmt.Println("ParseDylibSymbols:from", lib)
|
|
|
|
|
}
|
2024-10-15 23:08:20 +08:00
|
|
|
conf := ParseLibConfig(lib)
|
2024-10-17 18:54:28 +08:00
|
|
|
if debugConf {
|
|
|
|
|
fmt.Println("ParseDylibSymbols:LibConfig Parse To")
|
|
|
|
|
fmt.Println("conf.Names: ", conf.Names)
|
|
|
|
|
fmt.Println("conf.Paths: ", conf.Paths)
|
|
|
|
|
}
|
2024-10-16 15:08:17 +08:00
|
|
|
defaultPaths := getSysLibPaths()
|
|
|
|
|
dylibPaths, err := GenDylibPaths(conf, defaultPaths)
|
2024-10-08 11:17:33 +08:00
|
|
|
if err != nil {
|
2024-10-17 18:54:28 +08:00
|
|
|
return nil, fmt.Errorf("failed to generate some dylib paths: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if debugSymbol {
|
|
|
|
|
fmt.Println("ParseDylibSymbols:dylibPaths", dylibPaths)
|
2024-10-08 11:17:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var symbols []*nm.Symbol
|
|
|
|
|
var parseErrors []string
|
|
|
|
|
|
|
|
|
|
for _, dylibPath := range dylibPaths {
|
|
|
|
|
if _, err := os.Stat(dylibPath); err != nil {
|
2024-10-17 18:54:28 +08:00
|
|
|
if debugSymbol {
|
|
|
|
|
fmt.Printf("ParseDylibSymbols:Failed to access dylib %s: %v\n", dylibPath, err)
|
|
|
|
|
}
|
2024-10-08 11:17:33 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
files, err := nm.New("").List(dylibPath)
|
|
|
|
|
if err != nil {
|
2024-10-17 18:54:28 +08:00
|
|
|
parseErrors = append(parseErrors, fmt.Sprintf("ParseDylibSymbols:Failed to list symbols in dylib %s: %v", dylibPath, err))
|
2024-10-08 11:17:33 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
symbols = append(symbols, file.Symbols...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(symbols) > 0 {
|
2024-10-17 18:54:28 +08:00
|
|
|
if debugSymbol {
|
|
|
|
|
if len(parseErrors) > 0 {
|
|
|
|
|
fmt.Printf("ParseDylibSymbols:Some libraries could not be parsed: %v\n", parseErrors)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("ParseDylibSymbols:", len(symbols), "symbols")
|
2024-10-08 11:17:33 +08:00
|
|
|
}
|
|
|
|
|
return symbols, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("no symbols found in any dylib. Errors: %v", parseErrors)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-16 15:08:17 +08:00
|
|
|
func getSysLibPaths() []string {
|
|
|
|
|
var paths []string
|
|
|
|
|
if runtime.GOOS == "linux" {
|
2024-10-17 18:54:28 +08:00
|
|
|
if debugConf {
|
|
|
|
|
fmt.Println("getSysLibPaths:find sys lib path from linux")
|
|
|
|
|
}
|
2024-10-16 15:08:17 +08:00
|
|
|
paths = []string{
|
|
|
|
|
"/usr/lib",
|
|
|
|
|
"/usr/local/lib",
|
|
|
|
|
}
|
|
|
|
|
paths = append(paths, getPath("/etc/ld.so.conf")...)
|
2024-10-17 18:54:28 +08:00
|
|
|
if debugConf && len(paths) == 0 {
|
|
|
|
|
fmt.Println("getSysLibPaths:/etc/ld.so.conf havent find any path")
|
|
|
|
|
}
|
2024-10-16 15:08:17 +08:00
|
|
|
confd := "/etc/ld.so.conf.d"
|
2024-10-17 18:54:28 +08:00
|
|
|
dir, err := os.Stat(confd)
|
|
|
|
|
if err != nil || !dir.IsDir() {
|
|
|
|
|
if debugConf {
|
|
|
|
|
fmt.Println("getSysLibPaths:/etc/ld.so.conf.d not found or not dir")
|
|
|
|
|
}
|
|
|
|
|
return paths
|
2024-10-16 15:08:17 +08:00
|
|
|
}
|
2024-10-17 18:54:28 +08:00
|
|
|
// todo(zzy) : wait llgo os.ReadDir support
|
|
|
|
|
// files, err := os.ReadDir(confd)
|
|
|
|
|
// if err == nil {
|
|
|
|
|
// for _, file := range files {
|
|
|
|
|
// filepath := filepath.Join(confd, file.Name())
|
|
|
|
|
// paths = append(paths, getPath(filepath)...)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-10-16 15:08:17 +08:00
|
|
|
}
|
|
|
|
|
return paths
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getPath(file string) []string {
|
2024-10-17 18:54:28 +08:00
|
|
|
if debugConf {
|
|
|
|
|
fmt.Println("getPath:from", file)
|
|
|
|
|
}
|
2024-10-16 15:08:17 +08:00
|
|
|
var paths []string
|
|
|
|
|
content, err := os.ReadFile(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return paths
|
|
|
|
|
}
|
|
|
|
|
lines := strings.Split(string(content), "\n")
|
|
|
|
|
for _, line := range lines {
|
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
if line != "" && !strings.HasPrefix(line, "#") {
|
|
|
|
|
if file, err := os.Stat(line); err == nil && file.IsDir() {
|
|
|
|
|
paths = append(paths, line)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return paths
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 14:59:02 +08:00
|
|
|
// finds the intersection of symbols from the dynamic library's symbol table and the symbols parsed from header files.
|
|
|
|
|
// It returns a list of symbols that can be externally linked.
|
|
|
|
|
func GetCommonSymbols(dylibSymbols []*nm.Symbol, headerSymbols map[string]*parse.SymbolInfo) []*types.SymbolInfo {
|
|
|
|
|
var commonSymbols []*types.SymbolInfo
|
|
|
|
|
for _, dylibSym := range dylibSymbols {
|
2024-10-10 12:21:20 +08:00
|
|
|
symName := strings.TrimLeft(dylibSym.Name, "_")
|
2024-09-25 14:59:02 +08:00
|
|
|
if symInfo, ok := headerSymbols[symName]; ok {
|
|
|
|
|
symbolInfo := &types.SymbolInfo{
|
|
|
|
|
Mangle: symName,
|
|
|
|
|
CPP: symInfo.ProtoName,
|
|
|
|
|
Go: symInfo.GoName,
|
|
|
|
|
}
|
|
|
|
|
commonSymbols = append(commonSymbols, symbolInfo)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return commonSymbols
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ReadExistingSymbolTable(fileName string) (map[string]types.SymbolInfo, bool) {
|
|
|
|
|
if _, err := os.Stat(fileName); err != nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := os.ReadFile(fileName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parsedJSON := cjson.ParseBytes(data)
|
|
|
|
|
if parsedJSON == nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
existingSymbols := make(map[string]types.SymbolInfo)
|
|
|
|
|
arraySize := parsedJSON.GetArraySize()
|
|
|
|
|
|
|
|
|
|
for i := 0; i < int(arraySize); i++ {
|
|
|
|
|
item := parsedJSON.GetArrayItem(c.Int(i))
|
|
|
|
|
symbol := types.SymbolInfo{
|
|
|
|
|
Mangle: config.GetStringItem(item, "mangle", ""),
|
|
|
|
|
CPP: config.GetStringItem(item, "c++", ""),
|
|
|
|
|
Go: config.GetStringItem(item, "go", ""),
|
|
|
|
|
}
|
|
|
|
|
existingSymbols[symbol.Mangle] = symbol
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return existingSymbols, true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GenSymbolTableData(commonSymbols []*types.SymbolInfo, existingSymbols map[string]types.SymbolInfo) ([]byte, error) {
|
2024-10-17 18:54:28 +08:00
|
|
|
if len(existingSymbols) > 0 {
|
|
|
|
|
if debugSymbol {
|
|
|
|
|
fmt.Println("GenSymbolTableData:generate symbol table with exist symbol table")
|
|
|
|
|
}
|
|
|
|
|
for i := range commonSymbols {
|
|
|
|
|
if existingSymbol, exists := existingSymbols[commonSymbols[i].Mangle]; exists && commonSymbols[i].Go != existingSymbol.Go {
|
|
|
|
|
if debugSymbol {
|
|
|
|
|
fmt.Println("symbol", commonSymbols[i].Mangle, "already exist, use exist symbol", existingSymbol.Go)
|
|
|
|
|
}
|
|
|
|
|
commonSymbols[i].Go = existingSymbol.Go
|
|
|
|
|
} else {
|
|
|
|
|
if debugSymbol {
|
|
|
|
|
fmt.Println("new symbol", commonSymbols[i].Mangle, "-", commonSymbols[i].CPP, "-", commonSymbols[i].Go)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if debugSymbol {
|
|
|
|
|
fmt.Println("GenSymbolTableData:generate symbol table without symbol table")
|
|
|
|
|
for _, symbol := range commonSymbols {
|
|
|
|
|
fmt.Println("new symbol", symbol.Mangle, "-", symbol.CPP, "-", symbol.Go)
|
|
|
|
|
}
|
2024-09-25 14:59:02 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root := cjson.Array()
|
|
|
|
|
defer root.Delete()
|
|
|
|
|
|
|
|
|
|
for _, symbol := range commonSymbols {
|
|
|
|
|
item := cjson.Object()
|
|
|
|
|
item.SetItem(c.Str("mangle"), cjson.String(c.AllocaCStr(symbol.Mangle)))
|
|
|
|
|
item.SetItem(c.Str("c++"), cjson.String(c.AllocaCStr(symbol.CPP)))
|
|
|
|
|
item.SetItem(c.Str("go"), cjson.String(c.AllocaCStr(symbol.Go)))
|
|
|
|
|
root.AddItem(item)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cStr := root.Print()
|
|
|
|
|
if cStr == nil {
|
|
|
|
|
return nil, errors.New("symbol table is empty")
|
|
|
|
|
}
|
|
|
|
|
defer c.Free(unsafe.Pointer(cStr))
|
|
|
|
|
result := []byte(c.GoString(cStr))
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GenerateAndUpdateSymbolTable(symbols []*nm.Symbol, headerInfos map[string]*parse.SymbolInfo, symbFile string) ([]byte, error) {
|
|
|
|
|
commonSymbols := GetCommonSymbols(symbols, headerInfos)
|
2024-10-17 18:54:28 +08:00
|
|
|
if debugSymbol {
|
|
|
|
|
fmt.Println("GenerateAndUpdateSymbolTable:", len(commonSymbols), "common symbols")
|
|
|
|
|
}
|
2024-09-25 14:59:02 +08:00
|
|
|
|
2024-10-17 18:54:28 +08:00
|
|
|
existSymbols, exist := ReadExistingSymbolTable(symbFile)
|
|
|
|
|
if exist && debugSymbol {
|
|
|
|
|
fmt.Println("GenerateAndUpdateSymbolTable:current path have exist symbol table", symbFile)
|
|
|
|
|
}
|
2024-09-25 14:59:02 +08:00
|
|
|
|
|
|
|
|
symbolData, err := GenSymbolTableData(commonSymbols, existSymbols)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return symbolData, nil
|
|
|
|
|
}
|