Skip to content

Commit da14cd7

Browse files
author
Greg Parker
authored
[runtime] Clean up symbol exports in libc functions. (#13202)
1 parent 415b36d commit da14cd7

24 files changed

+377
-437
lines changed

docs/StandardLibraryProgrammersManual.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,17 @@ This attribute specifies the name that a declaration will have at link time. It
137137
Rather than hard-code Swift mangling into C code, `@_silgen_name` is used to provide a stable and known symbol name for linking. Note that C code still must understand and use the Swift calling convention (available in swift-clang) for such Swift functions (if they use Swift's CC). Example:
138138

139139
```swift
140-
@_silgen_name("_swift_stdlib_destroyTLS")
140+
@_silgen_name("_destroyTLS")
141141
internal func _destroyTLS(_ ptr: UnsafeMutableRawPointer?) {
142142
// ... implementation ...
143143
}
144144
```
145145

146146
```C++
147-
__attribute__((__swiftcall__)) extern "C" void _swift_stdlib_destroyTLS(void *);
147+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
148+
void _destroyTLS(void *);
148149

149-
// ... C code can now call _swift_stdlib_destroyTLS on a void * ...
150+
// ... C code can now call _destroyTLS on a void * ...
150151
```
151152
152153
##### Using `@_silgen_name` to call C from Swift

stdlib/private/SwiftPrivate/IO.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public struct _FDInputStream {
5757
let fd = self.fd
5858
let addr = _buffer.baseAddress! + self._bufferUsed
5959
let size = bufferFree
60-
return _swift_stdlib_read(fd, addr, size)
60+
return _stdlib_read(fd, addr, size)
6161
}
6262
if readResult == 0 {
6363
isEOF = true
@@ -73,7 +73,7 @@ public struct _FDInputStream {
7373
if isClosed {
7474
return
7575
}
76-
let result = _swift_stdlib_close(fd)
76+
let result = _stdlib_close(fd)
7777
if result < 0 {
7878
fatalError("close() returned an error")
7979
}
@@ -106,7 +106,7 @@ public struct _FDOutputStream : TextOutputStream {
106106
var writtenBytes = 0
107107
let bufferSize = utf8CStr.count - 1
108108
while writtenBytes != bufferSize {
109-
let result = _swift_stdlib_write(
109+
let result = _stdlib_write(
110110
self.fd, UnsafeRawPointer(utf8CStr.baseAddress! + Int(writtenBytes)),
111111
bufferSize - writtenBytes)
112112
if result < 0 {
@@ -121,7 +121,7 @@ public struct _FDOutputStream : TextOutputStream {
121121
if isClosed {
122122
return
123123
}
124-
let result = _swift_stdlib_close(fd)
124+
let result = _stdlib_close(fd)
125125
if result < 0 {
126126
fatalError("close() returned an error")
127127
}

stdlib/private/SwiftPrivate/PRNG.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
import SwiftShims
1414

1515
public func rand32() -> UInt32 {
16-
return _swift_stdlib_cxx11_mt19937()
16+
return _stdlib_cxx11_mt19937()
1717
}
1818

1919
public func rand32(exclusiveUpperBound limit: UInt32) -> UInt32 {
20-
return _swift_stdlib_cxx11_mt19937_uniform(limit)
20+
return _stdlib_cxx11_mt19937_uniform(limit)
2121
}
2222

2323
public func rand64() -> UInt64 {
2424
return
25-
(UInt64(_swift_stdlib_cxx11_mt19937()) << 32) |
26-
UInt64(_swift_stdlib_cxx11_mt19937())
25+
(UInt64(_stdlib_cxx11_mt19937()) << 32) |
26+
UInt64(_stdlib_cxx11_mt19937())
2727
}
2828

2929
public func randInt() -> Int {

stdlib/private/SwiftPrivateLibcExtras/Subprocess.c

+5-16
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,39 @@
2020
#define availability(...)
2121
#include <spawn.h>
2222
#include <sys/types.h>
23-
#if defined(__APPLE__)
24-
// NOTE: forward declare this rather than including crt_externs.h as not all
25-
// SDKs provide it
26-
extern char ***_NSGetEnviron(void);
27-
#endif // defined(__APPLE__)
2823

2924
SWIFT_CC(swift)
30-
int swift_posix_spawn_file_actions_init(
25+
int _stdlib_posix_spawn_file_actions_init(
3126
posix_spawn_file_actions_t *file_actions) {
3227
return posix_spawn_file_actions_init(file_actions);
3328
}
3429

3530
SWIFT_CC(swift)
36-
int swift_posix_spawn_file_actions_destroy(
31+
int _stdlib_posix_spawn_file_actions_destroy(
3732
posix_spawn_file_actions_t *file_actions) {
3833
return posix_spawn_file_actions_destroy(file_actions);
3934
}
4035

4136
SWIFT_CC(swift)
42-
int swift_posix_spawn_file_actions_addclose(
37+
int _stdlib_posix_spawn_file_actions_addclose(
4338
posix_spawn_file_actions_t *file_actions, int filedes) {
4439
return posix_spawn_file_actions_addclose(file_actions, filedes);
4540
}
4641

4742
SWIFT_CC(swift)
48-
int swift_posix_spawn_file_actions_adddup2(
43+
int _stdlib_posix_spawn_file_actions_adddup2(
4944
posix_spawn_file_actions_t *file_actions, int filedes, int newfiledes) {
5045
return posix_spawn_file_actions_adddup2(file_actions, filedes, newfiledes);
5146
}
5247

5348
SWIFT_CC(swift)
54-
int swift_posix_spawn(pid_t *__restrict pid, const char * __restrict path,
49+
int _stdlib_posix_spawn(pid_t *__restrict pid, const char * __restrict path,
5550
const posix_spawn_file_actions_t *file_actions,
5651
const posix_spawnattr_t *__restrict attrp,
5752
char *const argv[__restrict],
5853
char *const envp[__restrict]) {
5954
return posix_spawn(pid, path, file_actions, attrp, argv, envp);
6055
}
6156

62-
#if defined(__APPLE__)
63-
SWIFT_CC(swift)
64-
char ***swift_SwiftPrivateLibcExtras_NSGetEnviron(void) {
65-
return _NSGetEnviron();
66-
}
67-
#endif // defined(__APPLE__)
6857
#endif // !defined(__ANDROID__) && !defined(__HAIKU__) && (!defined(_WIN32) || defined(__CGYWIN__))
6958

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

+41-62
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,43 @@ import Glibc
2323
// posix_spawn is not available on Android.
2424
// posix_spawn is not available on Haiku.
2525
#if !os(Android) && !os(Haiku)
26-
// swift_posix_spawn isn't available in the public watchOS SDK, we sneak by the
26+
// posix_spawn isn't available in the public watchOS SDK, we sneak by the
2727
// unavailable attribute declaration here of the APIs that we need.
2828

2929
// FIXME: Come up with a better way to deal with APIs that are pointers on some
3030
// platforms but not others.
3131
#if os(Linux)
32-
typealias swift_posix_spawn_file_actions_t = posix_spawn_file_actions_t
32+
typealias _stdlib_posix_spawn_file_actions_t = posix_spawn_file_actions_t
3333
#else
34-
typealias swift_posix_spawn_file_actions_t = posix_spawn_file_actions_t?
34+
typealias _stdlib_posix_spawn_file_actions_t = posix_spawn_file_actions_t?
3535
#endif
3636

37-
@_silgen_name("swift_posix_spawn_file_actions_init")
38-
func swift_posix_spawn_file_actions_init(
39-
_ file_actions: UnsafeMutablePointer<swift_posix_spawn_file_actions_t>
37+
@_silgen_name("_stdlib_posix_spawn_file_actions_init")
38+
internal func _stdlib_posix_spawn_file_actions_init(
39+
_ file_actions: UnsafeMutablePointer<_stdlib_posix_spawn_file_actions_t>
4040
) -> CInt
4141

42-
@_silgen_name("swift_posix_spawn_file_actions_destroy")
43-
func swift_posix_spawn_file_actions_destroy(
44-
_ file_actions: UnsafeMutablePointer<swift_posix_spawn_file_actions_t>
42+
@_silgen_name("_stdlib_posix_spawn_file_actions_destroy")
43+
internal func _stdlib_posix_spawn_file_actions_destroy(
44+
_ file_actions: UnsafeMutablePointer<_stdlib_posix_spawn_file_actions_t>
4545
) -> CInt
4646

47-
@_silgen_name("swift_posix_spawn_file_actions_addclose")
48-
func swift_posix_spawn_file_actions_addclose(
49-
_ file_actions: UnsafeMutablePointer<swift_posix_spawn_file_actions_t>,
47+
@_silgen_name("_stdlib_posix_spawn_file_actions_addclose")
48+
internal func _stdlib_posix_spawn_file_actions_addclose(
49+
_ file_actions: UnsafeMutablePointer<_stdlib_posix_spawn_file_actions_t>,
5050
_ filedes: CInt) -> CInt
5151

52-
@_silgen_name("swift_posix_spawn_file_actions_adddup2")
53-
func swift_posix_spawn_file_actions_adddup2(
54-
_ file_actions: UnsafeMutablePointer<swift_posix_spawn_file_actions_t>,
52+
@_silgen_name("_stdlib_posix_spawn_file_actions_adddup2")
53+
internal func _stdlib_posix_spawn_file_actions_adddup2(
54+
_ file_actions: UnsafeMutablePointer<_stdlib_posix_spawn_file_actions_t>,
5555
_ filedes: CInt,
5656
_ newfiledes: CInt) -> CInt
5757

58-
@_silgen_name("swift_posix_spawn")
59-
func swift_posix_spawn(
58+
@_silgen_name("_stdlib_posix_spawn")
59+
internal func _stdlib_posix_spawn(
6060
_ pid: UnsafeMutablePointer<pid_t>?,
6161
_ file: UnsafePointer<Int8>,
62-
_ file_actions: UnsafePointer<swift_posix_spawn_file_actions_t>?,
62+
_ file_actions: UnsafePointer<_stdlib_posix_spawn_file_actions_t>?,
6363
_ attrp: UnsafePointer<posix_spawnattr_t>?,
6464
_ argv: UnsafePointer<UnsafeMutablePointer<Int8>?>,
6565
_ envp: UnsafePointer<UnsafeMutablePointer<Int8>?>?) -> CInt
@@ -113,7 +113,7 @@ public func spawnChild(_ args: [String])
113113
// code after this block will never be executed, and the parent write pipe
114114
// will be closed.
115115
withArrayOfCStrings([CommandLine.arguments[0]] + args) {
116-
execve(CommandLine.arguments[0], $0, _getEnviron())
116+
execve(CommandLine.arguments[0], $0, environ)
117117
}
118118

119119
// If execve() encountered an error, we write the errno encountered to the
@@ -142,44 +142,44 @@ public func spawnChild(_ args: [String])
142142
}
143143
#else
144144
var fileActions = _make_posix_spawn_file_actions_t()
145-
if swift_posix_spawn_file_actions_init(&fileActions) != 0 {
146-
preconditionFailure("swift_posix_spawn_file_actions_init() failed")
145+
if _stdlib_posix_spawn_file_actions_init(&fileActions) != 0 {
146+
preconditionFailure("_stdlib_posix_spawn_file_actions_init() failed")
147147
}
148148

149149
// Close the write end of the pipe on the child side.
150-
if swift_posix_spawn_file_actions_addclose(
150+
if _stdlib_posix_spawn_file_actions_addclose(
151151
&fileActions, childStdin.writeFD) != 0 {
152-
preconditionFailure("swift_posix_spawn_file_actions_addclose() failed")
152+
preconditionFailure("_stdlib_posix_spawn_file_actions_addclose() failed")
153153
}
154154

155155
// Remap child's stdin.
156-
if swift_posix_spawn_file_actions_adddup2(
156+
if _stdlib_posix_spawn_file_actions_adddup2(
157157
&fileActions, childStdin.readFD, STDIN_FILENO) != 0 {
158-
preconditionFailure("swift_posix_spawn_file_actions_adddup2() failed")
158+
preconditionFailure("_stdlib_posix_spawn_file_actions_adddup2() failed")
159159
}
160160

161161
// Close the read end of the pipe on the child side.
162-
if swift_posix_spawn_file_actions_addclose(
162+
if _stdlib_posix_spawn_file_actions_addclose(
163163
&fileActions, childStdout.readFD) != 0 {
164-
preconditionFailure("swift_posix_spawn_file_actions_addclose() failed")
164+
preconditionFailure("_stdlib_posix_spawn_file_actions_addclose() failed")
165165
}
166166

167167
// Remap child's stdout.
168-
if swift_posix_spawn_file_actions_adddup2(
168+
if _stdlib_posix_spawn_file_actions_adddup2(
169169
&fileActions, childStdout.writeFD, STDOUT_FILENO) != 0 {
170-
preconditionFailure("swift_posix_spawn_file_actions_adddup2() failed")
170+
preconditionFailure("_stdlib_posix_spawn_file_actions_adddup2() failed")
171171
}
172172

173173
// Close the read end of the pipe on the child side.
174-
if swift_posix_spawn_file_actions_addclose(
174+
if _stdlib_posix_spawn_file_actions_addclose(
175175
&fileActions, childStderr.readFD) != 0 {
176-
preconditionFailure("swift_posix_spawn_file_actions_addclose() failed")
176+
preconditionFailure("_stdlib_posix_spawn_file_actions_addclose() failed")
177177
}
178178

179179
// Remap child's stderr.
180-
if swift_posix_spawn_file_actions_adddup2(
180+
if _stdlib_posix_spawn_file_actions_adddup2(
181181
&fileActions, childStderr.writeFD, STDERR_FILENO) != 0 {
182-
preconditionFailure("swift_posix_spawn_file_actions_adddup2() failed")
182+
preconditionFailure("_stdlib_posix_spawn_file_actions_adddup2() failed")
183183
}
184184

185185
var pid: pid_t = -1
@@ -192,16 +192,16 @@ public func spawnChild(_ args: [String])
192192
}
193193
}
194194
let spawnResult = withArrayOfCStrings(childArgs) {
195-
swift_posix_spawn(
196-
&pid, childArgs[0], &fileActions, nil, $0, _getEnviron())
195+
_stdlib_posix_spawn(
196+
&pid, childArgs[0], &fileActions, nil, $0, environ)
197197
}
198198
if spawnResult != 0 {
199199
print(String(cString: strerror(spawnResult)))
200-
preconditionFailure("swift_posix_spawn() failed")
200+
preconditionFailure("_stdlib_posix_spawn() failed")
201201
}
202202

203-
if swift_posix_spawn_file_actions_destroy(&fileActions) != 0 {
204-
preconditionFailure("swift_posix_spawn_file_actions_destroy() failed")
203+
if _stdlib_posix_spawn_file_actions_destroy(&fileActions) != 0 {
204+
preconditionFailure("_stdlib_posix_spawn_file_actions_destroy() failed")
205205
}
206206
#endif
207207

@@ -226,12 +226,12 @@ public func spawnChild(_ args: [String])
226226
#if !os(Android) && !os(Haiku)
227227
#if os(Linux)
228228
internal func _make_posix_spawn_file_actions_t()
229-
-> swift_posix_spawn_file_actions_t {
229+
-> _stdlib_posix_spawn_file_actions_t {
230230
return posix_spawn_file_actions_t()
231231
}
232232
#else
233233
internal func _make_posix_spawn_file_actions_t()
234-
-> swift_posix_spawn_file_actions_t {
234+
-> _stdlib_posix_spawn_file_actions_t {
235235
return nil
236236
}
237237
#endif
@@ -292,27 +292,6 @@ public func posixWaitpid(_ pid: pid_t) -> ProcessTerminationStatus {
292292
preconditionFailure("did not understand what happened to child process")
293293
}
294294

295-
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
296-
@_silgen_name("swift_SwiftPrivateLibcExtras_NSGetEnviron")
297-
func _NSGetEnviron() -> UnsafeMutablePointer<UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>>
298-
#endif
299-
300-
internal func _getEnviron() -> UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
301-
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
302-
return _NSGetEnviron().pointee
303-
#elseif os(FreeBSD)
304-
return environ
305-
#elseif os(PS4)
306-
return environ
307-
#elseif os(Android)
308-
return environ
309-
#elseif os(Cygwin)
310-
return environ
311-
#elseif os(Haiku)
312-
return environ
313-
#else
314-
return __environ
315-
#endif
316-
}
295+
// !os(Windows)
317296
#endif
318297

stdlib/public/Platform/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ set(swift_platform_name)
22
set(swift_platform_flags)
33
set(swift_platform_sources
44
Platform.swift
5-
Misc.c
65
TiocConstants.swift
76
tgmath.swift.gyb)
87

0 commit comments

Comments
 (0)