Skip to content

Commit 5f48c7d

Browse files
committed
Remove ERRNO_MESSAGES from JS library. NFC
The native `strerror()` function can be used instead in the two places that still depend on `ERRNO_MESSAGES`
1 parent 131cb71 commit 5f48c7d

12 files changed

+36
-134
lines changed

src/jsifier.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ function getTransitiveDeps(symbol, debug) {
124124
directDeps = directDeps.filter((d) => typeof d === 'string');
125125
for (const dep of directDeps) {
126126
const resolved = resolveAlias(dep);
127+
if (VERBOSE && !transitiveDeps.has(dep)) {
128+
printErr(`adding dependency ${symbol} -> ${dep}`);
129+
}
127130
transitiveDeps.add(resolved);
128131
toVisit.push(resolved);
129132
}

src/library.js

Lines changed: 10 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,129 +1431,15 @@ addToLibrary({
14311431
'EOWNERDEAD': {{{ cDefs.EOWNERDEAD }}},
14321432
'ESTRPIPE': {{{ cDefs.ESTRPIPE }}},
14331433
}`,
1434-
$ERRNO_MESSAGES: {
1435-
0: 'Success',
1436-
{{{ cDefs.EPERM }}}: 'Not super-user',
1437-
{{{ cDefs.ENOENT }}}: 'No such file or directory',
1438-
{{{ cDefs.ESRCH }}}: 'No such process',
1439-
{{{ cDefs.EINTR }}}: 'Interrupted system call',
1440-
{{{ cDefs.EIO }}}: 'I/O error',
1441-
{{{ cDefs.ENXIO }}}: 'No such device or address',
1442-
{{{ cDefs.E2BIG }}}: 'Arg list too long',
1443-
{{{ cDefs.ENOEXEC }}}: 'Exec format error',
1444-
{{{ cDefs.EBADF }}}: 'Bad file number',
1445-
{{{ cDefs.ECHILD }}}: 'No children',
1446-
{{{ cDefs.EWOULDBLOCK }}}: 'No more processes',
1447-
{{{ cDefs.ENOMEM }}}: 'Not enough core',
1448-
{{{ cDefs.EACCES }}}: 'Permission denied',
1449-
{{{ cDefs.EFAULT }}}: 'Bad address',
1450-
{{{ cDefs.ENOTBLK }}}: 'Block device required',
1451-
{{{ cDefs.EBUSY }}}: 'Mount device busy',
1452-
{{{ cDefs.EEXIST }}}: 'File exists',
1453-
{{{ cDefs.EXDEV }}}: 'Cross-device link',
1454-
{{{ cDefs.ENODEV }}}: 'No such device',
1455-
{{{ cDefs.ENOTDIR }}}: 'Not a directory',
1456-
{{{ cDefs.EISDIR }}}: 'Is a directory',
1457-
{{{ cDefs.EINVAL }}}: 'Invalid argument',
1458-
{{{ cDefs.ENFILE }}}: 'Too many open files in system',
1459-
{{{ cDefs.EMFILE }}}: 'Too many open files',
1460-
{{{ cDefs.ENOTTY }}}: 'Not a typewriter',
1461-
{{{ cDefs.ETXTBSY }}}: 'Text file busy',
1462-
{{{ cDefs.EFBIG }}}: 'File too large',
1463-
{{{ cDefs.ENOSPC }}}: 'No space left on device',
1464-
{{{ cDefs.ESPIPE }}}: 'Illegal seek',
1465-
{{{ cDefs.EROFS }}}: 'Read only file system',
1466-
{{{ cDefs.EMLINK }}}: 'Too many links',
1467-
{{{ cDefs.EPIPE }}}: 'Broken pipe',
1468-
{{{ cDefs.EDOM }}}: 'Math arg out of domain of func',
1469-
{{{ cDefs.ERANGE }}}: 'Math result not representable',
1470-
{{{ cDefs.ENOMSG }}}: 'No message of desired type',
1471-
{{{ cDefs.EIDRM }}}: 'Identifier removed',
1472-
{{{ cDefs.ECHRNG }}}: 'Channel number out of range',
1473-
{{{ cDefs.EL2NSYNC }}}: 'Level 2 not synchronized',
1474-
{{{ cDefs.EL3HLT }}}: 'Level 3 halted',
1475-
{{{ cDefs.EL3RST }}}: 'Level 3 reset',
1476-
{{{ cDefs.ELNRNG }}}: 'Link number out of range',
1477-
{{{ cDefs.EUNATCH }}}: 'Protocol driver not attached',
1478-
{{{ cDefs.ENOCSI }}}: 'No CSI structure available',
1479-
{{{ cDefs.EL2HLT }}}: 'Level 2 halted',
1480-
{{{ cDefs.EDEADLK }}}: 'Deadlock condition',
1481-
{{{ cDefs.ENOLCK }}}: 'No record locks available',
1482-
{{{ cDefs.EBADE }}}: 'Invalid exchange',
1483-
{{{ cDefs.EBADR }}}: 'Invalid request descriptor',
1484-
{{{ cDefs.EXFULL }}}: 'Exchange full',
1485-
{{{ cDefs.ENOANO }}}: 'No anode',
1486-
{{{ cDefs.EBADRQC }}}: 'Invalid request code',
1487-
{{{ cDefs.EBADSLT }}}: 'Invalid slot',
1488-
{{{ cDefs.EDEADLOCK }}}: 'File locking deadlock error',
1489-
{{{ cDefs.EBFONT }}}: 'Bad font file fmt',
1490-
{{{ cDefs.ENOSTR }}}: 'Device not a stream',
1491-
{{{ cDefs.ENODATA }}}: 'No data (for no delay io)',
1492-
{{{ cDefs.ETIME }}}: 'Timer expired',
1493-
{{{ cDefs.ENOSR }}}: 'Out of streams resources',
1494-
{{{ cDefs.ENONET }}}: 'Machine is not on the network',
1495-
{{{ cDefs.ENOPKG }}}: 'Package not installed',
1496-
{{{ cDefs.EREMOTE }}}: 'The object is remote',
1497-
{{{ cDefs.ENOLINK }}}: 'The link has been severed',
1498-
{{{ cDefs.EADV }}}: 'Advertise error',
1499-
{{{ cDefs.ESRMNT }}}: 'Srmount error',
1500-
{{{ cDefs.ECOMM }}}: 'Communication error on send',
1501-
{{{ cDefs.EPROTO }}}: 'Protocol error',
1502-
{{{ cDefs.EMULTIHOP }}}: 'Multihop attempted',
1503-
{{{ cDefs.EDOTDOT }}}: 'Cross mount point (not really error)',
1504-
{{{ cDefs.EBADMSG }}}: 'Trying to read unreadable message',
1505-
{{{ cDefs.ENOTUNIQ }}}: 'Given log. name not unique',
1506-
{{{ cDefs.EBADFD }}}: 'f.d. invalid for this operation',
1507-
{{{ cDefs.EREMCHG }}}: 'Remote address changed',
1508-
{{{ cDefs.ELIBACC }}}: 'Can access a needed shared lib',
1509-
{{{ cDefs.ELIBBAD }}}: 'Accessing a corrupted shared lib',
1510-
{{{ cDefs.ELIBSCN }}}: '.lib section in a.out corrupted',
1511-
{{{ cDefs.ELIBMAX }}}: 'Attempting to link in too many libs',
1512-
{{{ cDefs.ELIBEXEC }}}: 'Attempting to exec a shared library',
1513-
{{{ cDefs.ENOSYS }}}: 'Function not implemented',
1514-
{{{ cDefs.ENOTEMPTY }}}: 'Directory not empty',
1515-
{{{ cDefs.ENAMETOOLONG }}}: 'File or path name too long',
1516-
{{{ cDefs.ELOOP }}}: 'Too many symbolic links',
1517-
{{{ cDefs.EOPNOTSUPP }}}: 'Operation not supported on transport endpoint',
1518-
{{{ cDefs.EPFNOSUPPORT }}}: 'Protocol family not supported',
1519-
{{{ cDefs.ECONNRESET }}}: 'Connection reset by peer',
1520-
{{{ cDefs.ENOBUFS }}}: 'No buffer space available',
1521-
{{{ cDefs.EAFNOSUPPORT }}}: 'Address family not supported by protocol family',
1522-
{{{ cDefs.EPROTOTYPE }}}: 'Protocol wrong type for socket',
1523-
{{{ cDefs.ENOTSOCK }}}: 'Socket operation on non-socket',
1524-
{{{ cDefs.ENOPROTOOPT }}}: 'Protocol not available',
1525-
{{{ cDefs.ESHUTDOWN }}}: 'Can\'t send after socket shutdown',
1526-
{{{ cDefs.ECONNREFUSED }}}: 'Connection refused',
1527-
{{{ cDefs.EADDRINUSE }}}: 'Address already in use',
1528-
{{{ cDefs.ECONNABORTED }}}: 'Connection aborted',
1529-
{{{ cDefs.ENETUNREACH }}}: 'Network is unreachable',
1530-
{{{ cDefs.ENETDOWN }}}: 'Network interface is not configured',
1531-
{{{ cDefs.ETIMEDOUT }}}: 'Connection timed out',
1532-
{{{ cDefs.EHOSTDOWN }}}: 'Host is down',
1533-
{{{ cDefs.EHOSTUNREACH }}}: 'Host is unreachable',
1534-
{{{ cDefs.EINPROGRESS }}}: 'Connection already in progress',
1535-
{{{ cDefs.EALREADY }}}: 'Socket already connected',
1536-
{{{ cDefs.EDESTADDRREQ }}}: 'Destination address required',
1537-
{{{ cDefs.EMSGSIZE }}}: 'Message too long',
1538-
{{{ cDefs.EPROTONOSUPPORT }}}: 'Unknown protocol',
1539-
{{{ cDefs.ESOCKTNOSUPPORT }}}: 'Socket type not supported',
1540-
{{{ cDefs.EADDRNOTAVAIL }}}: 'Address not available',
1541-
{{{ cDefs.ENETRESET }}}: 'Connection reset by network',
1542-
{{{ cDefs.EISCONN }}}: 'Socket is already connected',
1543-
{{{ cDefs.ENOTCONN }}}: 'Socket is not connected',
1544-
{{{ cDefs.ETOOMANYREFS }}}: 'Too many references',
1545-
{{{ cDefs.EUSERS }}}: 'Too many users',
1546-
{{{ cDefs.EDQUOT }}}: 'Quota exceeded',
1547-
{{{ cDefs.ESTALE }}}: 'Stale file handle',
1548-
{{{ cDefs.ENOTSUP }}}: 'Not supported',
1549-
{{{ cDefs.ENOMEDIUM }}}: 'No medium (in tape drive)',
1550-
{{{ cDefs.EILSEQ }}}: 'Illegal byte sequence',
1551-
{{{ cDefs.EOVERFLOW }}}: 'Value too large for defined data type',
1552-
{{{ cDefs.ECANCELED }}}: 'Operation canceled',
1553-
{{{ cDefs.ENOTRECOVERABLE }}}: 'State not recoverable',
1554-
{{{ cDefs.EOWNERDEAD }}}: 'Previous owner died',
1555-
{{{ cDefs.ESTRPIPE }}}: 'Streams pipe error',
1434+
1435+
#if PURE_WASI
1436+
$strError: (errno) => errno + '',
1437+
#else
1438+
$strError__deps: ['strerror', '$UTF8ToString'],
1439+
$strError: (errno) => {
1440+
return UTF8ToString(_strerror(errno));
15561441
},
1442+
#endif
15571443

15581444
#if PROXY_POSIX_SOCKETS == 0
15591445
// ==========================================================================
@@ -3386,7 +3272,7 @@ function wrapSyscallFunction(x, library, isWasi) {
33863272
pre += "var ret = (() => {";
33873273
post += "})();\n";
33883274
post += "if (ret && ret < 0 && canWarn) {\n";
3389-
post += " dbg(`error: syscall may have failed with ${-ret} (${ERRNO_MESSAGES[-ret]})`);\n";
3275+
post += " dbg(`error: syscall may have failed with ${-ret} (${strError(-ret)})`);\n";
33903276
post += "}\n";
33913277
post += "dbg(`syscall return: ${ret}`);\n";
33923278
post += "return ret;\n";
@@ -3400,7 +3286,7 @@ function wrapSyscallFunction(x, library, isWasi) {
34003286
" if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;\n";
34013287
#if SYSCALL_DEBUG
34023288
handler +=
3403-
" dbg(`error: syscall failed with ${e.errno} (${ERRNO_MESSAGES[e.errno]})`);\n" +
3289+
" dbg(`error: syscall failed with ${e.errno} (${strError(e.errno)})`);\n" +
34043290
" canWarn = false;\n";
34053291
#endif
34063292
// Musl syscalls are negated.

src/library_fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ addToLibrary({
2828
'$PROXYFS',
2929
#endif
3030
#if ASSERTIONS
31-
'$ERRNO_MESSAGES', '$ERRNO_CODES',
31+
'$strError', '$ERRNO_CODES',
3232
#endif
3333
],
3434
$FS__postset: function() {
@@ -80,7 +80,7 @@ FS.staticInit();` +
8080
// we'll use the reliable test `err.name == "ErrnoError"` instead
8181
constructor(errno) {
8282
#if ASSERTIONS
83-
super(ERRNO_MESSAGES[errno]);
83+
super(runtimeInitialized ? strError(errno) : '');
8484
#endif
8585
// TODO(sbc): Use the inline member declaration syntax once we
8686
// support it in acorn and closure.

src/library_syscall.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var SyscallsLibrary = {
1111
'$FS',
1212
#endif
1313
#if SYSCALL_DEBUG
14-
'$ERRNO_MESSAGES'
14+
'$strError',
1515
#endif
1616
],
1717
$SYSCALLS: {

system/lib/libc/musl/src/errno/strerror.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,24 @@ char *__strerror_l(int e, locale_t loc)
3636
#endif
3737
if (e >= sizeof errmsgidx / sizeof *errmsgidx) e = 0;
3838
s = (char *)&errmsgstr + errmsgidx[e];
39+
#ifdef __EMSCRIPTEN__
40+
// strerror is a (debug) dependency of many emscripten syscalls which mean it
41+
// must be excluded from LTO, along with all of its dependencies.
42+
// In order to limit the transitive dependencies we disable localization of
43+
// rrno messages here.
44+
return (char *)s;
45+
#else
3946
return (char *)LCTRANS(s, LC_MESSAGES, loc);
47+
#endif
4048
}
4149

4250
char *strerror(int e)
4351
{
52+
#ifdef __EMSCRIPTEN__
53+
return __strerror_l(e, NULL);
54+
#else
4455
return __strerror_l(e, CURRENT_LOCALE);
56+
#endif
4557
}
4658

4759
weak_alias(__strerror_l, strerror_l);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8017
1+
8014
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
21507
1+
21501
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6584
1+
6581
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17647
1+
17641
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
55036
1+
55030
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
53933
1+
53927

tools/system_libs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,9 +1070,10 @@ def get_libcall_files(self):
10701070
path='system/lib/libc/musl/src/string',
10711071
filenames=['strlen.c'])
10721072

1073+
# Transitively required my message system call imports
10731074
errno_files = files_in_path(
10741075
path='system/lib/libc/musl/src/errno',
1075-
filenames=['__errno_location.c'])
1076+
filenames=['__errno_location.c', 'strerror.c'])
10761077

10771078
return math_files + exit_files + other_files + iprintf_files + errno_files
10781079

0 commit comments

Comments
 (0)