Skip to content

Commit

Permalink
Clean up some code
Browse files Browse the repository at this point in the history
- Use good ELF technique in cosmo_dlopen()
- Make strerror() conform more to other libc impls
- Introduce __clear_cache() and use it in cosmo_dlopen()
- Remove libc/fmt/fmt.h header (trying to kill off LIBC_FMT)
  • Loading branch information
jart committed Nov 17, 2023
1 parent 7010a80 commit 68c7c9c
Show file tree
Hide file tree
Showing 244 changed files with 378 additions and 588 deletions.
1 change: 0 additions & 1 deletion dsp/tty/describe.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "dsp/tty/tty.h"
#include "libc/fmt/fmt.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
Expand Down
2 changes: 1 addition & 1 deletion examples/dlopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "libc/calls/calls.h"
#include "libc/dlopen/dlfcn.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/runtime/runtime.h"

Expand All @@ -24,6 +23,7 @@
* - x86-64 FreeBSD
* - x86-64 Windows
* - aarch64 Linux w/ Glibc
* - aarch64 Linux w/ Musl Libc
* - aarch64 MacOS
*
*/
Expand Down
1 change: 0 additions & 1 deletion examples/hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#endif
#include "libc/runtime/runtime.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "third_party/hiredis/hiredis.h"
Expand Down
1 change: 0 additions & 1 deletion examples/kilo.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ Contact: [email protected]\"\n\
#include "libc/calls/termios.h"
#include "libc/calls/weirdtypes.h"
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/log/log.h"
#include "libc/mem/alg.h"
#include "libc/mem/arraylist2.internal.h"
Expand Down
1 change: 0 additions & 1 deletion examples/nesemu1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/inttypes.h"
Expand Down
1 change: 0 additions & 1 deletion examples/picol.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
*/

#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
Expand Down
1 change: 0 additions & 1 deletion examples/portscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "libc/calls/struct/timeval.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/mem/alloca.h"
#include "libc/runtime/runtime.h"
Expand Down
1 change: 0 additions & 1 deletion examples/stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/mem/gc.h"
Expand Down
1 change: 0 additions & 1 deletion examples/stringbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
╚─────────────────────────────────────────────────────────────────*/
#endif
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/log/check.h"
#include "libc/mem/mem.h"
#include "libc/stdio/append.h"
Expand Down
1 change: 0 additions & 1 deletion examples/ttyinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "libc/calls/struct/sigset.h"
#include "libc/calls/termios.h"
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/runtime/runtime.h"
Expand Down
1 change: 0 additions & 1 deletion examples/unbourne.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/limits.h"
#include "libc/log/log.h"
Expand Down
1 change: 0 additions & 1 deletion examples/wall.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "libc/calls/struct/timespec.h"
#include "libc/calls/termios.h"
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/append.h"
#include "libc/stdio/stdio.h"
Expand Down
1 change: 0 additions & 1 deletion libc/calls/ntaccesscheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/mem/mem.h"
Expand Down
1 change: 0 additions & 1 deletion libc/calls/ttyname_r.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/intrin/strace.internal.h"
Expand Down
52 changes: 34 additions & 18 deletions libc/dlopen/dlopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigset.internal.h"
Expand All @@ -34,8 +35,8 @@
#include "libc/elf/struct/phdr.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.internal.h"
#include "libc/limits.h"
#include "libc/nt/dll.h"
Expand Down Expand Up @@ -69,11 +70,6 @@
* @kudos jacereda for figuring out how to do this
*/

#define XNU_RTLD_LAZY 1
#define XNU_RTLD_NOW 2
#define XNU_RTLD_LOCAL 4
#define XNU_RTLD_GLOBAL 8

#define AMD_REXB 0x41
#define AMD_REXW 0x48
#define AMD_MOV_IMM 0xb8
Expand All @@ -83,11 +79,16 @@
#define ARM_IDX_OFF 21
#define ARM_MOV_NEX 0xf2800000u

#define XNU_RTLD_LAZY 1
#define XNU_RTLD_NOW 2
#define XNU_RTLD_LOCAL 4
#define XNU_RTLD_GLOBAL 8

#define HELPER \
"#include <dlfcn.h>\n\
#include <stdio.h>\n\
#include <stdlib.h>\n\
int main(int argc, char *argv[]) {\n\
int main(int argc, char **argv, char **envp) {\n\
char *ep;\n\
long addr;\n\
if (argc != 2) {\n\
Expand Down Expand Up @@ -201,7 +202,7 @@ static char *elf_map(int fd, Elf64_Ehdr *ehdr, Elf64_Phdr *phdr, long pagesz,
char *interp_path, size_t interp_size) {
Elf64_Addr maxva = 0;
Elf64_Addr minva = -1;
for (Elf64_Phdr *p = phdr; p < &phdr[ehdr->e_phnum]; p++) {
for (Elf64_Phdr *p = phdr; p < phdr + ehdr->e_phnum; p++) {
if (p->p_type != PT_LOAD) {
continue;
}
Expand All @@ -227,18 +228,31 @@ static char *elf_map(int fd, Elf64_Ehdr *ehdr, Elf64_Phdr *phdr, long pagesz,
}
continue;
}
int prot = elf2prot(p->p_flags);
Elf64_Addr skew = p->p_vaddr & (pagesz - 1);
Elf64_Off off = p->p_offset - skew;
if (__sys_mmap(base + p->p_vaddr - skew, skew + p->p_filesz, prot,
Elf64_Addr a = p->p_vaddr + p->p_filesz;
Elf64_Addr b = (a + (pagesz - 1)) & -pagesz;
Elf64_Addr c = p->p_vaddr + p->p_memsz;
int prot2 = elf2prot(p->p_flags);
int prot1 = prot2;
if (b > a) {
prot1 |= PROT_WRITE;
prot1 &= ~PROT_EXEC;
}
if (__sys_mmap(base + p->p_vaddr - skew, skew + p->p_filesz, prot1,
MAP_FIXED | MAP_PRIVATE, fd, off, off) == MAP_FAILED) {
return MAP_FAILED;
}
Elf64_Addr fend = (p->p_vaddr + p->p_filesz + (pagesz - 1)) & -pagesz;
Elf64_Addr mend = p->p_vaddr + p->p_memsz;
if (mend > fend && __sys_mmap(base + fend, mend - fend, prot,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1,
0, 0) == MAP_FAILED) {
if (b > a) {
bzero(base + a, b - a);
}
if (c > b && __sys_mmap(base + b, c - b, prot2,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0,
0) == MAP_FAILED) {
return MAP_FAILED;
}
if (prot1 != prot2 &&
sys_mprotect(base + p->p_vaddr - skew, skew + p->p_filesz, prot2)) {
return MAP_FAILED;
}
}
Expand Down Expand Up @@ -302,14 +316,14 @@ static dontinline void elf_exec(const char *file, char **envp) {
// get microprocessor page size
long pagesz = getauxval(AT_PAGESZ);

// load executable
// load helper executable into address space
struct Loaded prog;
char interp_path[256] = {0};
if (!elf_load(&prog, file, pagesz, interp_path, sizeof(interp_path))) {
return;
}

// load platform libc
// load platform c library into address space
struct Loaded interp;
if (!elf_load(&interp, interp_path, pagesz, 0, 0)) {
return;
Expand Down Expand Up @@ -343,6 +357,7 @@ static dontinline void elf_exec(const char *file, char **envp) {
FormatInt64(address_argument, (uintptr_t)foreign_helper);

// push auxiliary values
// these tell the platform libc how to load the executable
*--sp = 0;
unsigned long key, val;
for (av = (Elf64_auxv_t *)__auxv; (key = av->a_type); ++av) {
Expand Down Expand Up @@ -508,7 +523,8 @@ static void *foreign_thunk_sysv(void *func) {
if (!(p = code = foreign_alloc(36))) return 0; // 16 + 16 + 4 = 36
p = movimm(p, 5, (uintptr_t)func);
p = movimm(p, 10, (uintptr_t)foreign_tramp);
*(uint32_t *)p = 0xd63f0140; // blr x10
*(uint32_t *)p = 0xd61f0140; // br x10
__clear_cache(code, p + 4);
#else
#error "unsupported architecture"
#endif
Expand Down
1 change: 1 addition & 0 deletions libc/dlopen/dlopen.mk
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ LIBC_DLOPEN_A_DIRECTDEPS = \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_STR \
THIRD_PARTY_COMPILER_RT

LIBC_DLOPEN_A_DEPS := \
$(call uniq,$(foreach x,$(LIBC_DLOPEN_A_DIRECTDEPS),$($(x))))
Expand Down
1 change: 0 additions & 1 deletion libc/dns/gethoststxt.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "libc/dce.h"
#include "libc/dns/hoststxt.h"
#include "libc/dns/servicestxt.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/pushpop.internal.h"
#include "libc/intrin/safemacros.internal.h"
Expand Down
1 change: 0 additions & 1 deletion libc/dns/getnameinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "libc/dns/resolvconf.h"
#include "libc/dns/servicestxt.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/mem/mem.h"
Expand Down
1 change: 0 additions & 1 deletion libc/dns/getresolvconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "libc/cxxabi.h"
#include "libc/dce.h"
#include "libc/dns/resolvconf.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/pushpop.internal.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
Expand Down
1 change: 0 additions & 1 deletion libc/dns/resolvehostsreverse.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "libc/dns/consts.h"
#include "libc/dns/dns.h"
#include "libc/dns/hoststxt.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/bits.h"
#include "libc/mem/alg.h"
#include "libc/sock/sock.h"
Expand Down
6 changes: 3 additions & 3 deletions libc/fmt/__xpg_strerror_r.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/fmt/fmt.h"
#include "libc/str/str.h"

int __xpg_strerror_r(int a, char *b, size_t c) {
return strerror_r(a, b, c);
char *__xpg_strerror_r(int a, char *b, size_t c) {
strerror_r(a, b, c);
return b;
}
5 changes: 2 additions & 3 deletions libc/fmt/atoi.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@
*/
int atoi(const char *s) {
int x, c, d;
do {
c = *s++;
} while (c == ' ' || c == '\t');
do c = *s++;
while (c == ' ' || c == '\t');
d = c == '-' ? -1 : 1;
if (c == '-' || c == '+') c = *s++;
for (x = 0; isdigit(c); c = *s++) {
Expand Down
5 changes: 2 additions & 3 deletions libc/fmt/atol.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
long atol(const char *s) {
long x;
int c, d;
do {
c = *s++;
} while (c == ' ' || c == '\t');
do c = *s++;
while (c == ' ' || c == '\t');
d = c == '-' ? -1 : 1;
if (c == '-' || c == '+') c = *s++;
for (x = 0; isdigit(c); c = *s++) {
Expand Down
11 changes: 0 additions & 11 deletions libc/fmt/conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,6 @@ imaxdiv_t imaxdiv(intmax_t, intmax_t) pureconst;
#define lldiv(num, den) ((lldiv_t){(num) / (den), (num) % (den)})
#endif

#if (__GNUC__ * 100 + __GNUC_MINOR__ >= 406 || defined(__llvm__)) && \
!defined(__STRICT_ANSI__) && defined(_COSMO_SOURCE) && \
!defined(__COSMOCC__)
int128_t i128abs(int128_t)
libcesque pureconst;
int128_t strtoi128(const char *, char **, int) paramsnonnull((1));
uint128_t strtou128(const char *, char **, int) paramsnonnull((1));
int128_t wcstoi128(const wchar_t *, wchar_t **, int);
uint128_t wcstou128(const wchar_t *, wchar_t **, int);
#endif /* gcc 4.6+ */

COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_FMT_CONV_H_ */
2 changes: 1 addition & 1 deletion libc/fmt/divmod10.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ forceinline uint64_t __divmod10(uint64_t __x, unsigned *__r) {
*__r = __x % 10;
return __x / 10;
#else
uint128_t __dw;
unsigned __int128 __dw;
unsigned long long __hi, __rm;
__dw = __x;
__dw *= 0xcccccccccccccccdull;
Expand Down
25 changes: 0 additions & 25 deletions libc/fmt/fmt.h

This file was deleted.

11 changes: 0 additions & 11 deletions libc/fmt/fmt.mk
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,6 @@ o/$(MODE)/libc/fmt/wcstoumax.o: private \
CFLAGS += \
-Os

# we can't use compiler magic because:
# kprintf() depends on these functions
o/$(MODE)/libc/fmt/strerrno.greg.o \
o/$(MODE)/libc/fmt/strerrdoc.greg.o \
o/$(MODE)/libc/fmt/strerror_wr.greg.o: private \
COPTS += \
-fpie \
-fno-sanitize=all \
-fno-stack-protector \
-fpatchable-function-entry=0,0

LIBC_FMT_LIBS = $(foreach x,$(LIBC_FMT_ARTIFACTS),$($(x)))
LIBC_FMT_SRCS = $(foreach x,$(LIBC_FMT_ARTIFACTS),$($(x)_SRCS))
LIBC_FMT_HDRS = $(foreach x,$(LIBC_FMT_ARTIFACTS),$($(x)_HDRS))
Expand Down
Loading

0 comments on commit 68c7c9c

Please sign in to comment.