Make improvements

This commit is contained in:
Justine Tunney 2020-12-01 03:43:40 -08:00
parent 3e4fd4b0ad
commit e44a0cf6f8
256 changed files with 23100 additions and 2294 deletions

View file

@ -22,7 +22,6 @@
#include "libc/bits/pushpop.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/runtime/internal.h"
#include "libc/str/appendchar.h"
#include "libc/str/str.h"
#include "libc/str/tpenc.h"
#include "libc/str/utf16.h"
@ -34,8 +33,31 @@ struct DosArgv {
wint_t wc;
};
static textwindows wint_t DecodeDosArgv(const char16_t **s) {
wint_t x, y;
for (;;) {
if (!(x = *(*s)++)) break;
if (IsUtf16Cont(x)) continue;
if (IsUcs2(x)) {
return x;
} else {
if ((y = *(*s)++)) {
return MergeUtf16(x, y);
} else {
return 0;
}
}
}
return x;
}
static textwindows void AppendDosArgv(struct DosArgv *st, wint_t wc) {
AppendChar(&st->p, st->pe, wc);
uint64_t w;
w = tpenc(wc);
do {
if (st->p >= st->pe) break;
*st->p++ = w & 0xff;
} while (w >>= 8);
}
/**
@ -66,9 +88,9 @@ textwindows int GetDosArgv(const char16_t *cmdline, char *buf, size_t size,
st.p = buf;
st.pe = buf + size;
argc = 0;
st.wc = DecodeNtsUtf16(&st.s);
st.wc = DecodeDosArgv(&st.s);
while (st.wc) {
while (st.wc && iswspace(st.wc)) st.wc = DecodeNtsUtf16(&st.s);
while (st.wc && isspace(st.wc)) st.wc = DecodeDosArgv(&st.s);
if (!st.wc) break;
if (++argc < max) {
argv[argc - 1] = st.p < st.pe ? st.p : NULL;
@ -79,8 +101,8 @@ textwindows int GetDosArgv(const char16_t *cmdline, char *buf, size_t size,
if (st.wc == '"' || st.wc == '\\') {
slashes = 0;
quotes = 0;
while (st.wc == '\\') st.wc = DecodeNtsUtf16(&st.s), slashes++;
while (st.wc == '"') st.wc = DecodeNtsUtf16(&st.s), quotes++;
while (st.wc == '\\') st.wc = DecodeDosArgv(&st.s), slashes++;
while (st.wc == '"') st.wc = DecodeDosArgv(&st.s), quotes++;
if (!quotes) {
while (slashes--) AppendDosArgv(&st, '\\');
} else {
@ -94,7 +116,7 @@ textwindows int GetDosArgv(const char16_t *cmdline, char *buf, size_t size,
}
} else {
AppendDosArgv(&st, st.wc);
st.wc = DecodeNtsUtf16(&st.s);
st.wc = DecodeDosArgv(&st.s);
}
}
AppendDosArgv(&st, '\0');

View file

@ -17,26 +17,35 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/dce.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
/**
* Applies fixups to 64-bit rela addresses in binary.
* Transcodes NT environment variable block from UTF-16 to UTF-8.
*
* @param env is a double NUL-terminated block of key=values
* @param buf is the new environment which gets double-nul'd
* @param size is the byte capacity of buf
* @param envp stores NULL-terminated string pointer list (optional)
* @param max is the pointer count capacity of envp
* @return number of variables decoded, excluding NULL-terminator
*/
void __relocate(void) {
ptrdiff_t skew;
unsigned char **p;
p = __relo_start;
if (p != __relo_end) {
skew = (intptr_t)p - (intptr_t)*p;
do {
if (*p) {
*p += skew;
if (!NoDebug() && !(_base <= *p && *p < _end)) {
asm("int3");
}
}
} while (++p != __relo_end);
textwindows int GetDosEnviron(const char16_t *env, char *buf, size_t size,
char **envp, size_t max) {
int i;
axdx_t r;
i = 0;
if (size) {
--size;
while (*env) {
if (i + 1 < max) envp[i++] = buf;
r = tprecode16to8(buf, size, env);
size -= r.ax + 1;
buf += r.ax + 1;
env += r.dx;
}
*buf = '\0';
}
return;
if (i < max) envp[i] = NULL;
return i;
}

View file

@ -1,55 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_DOSENVIRON_H_
#define COSMOPOLITAN_LIBC_DOSENVIRON_H_
#ifndef __STRICT_ANSI__
#include "libc/bits/safemacros.internal.h"
#include "libc/str/appendchar.h"
#include "libc/str/str.h"
#include "libc/str/utf16.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/**
* Transcodes NT environment variable block from UTF-16 to UTF-8.
*
* @param env is a double NUL-terminated block of key=values
* @param buf is the new environment
* @param size is the byte capacity of buf
* @param envp stores NULL-terminated string pointer list
* @param max is the pointer count capacity of envp
* @return number of variables decoded, excluding NULL-terminator
*/
static inline int GetDosEnviron(const char16_t *env, char *buf, size_t size,
char **envp, size_t max) {
wint_t wc;
size_t envc;
char *p, *pe;
bool endstring;
const char16_t *s;
s = env;
envc = 0;
if (size) {
p = buf;
pe = buf + size - 1;
if (p < pe) {
wc = DecodeNtsUtf16(&s);
while (wc) {
if (++envc < max) {
envp[envc - 1] = p < pe ? p : NULL;
}
do {
AppendChar(&p, pe, wc);
endstring = !wc;
wc = DecodeNtsUtf16(&s);
} while (!endstring);
buf[min(p - buf, size - 2)] = u'\0';
}
}
AppendChar(&p, pe, '\0');
buf[min(p - buf, size - 1)] = u'\0';
}
if (max) envp[min(envc, max - 1)] = NULL;
return envc;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* !ANSI */
#endif /* COSMOPOLITAN_LIBC_DOSENVIRON_H_ */

View file

@ -45,7 +45,7 @@
/ @param r15 is envp (still callee saved)
/ @note rdi is __init_bss_start (callee monotonic lockstep)
/ @note rsi is __init_rodata_start (callee monotonic lockstep)
/ @see .init.start & .init.end (libc/macros.inc)
/ @see .init.start & .init.end (libc/macros.internal.inc)
/ @see ape/ape.lds
.section .initprologue,"ax",@progbits
.type _init,@function
@ -78,7 +78,7 @@ _woot: leave
/ Decentralized section for packed data structures & initializers.
/
/ @see .initro (libc/macros.inc)
/ @see .initro (libc/macros.internal.inc)
/ @see ape/ape.lds
.section .initroprologue,"a",@progbits
.type __init_rodata_start,@object
@ -100,7 +100,7 @@ __init_rodata_end:
/
/ Data in this section becomes read-only after initialization.
/
/ @see .piro.bss.init (libc/macros.inc)
/ @see .piro.bss.init (libc/macros.internal.inc)
/ @see libc/runtime/piro.c
/ @see ape/ape.lds
.section .piro.bss.init.1,"aw",@nobits

View file

@ -26,6 +26,7 @@ void _jmpstack(void *, void *, ...) hidden noreturn;
long _setstack(void *, void *, ...) hidden;
int GetDosArgv(const char16_t *, char *, size_t, char **, size_t) hidden;
Elf64_Ehdr *MapElfRead(const char *, struct MappedFile *) hidden;
int GetDosEnviron(const char16_t *, char *, size_t, char **, size_t) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -1,21 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_RBX_H_
#define COSMOPOLITAN_LIBC_RUNTIME_RBX_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#if 0
#ifndef __llvm__
register uint8_t *__rbx asm("rbx");
#else
#define __rbx \
({ \
register uint8_t *Rbx asm("rbx"); \
asm("" : "=r"(Rbx)); \
Rbx; \
})
#endif
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_RBX_H_ */

View file

@ -37,7 +37,6 @@
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/teb.h"
#include "libc/runtime/getdosenviron.internal.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/runtime.h"
@ -130,8 +129,7 @@ static textwindows noreturn void WinMainNew(void) {
NormalizeCmdExe();
*(/*unconst*/ int *)&__hostos = WINDOWS;
size = ROUNDUP(STACKSIZE + sizeof(struct WinArgs), FRAMESIZE);
data = 0x777000000000;
data = (intptr_t)AllocateMemory((char *)data, size, &_mmi.p[0].h);
data = (intptr_t)AllocateMemory((char *)0x777000000000, size, &_mmi.p[0].h);
_mmi.p[0].x = data >> 16;
_mmi.p[0].y = (data >> 16) + ((size >> 16) - 1);
_mmi.p[0].prot = PROT_READ | PROT_WRITE;