Skip to content

Commit 62a97c9

Browse files
committed
Fix typos in APE specification
Fixes #1244
1 parent 16d2446 commit 62a97c9

File tree

2 files changed

+27
-37
lines changed

2 files changed

+27
-37
lines changed

ape/specification.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ considered an APE program.
3636

3737
This is the canonical magic used by almost all APE programs. It enables
3838
maximum portability between OSes. When interpreted as a shell script, it
39-
is assiging a single quoted string to an unused variable. The shell will
40-
then ignore subsequent binary content that's placed inside the string.
39+
is assigning a single quoted string to an unused variable. The shell
40+
will then ignore subsequent binary content that's placed inside the
41+
string.
4142

4243
It is strongly recommended that this magic value be immediately followed
4344
by a newline (\n or hex 0a) character. Some shells, e.g. FreeBSD SH and
@@ -167,12 +168,12 @@ printf '\177ELF\2\1\1\011\0\0\0\0\0\0\0\0\2\0\076\0\1\0\0\0\166\105\100\000\000\
167168

168169
This `printf` statement MUST appear in the first 8192 bytes of the APE
169170
executable, so as to limit how much of the initial portion of a file an
170-
intepreter must load.
171+
interpreter must load.
171172

172-
Multiple such `printf` statements MAY appear in hte first 8192 bytes, in
173+
Multiple such `printf` statements MAY appear in the first 8192 bytes, in
173174
order to specify multiple architectures. For example, fat binaries built
174175
by the `apelink` program (provided by Cosmo Libc) will have two encoded
175-
ELF headers, for amd64 and arm64, each of which point into the proper
176+
ELF headers, for AMD64 and ARM64, each of which point into the proper
176177
file offsets for their respective native code. Therefore, kernels and
177178
interpreters which load the APE format directly MUST check the
178179
`e_machine` field of the `Elf64_Ehdr` that's decoded from the octal
@@ -313,7 +314,7 @@ their support vector MUST be compiled using `-mno-red-zone`. This is
313314
because, on Windows, DLLs and other software lurking in the va-space
314315
might use tricks like SetThreadContext() to take control of a thread
315316
whereas on bare metal, it's also generally accepted that kernel-mode
316-
code cannot assume a red zone either due to hardware interrutps that
317+
code cannot assume a red zone either due to hardware interrupts that
317318
pull the exact same kinds of stunts.
318319

319320
APE software that only has truly System V ABI conformant OSes (e.g.
@@ -350,7 +351,7 @@ would be friction-free alternative.
350351

351352
It's not possible for an APE runtime that targets the full range of OSes
352353
to use the `tpidr_el0` register for TLS because Apple won't allow it. On
353-
MacOS ARM64 systems, this reigster can only be used by a runtime to
354+
MacOS ARM64 systems, this register can only be used by a runtime to
354355
implement the `sched_getcpu()` system call. It's reserved by MacOS.
355356

356357
#### x86-64
@@ -441,11 +442,11 @@ static void ChangeTlsFsToGs(unsigned char *p, size_t n) {
441442
}
442443
```
443444
444-
By favoring `%gs` we've now ensured friction-free compatibilty for the
445+
By favoring `%gs` we've now ensured friction-free compatibility for the
445446
APE runtime on MacOS, Linux, and FreeBSD which are all able to conform
446447
easily to this convention. However additional work needs to be done at
447448
runtime when an APE program is started on Windows, OpenBSD, and NetBSD.
448-
On these platforms, all executable pages must be faulted and morped to
449+
On these platforms, all executable pages must be faulted and morphed to
449450
fixup the TLS instructions.
450451
451452
On OpenBSD and NetBSD, this is as simple as undoing the example
@@ -466,7 +467,7 @@ a privileged function, so that it can be used to disable the execute bit
466467
on all other parts of the executable except for the privileged section,
467468
thereby making it writable. Once this has been done, code can change.
468469
469-
On Windows the diplacement bytes of the TLS instruction are changed to
470+
On Windows the displacement bytes of the TLS instruction are changed to
470471
use the `%gs:0x1480+i*8` ABI where `i` is a number assigned by the WIN32
471472
`TlsAlloc()` API. This avoids the need to call `TlsGetValue()` which is
472473
implemented this exact same way under the hood. Even though 0x1480 isn't
@@ -477,7 +478,7 @@ possible, to ensure an index less than 64 is returned.
477478
478479
### Thread Information Block (TIB)
479480
480-
The Actually Portable Exccutable Thread Information Block (TIB) is
481+
The Actually Portable Executable Thread Information Block (TIB) is
481482
defined by this version of the specification as follows:
482483
483484
- The 64-bit TIB self-pointer is stored at offset 0x00.
@@ -520,7 +521,7 @@ Actually Portable Executable defines `char` as signed.
520521
521522
Therefore conformant APE software MUST use `-fsigned-char` when building
522523
code for aarch64, as well as any other architecture that (unlike x86-64)
523-
would otherwise define `char` as being `unsigned char` by deafult.
524+
would otherwise define `char` as being `unsigned char` by default.
524525
525526
This decision was one of the cases where it made sense to offer a more
526527
consistent runtime experience for fat multi-arch binaries. However you
@@ -584,7 +585,7 @@ imposed by the executable formats that APE wraps.
584585
happily map program headers from arbitrary file intervals (which may
585586
overlap) onto arbitrarily virtual intervals (which don't need to be
586587
contiguous). in order to do that, the loaders will generally use
587-
unix's mmap() function which needs to have both page aligned
588+
UNIX's mmap() function which needs to have both page aligned
588589
addresses and file offsets, even though the ELF programs headers
589590
themselves do not. Since program headers start and stop at
590591
potentially any byte, ELF loaders tease the intervals specified by
@@ -595,7 +596,7 @@ imposed by the executable formats that APE wraps.
595596
don't want to; we can simply allow the offset to drift apart from the
596597
virtual offset.
597598

598-
2. PE doesn't care about congruency and instead specifies a second kind
599+
2. PE doesn't care about congruence and instead specifies a second kind
599600
of alignment. The minimum alignment of files is 512 because that's
600601
what MS-DOS used. Where things get hairy is with PE's SizeOfHeaders
601602
which has complex requirements. When the PE image base needs to be
@@ -694,4 +695,4 @@ to the system allocation granularity, which is generally 64kb. If you
694695
use a function like mmap() with Cosmopolitan Libc, then the `addr` and
695696
`offset` parameters need to be aligned to `sysconf(_SC_GRANSIZE)` or
696697
else your software won't work on Windows. Windows has other limitations
697-
too, such as lacking the abiilty to carve or punch holes in mappings.
698+
too, such as lacking the ability to carve or punch holes in mappings.

libc/calls/sig.c

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ static textwindows int __sig_getter(atomic_ulong *sigs, sigset_t masked) {
9696
if ((deliverable = pending & ~masked)) {
9797
sig = bsfl(deliverable) + 1;
9898
bit = 1ull << (sig - 1);
99-
if (atomic_fetch_and_explicit(sigs, ~bit, memory_order_acq_rel) & bit) {
99+
if (atomic_fetch_and_explicit(sigs, ~bit, memory_order_acq_rel) & bit)
100100
return sig;
101-
}
102101
} else {
103102
return 0;
104103
}
@@ -107,28 +106,23 @@ static textwindows int __sig_getter(atomic_ulong *sigs, sigset_t masked) {
107106

108107
textwindows int __sig_get(sigset_t masked) {
109108
int sig;
110-
if (!(sig = __sig_getter(&__get_tls()->tib_sigpending, masked))) {
109+
if (!(sig = __sig_getter(&__get_tls()->tib_sigpending, masked)))
111110
sig = __sig_getter(&__sig.pending, masked);
112-
}
113111
return sig;
114112
}
115113

116114
static textwindows bool __sig_should_use_altstack(unsigned flags,
117115
struct CosmoTib *tib) {
118-
if (!(flags & SA_ONSTACK)) {
116+
if (!(flags & SA_ONSTACK))
119117
return false; // signal handler didn't enable it
120-
}
121-
if (!tib->tib_sigstack_size) {
118+
if (!tib->tib_sigstack_size)
122119
return false; // sigaltstack() wasn't installed on this thread
123-
}
124-
if (tib->tib_sigstack_flags & SS_DISABLE) {
120+
if (tib->tib_sigstack_flags & SS_DISABLE)
125121
return false; // sigaltstack() on this thread was disabled by user
126-
}
127122
char *bp = __builtin_frame_address(0);
128123
if (tib->tib_sigstack_addr <= bp &&
129-
bp <= tib->tib_sigstack_addr + tib->tib_sigstack_size) {
124+
bp <= tib->tib_sigstack_addr + tib->tib_sigstack_size)
130125
return false; // we're already on the alternate stack
131-
}
132126
return true;
133127
}
134128

@@ -282,9 +276,8 @@ static textwindows wontreturn void __sig_tramp(struct SignalFrame *sf) {
282276

283277
// jump back into original code if there aren't any pending signals
284278
do {
285-
if (!(sig = __sig_get(sf->ctx.uc_sigmask))) {
279+
if (!(sig = __sig_get(sf->ctx.uc_sigmask)))
286280
__sig_restore(&sf->ctx);
287-
}
288281
} while (!__sig_start(pt, sig, &sf->rva, &sf->flags));
289282

290283
// tail recurse into another signal handler
@@ -459,11 +452,9 @@ textwindows void __sig_generate(int sig, int sic) {
459452

460453
static textwindows char *__sig_stpcpy(char *d, const char *s) {
461454
size_t i;
462-
for (i = 0;; ++i) {
463-
if (!(d[i] = s[i])) {
455+
for (i = 0;; ++i)
456+
if (!(d[i] = s[i]))
464457
return d + i;
465-
}
466-
}
467458
}
468459

469460
static textwindows wontreturn void __sig_death(int sig, const char *thing) {
@@ -500,9 +491,8 @@ static textwindows void __sig_unmaskable(struct NtExceptionPointers *ep,
500491
// if the user didn't install a signal handler for this unmaskable
501492
// exception, then print a friendly helpful hint message to stderr
502493
unsigned rva = __sighandrvas[sig];
503-
if (rva == (intptr_t)SIG_DFL || rva == (intptr_t)SIG_IGN) {
494+
if (rva == (intptr_t)SIG_DFL || rva == (intptr_t)SIG_IGN)
504495
__sig_death(sig, "uncaught ");
505-
}
506496

507497
// if this signal handler is configured to auto-reset to the default
508498
// then that reset needs to happen before the user handler is called
@@ -560,9 +550,8 @@ __msabi dontinstrument unsigned __sig_crash(struct NtExceptionPointers *ep) {
560550
// this behavior is consistent with how unix kernels are implemented
561551
if (sig == SIGTRAP) {
562552
ep->ContextRecord->Rip++;
563-
if (__sig_ignored(sig)) {
553+
if (__sig_ignored(sig))
564554
return kNtExceptionContinueExecution;
565-
}
566555
}
567556

568557
// win32 stack overflow detection executes INSIDE the guard page

0 commit comments

Comments
 (0)