Skip to content

Add sys/socket.h header file and associated symbols - #8

Draft
neomantra wants to merge 2 commits into
tinygo-org:mainfrom
neomantra:add-sys-socket
Draft

neomantra wants to merge 2 commits into
tinygo-org:mainfrom
neomantra:add-sys-socket

Conversation

@neomantra

Copy link
Copy Markdown
Contributor

tinygo-org/tinygo#5612 switches TinyGo to link against this SDK's real symbol list, which surfaced that no socket-family symbols are stubbed here: a Go program calling unix.Socket (golang.org/x/sys/unix) now stops at link time with:

zsyscall_darwin_arm64.go:105: linker could not find symbol _socket

(as noted in tinygo-org/tinygo#5612 (comment) — opening this PR as the follow-up instead of an issue.)

Changes, following the pattern of 4e4113e ("Add sys/sysctl.h header file and associated symbols"):

  • Extract sys/socket.h, net/net_kev.h, and machine/_param.h from the already-pinned xnu-7195.141.2 tarball (all APSL 2.0) and add them to update.sh.
  • Add __darwin_socklen_t to the public-domain machine/_types.h replacement (src/_types.h), needed by sys/_types/_socklen_t.h.
  • Include <sys/socket.h> from src/libSystem.h and regenerate src/{arm64,x86_64}/libSystem.s. This adds 22 stubs: accept, bind, connect, connectx, disconnectx, getpeername, getsockname, getsockopt, listen, pfctlinput, recv, recvfrom, recvmsg, send, sendfile, sendmsg, sendto, setsockopt, shutdown, sockatmark, socket, socketpair.
  • Add test/socket.c to make test, compiling and linking a socket()/connect() call for both architectures (the failure mode from the review comment).

Notes:

  • The CMSG_* macros in sys/socket.h expand to __DARWIN_ALIGN32, which lives in arm/_param.h / i386/_param.h. Since arm/_param.h is intentionally emptied by update.sh (license), those macros are unusable on arm64 for now — same as the existing arm-specific gaps in this tree. Function declarations and everything x/sys/unix needs are unaffected.
  • Unrelated pre-existing issue spotted while doing this: opensource.apple.com now redirects tarball downloads to GitHub codeload, whose archives have a xnu-xnu-$version/ root directory, so the extraction paths in update.sh no longer match freshly downloaded tarballs. I extracted the new headers with the same --strip-components layout; happy to fix update.sh separately if wanted.

Verified with make test CLANG=$(brew --prefix llvm)/bin/clang on macOS, plus an explicit -isysroot build of test/socket.c against the generated sysroots to confirm the new header itself parses (the default test invocation on macOS picks up host SDK headers), and llvm-nm shows the new symbols exported from the stub libSystem.B.dylib.

@neomantra
neomantra marked this pull request as draft September 19, 2026 22:50
This adds stubs for the socket family of functions (socket, bind,
connect, listen, accept, send*, recv*, etc.) so that programs using
them can be linked, such as Go programs using golang.org/x/sys/unix.

The sys/socket.h header requires machine/_param.h and net/net_kev.h
from xnu, and a __darwin_socklen_t typedef which is added to the
machine/_types.h replacement header.

Signed-off-by: Evan Wies <evan@neomantra.net>
Signed-off-by: Evan Wies <evan@neomantra.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant