Skip to content

Commit

Permalink
Replace .pushsection directives (jart#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Jan 10, 2021
1 parent 2831689 commit 04f1d89
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 10,422 deletions.
4 changes: 2 additions & 2 deletions ape/idata.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ idata.iat.\name:
*/.section .piro.data.sort.iat.2.\name\().3,"awG",\name,comdat
.quad 0
.previous
.pushsection .rodata.str1.1,"aSM",@progbits,1
.section .rodata.str1.1,"aSM",@progbits,1
.Lidata.str.\name:
.asciz "\name\().dll"
.popsection
.previous
.endm

/* clang-format on */
Expand Down
2 changes: 1 addition & 1 deletion examples/nesemu1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static unsigned char pixels_[3][DYN][DXN];
static unsigned char palette_[3][64][512][3];
static int joy_current_[2], joy_next_[2], joypos_[2];

static int keyframes_ = 20;
static int keyframes_ = 10;
static enum TtyBlocksSelection blocks_ = kTtyBlocksUnicode;
static enum TtyQuantizationAlgorithm quant_ = kTtyQuantTrue;

Expand Down
4 changes: 2 additions & 2 deletions libc/bits/initializer.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* Teleports code fragment inside _init().
*/
#define INITIALIZER(PRI, NAME, CODE) \
asm(".pushsection .init." #PRI "." #NAME ",\"ax\",@progbits\n\t" \
asm(".section .init." #PRI "." #NAME ",\"ax\",@progbits\n\t" \
"call\t" #NAME "\n\t" \
".popsection"); \
".previous"); \
textstartup optimizesize void NAME(char *rdi, const char *rsi) { \
CODE; \
asm volatile("" : /* no outputs */ : "D"(rdi), "S"(rsi)); \
Expand Down
4 changes: 2 additions & 2 deletions libc/fmt/pflink.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
#else
#define PFLINK(FMT) FMT
#define SFLINK(FMT) FMT
asm(".pushsection .yoink\n\t"
asm(".section .yoink\n\t"
"nop\tntoa(%rip)\n\t"
"nop\tftoa(%rip)\n\t"
"nop\tkCp437(%rip)\n\t"
Expand All @@ -77,7 +77,7 @@ asm(".pushsection .yoink\n\t"
"nop\tcalloc(%rip)\n\t"
"nop\tfree_s(%rip)\n\t"
"nop\t__grow(%rip)\n\t"
".popsection");
".previous");
#endif /* __STRICT_ANSI__ */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_FMT_PFLINK_H_ */
6 changes: 3 additions & 3 deletions libc/integral/c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -776,15 +776,15 @@ typedef uint64_t uintmax_t;
do { \
_Static_assert(!__builtin_types_compatible_p(typeof(SYMBOL), char[]), \
"Please YOINK(symbol), not YOINK(\"symbol\")"); \
asm(".pushsection .yoink\n\t" \
asm(".section .yoink\n\t" \
"nop\t%a0\n\t" \
".popsection" \
".previous" \
: /* no outputs */ \
: "X"(SYMBOL)); \
} while (0)

#define STATIC_YOINK(SYMBOLSTR) \
asm(".pushsection .yoink\n\tnopl\t\"" SYMBOLSTR "\"\n\t.popsection")
asm(".section .yoink\n\tnopl\t\"" SYMBOLSTR "\"\n\t.previous")

#if !defined(IM_FEELING_NAUGHTY) && !defined(__STRICT_ANSI__)
#define STATIC_YOINK_SOURCE(PATH) STATIC_YOINK(PATH)
Expand Down
12 changes: 0 additions & 12 deletions libc/log/cancolor.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,9 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/bits/weaken.h"
#include "libc/calls/internal.h"
#include "libc/dce.h"
#include "libc/log/log.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/consolemodeflags.h"
#include "libc/nt/enum/filetype.h"
#include "libc/nt/enum/version.h"
#include "libc/nt/files.h"
#include "libc/nt/pedef.internal.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/teb.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"

/**
* Returns true if ANSI terminal colors are appropriate.
Expand Down
16 changes: 8 additions & 8 deletions libc/macros-cpp.internal.inc
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,20 @@
.byte 0x0f,0x1f,0x44,0x00,0x00
#endif
#if defined(__MRECORD_MCOUNT__) && !defined(__MFENTRY__)
.pushsection __mcount_loc,"a",@progbits
.section __mcount_loc,"a",@progbits
.quad 1382b
.popsection
.previous
#endif
#endif
.endm

/ Pushes RVA on stack of linktime mergeable string literal.
/ @see popstr
.macro pushstr text
.pushsection .rodata.str1.1,"aSM",@progbits,1
.section .rodata.str1.1,"aSM",@progbits,1
.Lstr\@: .asciz "\text"
.endobj .Lstr\@
.popsection
.previous
push $.Lstr\@ - IMAGE_BASE_VIRTUAL
.endm

Expand All @@ -155,12 +155,12 @@

/ Loads address of linktime mergeable string literal into register.
.macro getstr text:req reg64:req reg32 regsz64 regsz32 bias=0
.pushsection .rodata.str1.1,"aSM",@progbits,1
.section .rodata.str1.1,"aSM",@progbits,1
.type .Lstr\@,@object
.Lstr\@: .asciz "\text"
.Lstr\@.size = .-.Lstr\@ - 1
.size .Lstr\@,.-.Lstr\@
.popsection
.previous
plea .Lstr\@,\reg64,\reg32
.ifnb \regsz64
#ifdef __OPTIMIZE_SIZE__
Expand All @@ -178,12 +178,12 @@
/ TODO(jart): delete
/ Loads address of linktime mergeable string literal into register.
.macro loadstr text:req reg:req regsz bias=0
.pushsection .rodata.str1.1,"aSM",@progbits,1
.section .rodata.str1.1,"aSM",@progbits,1
.type .Lstr\@,@object
.Lstr\@: .asciz "\text"
.Lstr\@.size = .-.Lstr\@ - 1
.size .Lstr\@,.-.Lstr\@
.popsection
.previous
ezlea .Lstr\@,\reg
.ifnb \regsz
#ifdef __OPTIMIZE_SIZE__
Expand Down
24 changes: 12 additions & 12 deletions libc/macros.internal.inc
Original file line number Diff line number Diff line change
Expand Up @@ -253,25 +253,25 @@
.macro .hookable
/ nopl 0x00(%rax,%rax,1)
83457: .byte 0x0f,0x1f,0x44,0x00,0x00
.pushsection __mcount_loc,"a",@progbits
.section __mcount_loc,"a",@progbits
.quad 83457b
.popsection
.previous
.endm

/ Puts initialized data in uninitialized data section.
.macro .bsdata name:req expr:req bnd vis
.pushsection .initbss.300._init_\name,"aw",@nobits
.section .initbss.300._init_\name,"aw",@nobits
\name: .quad 0
.endobj \name,\bnd,\vis
.popsection
.pushsection .initro.300._init_\name,"a",@progbits
.previous
.section .initro.300._init_\name,"a",@progbits
.quad \expr
.popsection
.pushsection .init.300._init_\name,"ax",@progbits
.previous
.section .init.300._init_\name,"ax",@progbits
_init_\name:
movsq
.endfn _init_\name
.popsection
.previous
.endm

/ ICE Breakpoint.
Expand Down Expand Up @@ -304,14 +304,14 @@ _init_\name:
/ In order for this technique to work with --gc-sections, another
/ module somewhere might want to weakly reference whats yoinked.
.macro yoink symbol:req
.pushsection .yoink
.section .yoink
nop "\symbol"
.popsection
.previous
.endm
.macro .yoink symbol:req
.pushsection .yoink
.section .yoink
nop "\symbol"
.popsection
.previous
.endm

/ Calls Windows function.
Expand Down
20 changes: 19 additions & 1 deletion libc/nexgen32e/macros.internal.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
#include "libc/nexgen32e/x86feature.h"
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
│vi: set et ft=asm ts=8 sw=8 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/macros.h"
#include "libc/nexgen32e/x86feature.h"

/ Broadcast byte literal to vector, e.g.
/
Expand Down
77 changes: 56 additions & 21 deletions libc/str/strcspn.c → libc/nexgen32e/strcspn.S
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
Expand All @@ -16,23 +16,58 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/nexgen32e/hascharacter.internal.h"
#include "libc/str/str.h"
#include "libc/macros.h"

/**
* Returns prefix length, consisting of chars not in reject.
* a.k.a. Return index of first byte that's in charset.
*
* @param reject is nul-terminated character set
* @see strspn(), strtok_r()
* @asyncsignalsafe
*/
size_t strcspn(const char *s, const char *reject) {
size_t i;
for (i = 0; s[i]; ++i) {
if (HasCharacter(s[i], reject)) {
break;
}
}
return i;
}
/ Returns prefix length, consisting of chars not in reject.
/ a.k.a. Return index of first byte that's in charset.
/
/ @param rdi is string
/ @param rsi is reject nul-terminated character set
/ @see strspn(), strtok_r()
/ @asyncsignalsafe
strcspn:
push %rbp
mov %rsp,%rbp
sub $16,%rsp
push %rdi
mov %rsi,%rdi
call strlen
pop %rdi
cmp $15,%rax
ja 4f
push %rdi
mov %rax,%rdx
pxor %xmm0,%xmm0
lea -16(%rbp),%rdi
movdqa %xmm0,(%rdi)
call MemCpy
movdqa (%rdi),%xmm1
pop %rdi
or $-1,%rax
0: inc %rax
movzbl (%rdi,%rax),%ecx
movd %ecx,%xmm0
punpcklbw %xmm0,%xmm0
punpcklwd %xmm0,%xmm0
pshufd $0,%xmm0,%xmm0
pcmpeqb %xmm1,%xmm0
pmovmskb %xmm0,%ecx
test %ecx,%ecx
jz 0b
9: leave
ret
1: cmp %ch,%cl
je 9b
inc %edx
2: mov (%rsi,%rdx),%ch
test %ch,%ch
jne 1b
inc %rax
3: mov (%rdi,%rax),%cl
test %cl,%cl
je 9b
xor %edx,%edx
jmp 2b
4: xor %eax,%eax
jmp 3b
.endfn strcspn,globl
24 changes: 12 additions & 12 deletions libc/nexgen32e/trampoline.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#define COSMOPOLITAN_LIBC_NEXGEN32E_TRAMPOLINE_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)

#define TRAMPOLINE(FUNCTION, THUNK) \
({ \
typeof(FUNCTION) *Tramp; \
asm(".pushsection .text.trampoline\n" \
"183:\n\t" \
"mov\t%1,%%eax\n\t" \
"jmp\t" #THUNK "\n\t" \
".popsection\n\t" \
"mov\t$183b,%k0" \
: "=r"(Tramp) \
: "i"(FUNCTION)); \
Tramp; \
#define TRAMPOLINE(FUNCTION, THUNK) \
({ \
typeof(FUNCTION) *Tramp; \
asm(".section .text.trampoline\n" \
"183:\n\t" \
"mov\t%1,%%eax\n\t" \
"jmp\t" #THUNK "\n\t" \
".previous\n\t" \
"mov\t$183b,%k0" \
: "=r"(Tramp) \
: "i"(FUNCTION)); \
Tramp; \
})

#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
Expand Down
Loading

0 comments on commit 04f1d89

Please sign in to comment.