Skip to content

Commit

Permalink
Fix ucontext_t alignment in strict ansi mode
Browse files Browse the repository at this point in the history
Using `cosmocc -std=c11` was causing `ucontext_t` to become misaligned.
This change also adds the GNU constants on x86_64 for accessing general
registers, so you will not need `#ifdef`s to support both Cosmo and GNU
  • Loading branch information
jart committed Nov 9, 2023
1 parent b8d1377 commit 15af5c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
28 changes: 27 additions & 1 deletion libc/calls/ucontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@ COSMOPOLITAN_C_START_

#ifdef __x86_64__

#if defined(_GNU_SOURCE) || defined(_COSMO_SOURCE)
#define REG_R8 0
#define REG_R9 1
#define REG_R10 2
#define REG_R11 3
#define REG_R12 4
#define REG_R13 5
#define REG_R14 6
#define REG_R15 7
#define REG_RDI 8
#define REG_RSI 9
#define REG_RBP 10
#define REG_RBX 11
#define REG_RDX 12
#define REG_RAX 13
#define REG_RCX 14
#define REG_RSP 15
#define REG_RIP 16
#define REG_EFL 17
#define REG_CSGSFS 18
#define REG_ERR 19
#define REG_TRAPNO 20
#define REG_OLDMASK 21
#define REG_CR2 22
#endif

struct XmmRegister {
uint64_t u64[2];
};
Expand All @@ -31,7 +57,7 @@ struct thatispacked FpuState {
uint32_t __padding[24];
};

typedef uint64_t greg_t;
typedef long long greg_t;
typedef greg_t gregset_t[23];
typedef struct FpuState *fpregset_t;

Expand Down
12 changes: 1 addition & 11 deletions libc/integral/c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,9 @@ typedef struct {
#endif

#ifndef forcealign
#ifndef __STRICT_ANSI__
#define forcealign(bytes) __attribute__((__aligned__(bytes)))
#else
#define forcealign(bytes)
#endif
#endif

#ifndef __STRICT_ANSI__
#define thatispacked __attribute__((__packed__))
#else
#define thatispacked
#endif

#ifndef __STRICT_ANSI__
#define printfesque(n) __attribute__((__format__(__gnu_printf__, n, n + 1)))
Expand Down Expand Up @@ -727,8 +718,7 @@ void abort(void) wontreturn;
#define __static_yoink_source(PATH)
#endif

#define __weak_reference(sym, alias) \
__weak_reference_impl(sym, alias)
#define __weak_reference(sym, alias) __weak_reference_impl(sym, alias)
#define __weak_reference_impl(sym, alias) \
__asm__(".weak\t" #alias "\n\t" \
".equ\t" #alias ", " #sym "\n\t" \
Expand Down

0 comments on commit 15af5c2

Please sign in to comment.