Skip to content

Commit 22f1257

Browse files
WIP : Glibc fixes to fix build errors and undefined symbol errors (#793)
* Added placeholder for libdl.a * Added unwind_def object file for libc.a * Updated s_frexpl.c with latest glibc code to fix the build error * Disable compiler flags -mno-sse, -mno-mmx, -mfpmath-387 that are not supported by wasm and caused build errors * Disable ELF symbol versioning and hidden visibility in glibc shared builds which caused undefined symbol error for pow etc * Removed flags that is not supported in wasm
1 parent a91b430 commit 22f1257

File tree

6 files changed

+72
-74
lines changed

6 files changed

+72
-74
lines changed

scripts/make_archive.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,14 @@ else
115115
echo "Failed to create the archive libpthread.a"
116116
return 1
117117
fi
118+
119+
#libdl.a is created as a placeholder since static python build requires libdl.a
120+
llvm-ar crs "$GLIBC/sysroot/lib/wasm32-wasi/libdl.a"
121+
122+
# Check if llvm-ar succeeded
123+
if [ $? -eq 0 ]; then
124+
echo "SUCCESS: Created archive libdl.a"
125+
else
126+
echo "Failed to create the archive libdl.a"
127+
return 1
128+
fi

scripts/make_glibc_and_sysroot.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SYSROOT_ARCHIVE="$SYSROOT/lib/wasm32-wasi/libc.a"
1717
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
1818

1919
# Define common flags
20-
CFLAGS="--target=wasm32-unknown-wasi -v -Wno-int-conversion -std=gnu11 -fgnu89-inline -matomics -mbulk-memory -O2 -g -fPIC"
20+
CFLAGS="--target=wasm32-unknown-wasi -v -Wno-int-conversion -DNO_HIDDEN -std=gnu11 -fgnu89-inline -matomics -mbulk-memory -O2 -g -fPIC"
2121
WARNINGS="-Wall -Wwrite-strings -Wundef -Wstrict-prototypes -Wold-style-definition"
2222
EXTRA_FLAGS="-fmerge-all-constants -ftrapping-math -fno-stack-protector -fno-common"
2323
EXTRA_FLAGS+=" -Wp,-U_FORTIFY_SOURCE -fmath-errno -fPIE -ftls-model=local-exec"

scripts/object_lists/libc_objects.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,7 @@ misc/tsearch.o
16481648
misc/ttyslot.o
16491649
misc/ualarm.o
16501650
misc/unwind-link.o
1651+
misc/unwind_def.o
16511652
misc/usleep.o
16521653
misc/ustat.o
16531654
misc/utimes.o

src/glibc/include/libc-symbols.h

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -320,31 +320,16 @@ for linking")
320320
past the last element in SET. */
321321
#define symbol_set_end_p(set, ptr) ((ptr) >= (void *const *) &__stop_##set)
322322

323-
#ifdef SHARED
324-
# define symbol_version(real, name, version) \
325-
symbol_version_reference(real, name, version)
326-
# define default_symbol_version(real, name, version) \
327-
_default_symbol_version(real, name, version)
328-
/* See <libc-symver.h>. */
329-
# ifdef __ASSEMBLER__
330-
# define _default_symbol_version(real, name, version) \
331-
_set_symbol_version (real, name@@version)
332-
# else
333-
# define _default_symbol_version(real, name, version) \
334-
_set_symbol_version (real, #name "@@" #version)
335-
# endif
336-
337-
/* Evaluates to a string literal for VERSION in LIB. */
338-
# define symbol_version_string(lib, version) \
339-
_symbol_version_stringify_1 (VERSION_##lib##_##version)
340-
# define _symbol_version_stringify_1(arg) _symbol_version_stringify_2 (arg)
341-
# define _symbol_version_stringify_2(arg) #arg
323+
/* Completely disable ELF symbol versioning for all builds */
324+
#define symbol_version(real, name, version)
325+
#define default_symbol_version(real, name, version) \
326+
strong_alias(real, name)
342327

343-
#else /* !SHARED */
344-
# define symbol_version(real, name, version)
345-
# define default_symbol_version(real, name, version) \
328+
/* Provide dummy fallbacks for internal macros to prevent undefined macro errors */
329+
#define _default_symbol_version(real, name, version) \
346330
strong_alias(real, name)
347-
#endif
331+
#define symbol_version_string(lib, version) #version
332+
348333

349334
#if defined SHARED || defined LIBC_NONSHARED \
350335
|| (BUILD_PIE_DEFAULT && IS_IN (libc))

src/glibc/sysdeps/i386/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ endif
4848
# the first 3 mm/xmm/ymm/zmm registers are used to pass vector parameters
4949
# which must be preserved.
5050
# With SSE disabled, ensure -fpmath is not set to use sse either.
51-
rtld-CFLAGS += -mno-sse -mno-mmx -mfpmath=387
51+
# Flags -mno-sse -mno-mmx -mfpmath=387 has been removed as they
52+
# are not supported by wasm
5253
ifeq ($(subdir),elf)
5354
CFLAGS-.os += $(if $(filter $(@F),$(patsubst %,%.os,$(all-rtld-routines))),\
5455
$(rtld-CFLAGS))
Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
1-
/* s_frexpl.c -- long double version of s_frexp.c.
2-
*/
3-
4-
/*
5-
* ====================================================
6-
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7-
*
8-
* Developed at SunPro, a Sun Microsystems, Inc. business.
9-
* Permission to use, copy, modify, and distribute this
10-
* software is freely granted, provided that this notice
11-
* is preserved.
12-
* ====================================================
13-
*/
14-
15-
#if defined(LIBM_SCCS) && !defined(lint)
16-
static char rcsid[] = "$NetBSD: $";
17-
#endif
18-
19-
/*
20-
* for non-zero x
21-
* x = frexpl(arg,&exp);
22-
* return a long double fp quantity x such that 0.5 <= |x| <1.0
23-
* and the corresponding binary exponent "exp". That is
24-
* arg = x*2^exp.
25-
* If arg is inf, 0.0, or NaN, then frexpl(arg,&exp) returns arg
26-
* with *exp=0.
27-
*/
1+
/* Optimized frexp implementation for intel96 (x87 80-bit).
2+
Copyright (C) 2025-2026 Free Software Foundation, Inc.
3+
This file is part of the GNU C Library.
4+
5+
The GNU C Library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
The GNU C Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with the GNU C Library; if not, see
17+
<https://www.gnu.org/licenses/>. */
2818

2919
#include <float.h>
3020
#include <math.h>
3121
#include <math_private.h>
3222
#include <libm-alias-ldouble.h>
3323

34-
static const long double
35-
#if LDBL_MANT_DIG == 64
36-
two65 = 3.68934881474191032320e+19L; /* 0x4040, 0x80000000, 0x00000000 */
37-
#else
38-
# error "Cannot handle this MANT_DIG"
39-
#endif
24+
#define EXPONENT_BIAS 16383
4025

26+
static const long double two65 = 0x1p65L;
4127

42-
long double __frexpl(long double x, int *eptr)
28+
long double
29+
__frexpl (long double x, int *eptr)
4330
{
44-
uint32_t se, hx, ix, lx;
45-
GET_LDOUBLE_WORDS(se,hx,lx,x);
46-
ix = 0x7fff&se;
47-
*eptr = 0;
48-
if(ix==0x7fff||((ix|hx|lx)==0)) return x + x; /* 0,inf,nan */
49-
if (ix==0x0000) { /* subnormal */
50-
x *= two65;
51-
GET_LDOUBLE_EXP(se,x);
52-
ix = se&0x7fff;
53-
*eptr = -65;
54-
}
55-
*eptr += ix-16382;
56-
se = (se & 0x8000) | 0x3ffe;
57-
SET_LDOUBLE_EXP(x,se);
58-
return x;
31+
uint32_t se, hx, lx;
32+
GET_LDOUBLE_WORDS (se, hx, lx, x);
33+
uint32_t ex = se & 0x7fff;
34+
35+
/* Fast path for normal numbers. */
36+
if (__glibc_likely ((ex - 1) < 0x7ffe))
37+
{
38+
ex -= EXPONENT_BIAS - 1;
39+
*eptr = ex;
40+
SET_LDOUBLE_EXP (x, se - ex);
41+
return x;
42+
}
43+
44+
/* Handle zero, infinity, and NaN. */
45+
if (__glibc_likely (ex == 0x7fff || (ex | hx | lx) == 0))
46+
{
47+
*eptr = 0;
48+
return x + x;
49+
}
50+
51+
/* Subnormal. */
52+
x *= two65;
53+
GET_LDOUBLE_EXP (se, x);
54+
55+
ex = (se & 0x7fff) - EXPONENT_BIAS + 1;
56+
*eptr = ex - 65;
57+
SET_LDOUBLE_EXP (x, se - ex);
58+
return x;
5959
}
6060
libm_alias_ldouble (__frexp, frexp)

0 commit comments

Comments
 (0)