Skip to content

Commit 950bf17

Browse files
committed
add libc wrapper
Signed-off-by: leongross <[email protected]>
1 parent c728e03 commit 950bf17

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

GNUmakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ endif
926926
@cp -rp lib/musl/src/env build/release/tinygo/lib/musl/src
927927
@cp -rp lib/musl/src/errno build/release/tinygo/lib/musl/src
928928
@cp -rp lib/musl/src/exit build/release/tinygo/lib/musl/src
929+
@cp -rp lib/musl/src/fcntl build/release/tinygo/lib/musl/src
929930
@cp -rp lib/musl/src/include build/release/tinygo/lib/musl/src
930931
@cp -rp lib/musl/src/internal build/release/tinygo/lib/musl/src
931932
@cp -rp lib/musl/src/legacy build/release/tinygo/lib/musl/src

builder/musl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ var libMusl = Library{
116116
"env/*.c",
117117
"errno/*.c",
118118
"exit/*.c",
119+
"fcntl/*.c",
119120
"internal/defsysinfo.c",
120121
"internal/libc.c",
121122
"internal/syscall_ret.c",

src/runtime/os_linux.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package runtime
55
// This file is for systems that are _actually_ Linux (not systems that pretend
66
// to be Linux, like baremetal systems).
77

8-
import "unsafe"
8+
import (
9+
"unsafe"
10+
)
911

1012
const GOOS = "linux"
1113

@@ -83,6 +85,13 @@ type elfProgramHeader32 struct {
8385
//go:extern __ehdr_start
8486
var ehdr_start elfHeader
8587

88+
var errno int32
89+
90+
//export __errno_location
91+
func libc_errno_location() *int32 {
92+
return &errno
93+
}
94+
8695
// findGlobals finds globals in the .data/.bss sections.
8796
// It parses the ELF program header to find writable segments.
8897
func findGlobals(found func(start, end uintptr)) {
@@ -139,3 +148,16 @@ func hardwareRand() (n uint64, ok bool) {
139148
//
140149
//export getrandom
141150
func libc_getrandom(buf unsafe.Pointer, buflen uintptr, flags uint32) uint32
151+
152+
// int fcntl(int fd, int cmd, int arg);
153+
//
154+
// export fcntl
155+
func libc_fcntl(fd int, cmd int, arg int) (ret int)
156+
157+
func fcntl(fd int, cmd int, arg int) (ret int, errno int) {
158+
ret = libc_fcntl(fd, cmd, arg)
159+
if ret < 0 {
160+
return 0, int(uintptr(*libc_errno_location()))
161+
}
162+
return ret, 0
163+
}

0 commit comments

Comments
 (0)