Skip to content

Commit aa3e22c

Browse files
committed
llcppsymg:syspath.GetLibPaths
1 parent 446d50e commit aa3e22c

File tree

6 files changed

+70
-48
lines changed

6 files changed

+70
-48
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#stdout
2+
=== TestLdOutput ===
3+
[/usr/local/lib/aarch64-linux-gnu /lib/aarch64-linux-gnu /usr/lib/aarch64-linux-gnu /usr/local/lib /lib /usr/lib /usr/aarch64-linux-gnu/lib]
4+
5+
#stderr
6+
7+
#exit 0
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/goplus/llcppg/_xtool/llcppsymg/syspath"
7+
)
8+
9+
func main() {
10+
TestLdOutput()
11+
}
12+
13+
func TestLdOutput() {
14+
fmt.Println("=== TestLdOutput ===")
15+
res := syspath.ParseLdOutput(
16+
`GNU ld (GNU Binutils for Ubuntu) 2.42
17+
Supported emulations:
18+
aarch64linux
19+
aarch64elf
20+
aarch64elf32
21+
aarch64elf32b
22+
aarch64elfb
23+
armelf
24+
armelfb
25+
aarch64linuxb
26+
aarch64linux32
27+
aarch64linux32b
28+
armelfb_linux_eabi
29+
armelf_linux_eabi
30+
using internal linker script:
31+
==================================================
32+
/* Script for -z combreloc */
33+
/* Copyright (C) 2014-2024 Free Software Foundation, Inc.
34+
Copying and distribution of this script, with or without modification,
35+
are permitted in any medium without royalty provided the copyright
36+
notice and this notice are preserved. */
37+
OUTPUT_FORMAT("elf64-littleaarch64", "elf64-bigaarch64",
38+
"elf64-littleaarch64")
39+
OUTPUT_ARCH(aarch64)
40+
ENTRY(_start)
41+
SEARCH_DIR("=/usr/local/lib/aarch64-linux-gnu"); SEARCH_DIR("=/lib/aarch64-linux-gnu"); SEARCH_DIR("=/usr/lib/aarch64-linux-gnu"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/aarch64-linux-gnu/lib");
42+
SECTIONS
43+
{
44+
/* Read-only sections, merged into text segment: */
45+
PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS;
46+
.interp : { *(.interp) }
47+
.note.gnu.build-id : { *(.note.gnu.build-id) }
48+
.hash : { *(.hash) }
49+
.gnu.hash : { *(.gnu.hash) }
50+
.dynsym : { *(.dynsym) }
51+
.dynstr : { *(.dynstr) }
52+
`)
53+
fmt.Println(res)
54+
}

_xtool/llcppsymg/config/cfgparse/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func ParseCFlags(cflags string) *CFlags {
8282
return cf
8383
}
8484

85-
func (cf *CFlags) GenHeaderFilePaths(files []string) ([]string, []string, error) {
85+
func (cf *CFlags) GenHeaderFilePaths(files []string, defaultPaths []string) ([]string, []string, error) {
8686
var foundPaths []string
8787
var notFound []string
8888

_xtool/llcppsymg/llcppsymg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func main() {
7979
check(err)
8080

8181
cflag := cfgparse.ParseCFlags(conf.CFlags)
82-
filepaths, notFounds, err := cflag.GenHeaderFilePaths(conf.Include)
82+
filepaths, notFounds, err := cflag.GenHeaderFilePaths(conf.Include, []string{})
8383
check(err)
8484

8585
if ags.Verbose {

_xtool/llcppsymg/symbol/symbol.go

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"os"
7-
"os/exec"
8-
"regexp"
97
"runtime"
108
"strings"
119
"unsafe"
@@ -14,6 +12,7 @@ import (
1412
"github.com/goplus/llcppg/_xtool/llcppsymg/config/cfgparse"
1513
"github.com/goplus/llcppg/_xtool/llcppsymg/dbg"
1614
"github.com/goplus/llcppg/_xtool/llcppsymg/parse"
15+
"github.com/goplus/llcppg/_xtool/llcppsymg/syspath"
1716
"github.com/goplus/llcppg/types"
1817
"github.com/goplus/llgo/c"
1918
"github.com/goplus/llgo/c/cjson"
@@ -30,7 +29,11 @@ func ParseDylibSymbols(lib string) ([]*nm.Symbol, error) {
3029
if dbg.GetDebugSymbol() {
3130
fmt.Println("ParseDylibSymbols:from", lib)
3231
}
33-
sysPaths := getSysLibPaths()
32+
sysPaths = syspath.GetLibPaths()
33+
if dbg.GetDebugSymbol() {
34+
fmt.Println("ParseDylibSymbols:sysPaths", sysPaths)
35+
}
36+
3437
lbs := cfgparse.ParseLibs(lib)
3538
if dbg.GetDebugSymbol() {
3639
fmt.Println("ParseDylibSymbols:LibConfig Parse To")
@@ -91,48 +94,6 @@ func ParseDylibSymbols(lib string) ([]*nm.Symbol, error) {
9194
return nil, fmt.Errorf("no symbols found in any dylib. Errors: %v", parseErrors)
9295
}
9396

94-
func getSysLibPaths() []string {
95-
var paths []string
96-
if runtime.GOOS == "linux" {
97-
if dbg.GetDebugSymbol() {
98-
fmt.Println("getSysLibPaths:find sys lib path from linux")
99-
}
100-
//resolution from https://github.com/goplus/llcppg/commit/02307485db9269481297a4dc5e8449fffaa4f562
101-
cmd := exec.Command("ld", "--verbose")
102-
output, err := cmd.Output()
103-
if err != nil {
104-
panic(err)
105-
}
106-
matches := regexp.MustCompile(`SEARCH_DIR\("=([^"]+)"\)`).FindAllStringSubmatch(string(output), -1)
107-
for _, match := range matches {
108-
paths = append(paths, match[1])
109-
}
110-
return paths
111-
}
112-
return paths
113-
}
114-
115-
func getPath(file string) []string {
116-
if dbg.GetDebugSymbol() {
117-
fmt.Println("getPath:from", file)
118-
}
119-
var paths []string
120-
content, err := os.ReadFile(file)
121-
if err != nil {
122-
return paths
123-
}
124-
lines := strings.Split(string(content), "\n")
125-
for _, line := range lines {
126-
line = strings.TrimSpace(line)
127-
if line != "" && !strings.HasPrefix(line, "#") {
128-
if file, err := os.Stat(line); err == nil && file.IsDir() {
129-
paths = append(paths, line)
130-
}
131-
}
132-
}
133-
return paths
134-
}
135-
13697
// finds the intersection of symbols from the dynamic library's symbol table and the symbols parsed from header files.
13798
// It returns a list of symbols that can be externally linked.
13899
func GetCommonSymbols(dylibSymbols []*nm.Symbol, headerSymbols map[string]*parse.SymbolInfo) []*types.SymbolInfo {

cmd/gogensig/convert/deps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (p *PkgInfo) GetIncPaths() ([]string, []string, error) {
164164
}
165165
expandedIncFlags := env.ExpandEnv(p.CppgConf.CFlags)
166166
cflags := cfgparse.ParseCFlags(expandedIncFlags)
167-
incPaths, notFounds, err := cflags.GenHeaderFilePaths(p.CppgConf.Include)
167+
incPaths, notFounds, err := cflags.GenHeaderFilePaths(p.CppgConf.Include, []string{})
168168
p.includes = incPaths
169169
return incPaths, notFounds, err
170170
}

0 commit comments

Comments
 (0)