mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
957c61cbbf
This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker appears to have changed things so that only a single de-duplicated str table is present in the binary, and it gets placed wherever the linker wants, regardless of what the linker script says. To cope with that we need to stop using .ident to embed licenses. As such, this change does significant work to revamp how third party licenses are defined in the codebase, using `.section .notice,"aR",@progbits`. This new GCC 12.3 toolchain has support for GNU indirect functions. It lets us support __target_clones__ for the first time. This is used for optimizing the performance of libc string functions such as strlen and friends so far on x86, by ensuring AVX systems favor a second codepath that uses VEX encoding. It shaves some latency off certain operations. It's a useful feature to have for scientific computing for the reasons explained by the test/libcxx/openmp_test.cc example which compiles for fifteen different microarchitectures. Thanks to the upgrades, it's now also possible to use newer instruction sets, such as AVX512FP16, VNNI. Cosmo now uses the %gs register on x86 by default for TLS. Doing it is helpful for any program that links `cosmo_dlopen()`. Such programs had to recompile their binaries at startup to change the TLS instructions. That's not great, since it means every page in the executable needs to be faulted. The work of rewriting TLS-related x86 opcodes, is moved to fixupobj.com instead. This is great news for MacOS x86 users, since we previously needed to morph the binary every time for that platform but now that's no longer necessary. The only platforms where we need fixup of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the kernels do not allow us to specify a value for the %gs register. OpenBSD users are now required to use APE Loader to run Cosmo binaries and assimilation is no longer possible. OpenBSD kernel needs to change to allow programs to specify a value for the %gs register, or it needs to stop marking executable pages loaded by the kernel as mimmutable(). This release fixes __constructor__, .ctor, .init_array, and lastly the .preinit_array so they behave the exact same way as glibc. We no longer use hex constants to define math.h symbols like M_PI.
124 lines
3.8 KiB
C++
124 lines
3.8 KiB
C++
|
|
/* ---------------------------- setting mparams -------------------------- */
|
|
|
|
#if LOCK_AT_FORK
|
|
static void dlmalloc_pre_fork(void) { ACQUIRE_LOCK(&(gm)->mutex); }
|
|
static void dlmalloc_post_fork_parent(void) { RELEASE_LOCK(&(gm)->mutex); }
|
|
static void dlmalloc_post_fork_child(void) { (void)INITIAL_LOCK(&(gm)->mutex); }
|
|
#endif /* LOCK_AT_FORK */
|
|
|
|
/* Initialize mparams */
|
|
__attribute__((__constructor__(50))) int init_mparams(void) {
|
|
#ifdef NEED_GLOBAL_LOCK_INIT
|
|
if (malloc_global_mutex_status <= 0)
|
|
init_malloc_global_mutex();
|
|
#endif
|
|
|
|
// ACQUIRE_MALLOC_GLOBAL_LOCK();
|
|
if (mparams.magic == 0) {
|
|
size_t magic;
|
|
size_t psize;
|
|
size_t gsize;
|
|
|
|
#if defined(__COSMOPOLITAN__)
|
|
psize = FRAMESIZE;
|
|
gsize = FRAMESIZE;
|
|
#elif !defined(WIN32)
|
|
psize = malloc_getpagesize;
|
|
gsize = ((DEFAULT_GRANULARITY != 0)? DEFAULT_GRANULARITY : psize);
|
|
#else /* WIN32 */
|
|
{
|
|
SYSTEM_INFO system_info;
|
|
GetSystemInfo(&system_info);
|
|
psize = system_info.dwPageSize;
|
|
gsize = ((DEFAULT_GRANULARITY != 0)?
|
|
DEFAULT_GRANULARITY : system_info.dwAllocationGranularity);
|
|
}
|
|
#endif /* WIN32 */
|
|
|
|
/* Sanity-check configuration:
|
|
size_t must be unsigned and as wide as pointer type.
|
|
ints must be at least 4 bytes.
|
|
alignment must be at least 8.
|
|
Alignment, min chunk size, and page size must all be powers of 2.
|
|
*/
|
|
if ((sizeof(size_t) != sizeof(char*)) ||
|
|
(MAX_SIZE_T < MIN_CHUNK_SIZE) ||
|
|
(sizeof(int) < 4) ||
|
|
(MALLOC_ALIGNMENT < (size_t)8U) ||
|
|
((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT-SIZE_T_ONE)) != 0) ||
|
|
((MCHUNK_SIZE & (MCHUNK_SIZE-SIZE_T_ONE)) != 0) ||
|
|
((gsize & (gsize-SIZE_T_ONE)) != 0) ||
|
|
((psize & (psize-SIZE_T_ONE)) != 0))
|
|
ABORT;
|
|
mparams.granularity = gsize;
|
|
mparams.page_size = psize;
|
|
mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
|
|
mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD;
|
|
#if MORECORE_CONTIGUOUS
|
|
mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT;
|
|
#else /* MORECORE_CONTIGUOUS */
|
|
mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT;
|
|
#endif /* MORECORE_CONTIGUOUS */
|
|
|
|
#if !ONLY_MSPACES
|
|
/* Set up lock for main malloc area */
|
|
gm->mflags = mparams.default_mflags;
|
|
(void)INITIAL_LOCK(&gm->mutex);
|
|
#endif
|
|
|
|
#if LOCK_AT_FORK
|
|
pthread_atfork(&dlmalloc_pre_fork,
|
|
&dlmalloc_post_fork_parent,
|
|
&dlmalloc_post_fork_child);
|
|
#endif
|
|
|
|
{
|
|
#if USE_DEV_RANDOM
|
|
int fd;
|
|
unsigned char buf[sizeof(size_t)];
|
|
/* Try to use /dev/urandom, else fall back on using time */
|
|
if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 &&
|
|
read(fd, buf, sizeof(buf)) == sizeof(buf)) {
|
|
magic = *((size_t *) buf);
|
|
close(fd);
|
|
}
|
|
else
|
|
#endif /* USE_DEV_RANDOM */
|
|
magic = (size_t)(_rand64() ^ (size_t)0x55555555U);
|
|
magic |= (size_t)8U; /* ensure nonzero */
|
|
magic &= ~(size_t)7U; /* improve chances of fault for bad values */
|
|
/* Until memory modes commonly available, use volatile-write */
|
|
(*(volatile size_t *)(&(mparams.magic))) = magic;
|
|
}
|
|
}
|
|
|
|
// RELEASE_MALLOC_GLOBAL_LOCK();
|
|
|
|
__runlevel = RUNLEVEL_MALLOC;
|
|
return 1;
|
|
}
|
|
|
|
/* support for mallopt */
|
|
static int change_mparam(int param_number, int value) {
|
|
size_t val;
|
|
ensure_initialization();
|
|
val = (value == -1)? MAX_SIZE_T : (size_t)value;
|
|
switch(param_number) {
|
|
case M_TRIM_THRESHOLD:
|
|
mparams.trim_threshold = val;
|
|
return 1;
|
|
case M_GRANULARITY:
|
|
if (val >= mparams.page_size && ((val & (val-1)) == 0)) {
|
|
mparams.granularity = val;
|
|
return 1;
|
|
}
|
|
else
|
|
return 0;
|
|
case M_MMAP_THRESHOLD:
|
|
mparams.mmap_threshold = val;
|
|
return 1;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|