-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsymg.go
More file actions
179 lines (153 loc) · 4.57 KB
/
symg.go
File metadata and controls
179 lines (153 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package symg
import (
"fmt"
"os"
"runtime"
"sort"
"strings"
"github.com/goplus/llcppg/_xtool/internal/clangtool"
"github.com/goplus/llcppg/_xtool/internal/header"
"github.com/goplus/llcppg/_xtool/internal/ld"
llcppg "github.com/goplus/llcppg/config"
"github.com/goplus/llgo/xtool/nm"
)
type dbgFlags = int
var (
dbgSymbol bool
)
const (
DbgSymbol dbgFlags = 1 << iota
DbgFlagAll = DbgSymbol
)
func SetDebug(flags dbgFlags) {
dbgSymbol = (flags & DbgSymbol) != 0
}
type Config struct {
Libs string
CFlags string
Includes []string
Mix bool
TrimPrefixes []string
SymMap map[string]string
IsCpp bool
LibMode LibMode
}
func Do(conf *Config) (symbolTable []*llcppg.SymbolInfo, err error) {
symbols, err := fetchSymbols(conf.Libs, conf.LibMode)
if err != nil {
return
}
pkgHfiles := header.PkgHfileInfo(&header.Config{
Includes: conf.Includes,
Args: strings.Fields(conf.CFlags),
Mix: conf.Mix,
})
if dbgSymbol {
fmt.Println("interfaces", pkgHfiles.Inters)
fmt.Println("implements", pkgHfiles.Impls)
fmt.Println("thirdhfile", pkgHfiles.Thirds)
}
tempFile, err := os.CreateTemp("", "combine*.h")
if err != nil {
return
}
defer os.Remove(tempFile.Name())
err = clangtool.ComposeIncludes(conf.Includes, tempFile.Name())
if err != nil {
return
}
headerInfos, err := ParseHeaderFile(tempFile.Name(), pkgHfiles.CurPkgFiles(), conf.TrimPrefixes, strings.Fields(conf.CFlags), conf.SymMap, conf.IsCpp)
if err != nil {
return
}
symbolTable = GetCommonSymbols(symbols, headerInfos)
return
}
// fetchSymbols 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 fetchSymbols(lib string, mode LibMode) ([]*nm.Symbol, error) {
if dbgSymbol {
fmt.Println("fetchSymbols:from", lib)
}
sysPaths := ld.GetLibSearchPaths()
if dbgSymbol {
fmt.Println("fetchSymbols:sysPaths", sysPaths)
}
lbs := ParseLibs(lib)
if dbgSymbol {
fmt.Println("fetchSymbols:LibConfig Parse To")
fmt.Println("libs.Names: ", lbs.Names)
fmt.Println("libs.Paths: ", lbs.Paths)
}
libFiles, notFounds, err := lbs.Files(sysPaths, mode)
if err != nil {
return nil, fmt.Errorf("failed to generate some dylib paths: %v", err)
}
if dbgSymbol {
fmt.Println("fetchSymbols:libFiles", libFiles)
if len(notFounds) > 0 {
fmt.Println("fetchSymbols:not found libname", notFounds)
} else {
fmt.Println("fetchSymbols:every library is found")
}
}
var symbols []*nm.Symbol
var parseErrors []string
for _, libFile := range libFiles {
args := []string{"-g"}
if runtime.GOOS == "linux" {
args = append(args, "-D")
}
files, err := nm.New("llvm-nm").List(libFile, args...)
if err != nil {
parseErrors = append(parseErrors, fmt.Sprintf("fetchSymbols:Failed to list symbols in dylib %s: %v", libFile, err))
continue
}
for _, file := range files {
symbols = append(symbols, file.Symbols...)
}
}
if len(symbols) > 0 {
if dbgSymbol {
if len(parseErrors) > 0 {
fmt.Printf("fetchSymbols:Some libraries could not be parsed: %v\n", parseErrors)
}
fmt.Println("fetchSymbols:", len(symbols), "symbols")
}
return symbols, nil
}
return nil, fmt.Errorf("no symbols found in any lib. Errors: %v", parseErrors)
}
// todo(zzy):only public for test,when llgo test support private package test,this function should be private
// GetCommonSymbols finds the intersection of symbols from the library symbol table and the symbols parsed from header files.
// It returns a list of symbols that can be externally linked.
func GetCommonSymbols(syms []*nm.Symbol, headerSymbols map[string]*SymbolInfo) []*llcppg.SymbolInfo {
var commonSymbols []*llcppg.SymbolInfo
processedSymbols := make(map[string]bool)
for _, sym := range syms {
symName := sym.Name
if runtime.GOOS == "darwin" {
symName = strings.TrimPrefix(symName, "_")
}
if _, ok := processedSymbols[symName]; ok {
continue
}
if symInfo, ok := headerSymbols[symName]; ok {
symbolInfo := &llcppg.SymbolInfo{
Mangle: symName,
CPP: symInfo.ProtoName,
Go: symInfo.GoName,
}
commonSymbols = append(commonSymbols, symbolInfo)
processedSymbols[symName] = true
}
}
sort.Slice(commonSymbols, func(i, j int) bool {
return commonSymbols[i].Mangle < commonSymbols[j].Mangle
})
return commonSymbols
}