From 2d1120bf6984cda9c29f550d3cd86e47a738057c Mon Sep 17 00:00:00 2001 From: Haolan Date: Fri, 5 Sep 2025 16:27:38 +0800 Subject: [PATCH] fix: symbol not found when building in baremental environment --- runtime/internal/clite/c.go | 2 +- runtime/internal/clite/debug/debug.go | 2 +- .../internal/clite/debug/debug_baremental.go | 40 +++++++++++++++++++ runtime/internal/clite/debug/debug_wasm.go | 2 + .../internal/clite/debug/libunwind_linux.go | 2 +- runtime/internal/clite/stdio_baremental.go | 29 ++++++++++++++ runtime/internal/clite/stdio_default.go | 3 +- .../internal/syscall/unix/fcntl_baremental.go | 15 +++++++ .../lib/internal/syscall/unix/fcntl_unix.go | 2 + .../lib/runtime/maxprocs_baremental.go | 5 +++ .../internal/lib/runtime/maxprocs_default.go | 11 +++++ runtime/internal/lib/runtime/runtime.go | 8 ---- .../internal/runtime/rethrow_baremental.go | 6 +++ runtime/internal/runtime/rethrow_default.go | 29 ++++++++++++++ runtime/internal/runtime/z_rt.go | 21 ---------- 15 files changed, 143 insertions(+), 34 deletions(-) create mode 100644 runtime/internal/clite/debug/debug_baremental.go create mode 100644 runtime/internal/clite/stdio_baremental.go create mode 100644 runtime/internal/lib/internal/syscall/unix/fcntl_baremental.go create mode 100644 runtime/internal/lib/runtime/maxprocs_baremental.go create mode 100644 runtime/internal/lib/runtime/maxprocs_default.go create mode 100644 runtime/internal/runtime/rethrow_baremental.go create mode 100644 runtime/internal/runtime/rethrow_default.go diff --git a/runtime/internal/clite/c.go b/runtime/internal/clite/c.go index 9d551872..dbbe80d9 100644 --- a/runtime/internal/clite/c.go +++ b/runtime/internal/clite/c.go @@ -21,7 +21,7 @@ import ( ) const ( - LLGoPackage = "decl" + LLGoPackage = "link" ) type ( diff --git a/runtime/internal/clite/debug/debug.go b/runtime/internal/clite/debug/debug.go index 67109984..c335c5d6 100644 --- a/runtime/internal/clite/debug/debug.go +++ b/runtime/internal/clite/debug/debug.go @@ -1,4 +1,4 @@ -//go:build !wasm +//go:build !wasm && !baremental package debug diff --git a/runtime/internal/clite/debug/debug_baremental.go b/runtime/internal/clite/debug/debug_baremental.go new file mode 100644 index 00000000..9132038c --- /dev/null +++ b/runtime/internal/clite/debug/debug_baremental.go @@ -0,0 +1,40 @@ +//go:build baremental + +package debug + +import ( + "unsafe" + + c "github.com/goplus/llgo/runtime/internal/clite" +) + +type Info struct { + Fname *c.Char + Fbase c.Pointer + Sname *c.Char + Saddr c.Pointer +} + +func Address() unsafe.Pointer { + panic("not implemented") +} + +func Addrinfo(addr unsafe.Pointer, info *Info) c.Int { + panic("not implemented") +} + +type Frame struct { + PC uintptr + Offset uintptr + SP unsafe.Pointer + Name string +} + +func StackTrace(skip int, fn func(fr *Frame) bool) { + panic("not implemented") +} + +func PrintStack(skip int) { + panic("not implemented") + +} diff --git a/runtime/internal/clite/debug/debug_wasm.go b/runtime/internal/clite/debug/debug_wasm.go index 08bde338..6b217bf2 100644 --- a/runtime/internal/clite/debug/debug_wasm.go +++ b/runtime/internal/clite/debug/debug_wasm.go @@ -1,3 +1,5 @@ +//go:build wasm + package debug import ( diff --git a/runtime/internal/clite/debug/libunwind_linux.go b/runtime/internal/clite/debug/libunwind_linux.go index f8b377ca..3857e052 100644 --- a/runtime/internal/clite/debug/libunwind_linux.go +++ b/runtime/internal/clite/debug/libunwind_linux.go @@ -1,4 +1,4 @@ -//go:build linux +//go:build linux && !baremental package debug diff --git a/runtime/internal/clite/stdio_baremental.go b/runtime/internal/clite/stdio_baremental.go new file mode 100644 index 00000000..2f1ca178 --- /dev/null +++ b/runtime/internal/clite/stdio_baremental.go @@ -0,0 +1,29 @@ +//go:build baremental + +/* + * 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 c + +import _ "unsafe" + +//go:linkname Stdin stdin +var Stdin FilePtr + +//go:linkname Stdout stdout +var Stdout FilePtr + +var Stderr FilePtr = Fopen(Str("/dev/stderr"), Str("w")) diff --git a/runtime/internal/clite/stdio_default.go b/runtime/internal/clite/stdio_default.go index 5f95fd27..f20e2519 100644 --- a/runtime/internal/clite/stdio_default.go +++ b/runtime/internal/clite/stdio_default.go @@ -1,5 +1,4 @@ -//go:build !darwin -// +build !darwin +//go:build !darwin && !baremental /* * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. diff --git a/runtime/internal/lib/internal/syscall/unix/fcntl_baremental.go b/runtime/internal/lib/internal/syscall/unix/fcntl_baremental.go new file mode 100644 index 00000000..a38a123c --- /dev/null +++ b/runtime/internal/lib/internal/syscall/unix/fcntl_baremental.go @@ -0,0 +1,15 @@ +//go:build baremental + +package unix + +import ( + _ "unsafe" +) + +func Fcntl(fd int, cmd int, arg int) (int, error) { + return 0, nil +} + +func fcntl(fd int, cmd int, arg int) (int, error) { + return 0, nil +} diff --git a/runtime/internal/lib/internal/syscall/unix/fcntl_unix.go b/runtime/internal/lib/internal/syscall/unix/fcntl_unix.go index 93c768d9..250e541c 100644 --- a/runtime/internal/lib/internal/syscall/unix/fcntl_unix.go +++ b/runtime/internal/lib/internal/syscall/unix/fcntl_unix.go @@ -1,3 +1,5 @@ +//go:build !baremental + package unix import ( diff --git a/runtime/internal/lib/runtime/maxprocs_baremental.go b/runtime/internal/lib/runtime/maxprocs_baremental.go new file mode 100644 index 00000000..8f6ba3a0 --- /dev/null +++ b/runtime/internal/lib/runtime/maxprocs_baremental.go @@ -0,0 +1,5 @@ +//go:build baremental + +package runtime + +func c_maxprocs() int32 { return 1 } diff --git a/runtime/internal/lib/runtime/maxprocs_default.go b/runtime/internal/lib/runtime/maxprocs_default.go new file mode 100644 index 00000000..3cd5174d --- /dev/null +++ b/runtime/internal/lib/runtime/maxprocs_default.go @@ -0,0 +1,11 @@ +//go:build !baremental + +package runtime + +const ( + LLGoPackage = "link" + LLGoFiles = "_wrap/runtime.c" +) + +//go:linkname c_maxprocs C.llgo_maxprocs +func c_maxprocs() int32 diff --git a/runtime/internal/lib/runtime/runtime.go b/runtime/internal/lib/runtime/runtime.go index 0b6fd9a7..0defce9e 100644 --- a/runtime/internal/lib/runtime/runtime.go +++ b/runtime/internal/lib/runtime/runtime.go @@ -26,11 +26,6 @@ import ( // llgo:skipall type _runtime struct{} -const ( - LLGoPackage = "link" - LLGoFiles = "_wrap/runtime.c" -) - var defaultGOROOT string // set by cmd/link func GOROOT() string { @@ -43,9 +38,6 @@ func Version() string { return buildVersion } -//go:linkname c_maxprocs C.llgo_maxprocs -func c_maxprocs() int32 - func GOMAXPROCS(n int) int { return int(c_maxprocs()) } diff --git a/runtime/internal/runtime/rethrow_baremental.go b/runtime/internal/runtime/rethrow_baremental.go new file mode 100644 index 00000000..4283500f --- /dev/null +++ b/runtime/internal/runtime/rethrow_baremental.go @@ -0,0 +1,6 @@ +//go:build baremental + +package runtime + +// Rethrow rethrows a panic. +func Rethrow(link *Defer) {} diff --git a/runtime/internal/runtime/rethrow_default.go b/runtime/internal/runtime/rethrow_default.go new file mode 100644 index 00000000..19cb5272 --- /dev/null +++ b/runtime/internal/runtime/rethrow_default.go @@ -0,0 +1,29 @@ +//go:build !baremental + +package runtime + +import ( + c "github.com/goplus/llgo/runtime/internal/clite" + "github.com/goplus/llgo/runtime/internal/clite/debug" + "github.com/goplus/llgo/runtime/internal/clite/pthread" +) + +// Rethrow rethrows a panic. +func Rethrow(link *Defer) { + if ptr := excepKey.Get(); ptr != nil { + if link == nil { + TracePanic(*(*any)(ptr)) + debug.PrintStack(2) + c.Free(ptr) + c.Exit(2) + } else { + c.Siglongjmp(link.Addr, 1) + } + } else if link == nil && goexitKey.Get() != nil { + if pthread.Equal(mainThread, pthread.Self()) != 0 { + fatal("no goroutines (main called runtime.Goexit) - deadlock!") + c.Exit(2) + } + pthread.Exit(nil) + } +} diff --git a/runtime/internal/runtime/z_rt.go b/runtime/internal/runtime/z_rt.go index fa29c25d..107696de 100644 --- a/runtime/internal/runtime/z_rt.go +++ b/runtime/internal/runtime/z_rt.go @@ -20,7 +20,6 @@ import ( "unsafe" c "github.com/goplus/llgo/runtime/internal/clite" - "github.com/goplus/llgo/runtime/internal/clite/debug" "github.com/goplus/llgo/runtime/internal/clite/pthread" "github.com/goplus/llgo/runtime/internal/clite/setjmp" ) @@ -57,26 +56,6 @@ func Panic(v any) { Rethrow((*Defer)(c.GoDeferData())) } -// Rethrow rethrows a panic. -func Rethrow(link *Defer) { - if ptr := excepKey.Get(); ptr != nil { - if link == nil { - TracePanic(*(*any)(ptr)) - debug.PrintStack(2) - c.Free(ptr) - c.Exit(2) - } else { - c.Siglongjmp(link.Addr, 1) - } - } else if link == nil && goexitKey.Get() != nil { - if pthread.Equal(mainThread, pthread.Self()) != 0 { - fatal("no goroutines (main called runtime.Goexit) - deadlock!") - c.Exit(2) - } - pthread.Exit(nil) - } -} - var ( excepKey pthread.Key goexitKey pthread.Key