2020-12-24 07:42:56 +00:00
|
|
|
/*-*- 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 │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-12-24 07:42:56 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-12-24 07:42:56 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-03-18 19:43:21 +00:00
|
|
|
#include "libc/intrin/asancodes.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "ape/relocations.h"
|
2020-12-24 07:42:56 +00:00
|
|
|
/* clang-format off */
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
#if __MNO_VZEROUPPER__ + 0
|
|
|
|
#define vzeroupper
|
|
|
|
#endif
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Begins definition of frameless function that calls no functions.
|
2020-08-25 11:23:25 +00:00
|
|
|
.macro .leafprologue
|
|
|
|
#if !(defined(TINY) && !defined(__PG__))
|
|
|
|
push %rbp
|
|
|
|
mov %rsp,%rbp
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Ends definition of frameless function that calls no functions.
|
2020-08-25 11:23:25 +00:00
|
|
|
.macro .leafepilogue
|
|
|
|
#if !(defined(TINY) && !defined(__PG__))
|
|
|
|
pop %rbp
|
|
|
|
#endif
|
|
|
|
ret
|
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Good alignment for functions where alignment actually helps.
|
|
|
|
// @note 16-byte
|
2020-08-25 11:23:25 +00:00
|
|
|
.macro .alignfunc
|
|
|
|
#ifndef __OPTIMIZE_SIZE__
|
|
|
|
.p2align 4
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Good alignment for loops where alignment actually helps.
|
|
|
|
// @note 16-byte if <10 padding otherwise 8-byte
|
2020-08-25 11:23:25 +00:00
|
|
|
.macro .alignloop
|
|
|
|
#ifndef __OPTIMIZE_SIZE__
|
|
|
|
.p2align 4,,10
|
|
|
|
.p2align 4
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Loads Effective Address
|
|
|
|
// Supporting security blankets
|
2020-06-15 14:18:57 +00:00
|
|
|
.macro plea symbol:req reg64:req reg32:req
|
|
|
|
#if __PIC__ + __PIE__ + __code_model_medium__ + __code_model_large__ + 0 > 1
|
|
|
|
lea \symbol(%rip),\reg64
|
|
|
|
#else
|
|
|
|
mov $\symbol,\reg32
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Loads Effective Address to Stack
|
|
|
|
// Supporting security blankets
|
2020-06-15 14:18:57 +00:00
|
|
|
.macro pshaddr symbol:req
|
|
|
|
#if __PIC__ + __PIE__ + __code_model_medium__ + __code_model_large__ + 0 > 1
|
|
|
|
push $IMAGE_BASE_VIRTUAL+RVA(\symbol)(%rip),\reg64
|
|
|
|
#else
|
|
|
|
push $\symbol
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// TODO(jart): delete
|
|
|
|
// Loads Effective Address
|
|
|
|
// Supporting security blankets
|
2020-06-15 14:18:57 +00:00
|
|
|
.macro ezlea symbol:req reg:req
|
2020-08-25 11:23:25 +00:00
|
|
|
#if __pic__ + __pie__ + __code_model_medium__ + __code_model_large__ + 0 > 1
|
2021-02-08 17:19:00 +00:00
|
|
|
// lea \symbol(%rip),%r\reg
|
2020-06-15 14:18:57 +00:00
|
|
|
mov $\symbol,%e\reg
|
|
|
|
#else
|
|
|
|
mov $\symbol,%e\reg
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
|
|
|
.macro farcall symbol:req
|
|
|
|
.type \symbol,@function
|
|
|
|
#if __PIC__ + __PIE__ + __code_model_medium__ + __code_model_large__ + 0 > 1
|
|
|
|
call *\symbol\()@gotpcrel(%rip)
|
|
|
|
#else
|
|
|
|
call \symbol\()@plt
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2022-03-18 19:43:21 +00:00
|
|
|
// Documents unreachable assembly code.
|
|
|
|
.macro .unreachable
|
|
|
|
#ifndef NDEBUG
|
|
|
|
ud2 # crash if contract is broken
|
|
|
|
#elif defined(__FNO_OMIT_FRAME_POINTER__)
|
|
|
|
nop # avoid noreturn tail call backtrace ambiguity
|
|
|
|
#endif
|
2020-06-16 02:01:28 +00:00
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Inserts profiling hook in prologue if cc wants it.
|
|
|
|
//
|
|
|
|
// Cosmopolitan does this in a slightly different way from normal
|
|
|
|
// GNU toolchains. We always use the -mnop-mcount behavior, since
|
|
|
|
// the runtime is able to morph the binary at runtime. It is good
|
|
|
|
// since we can put hooks for profiling and function tracing into
|
|
|
|
// most builds, without any impact on performance.
|
|
|
|
//
|
|
|
|
// @cost ≥0.3 cycles, 5 bytes
|
|
|
|
// @see build/compile
|
2020-06-15 14:18:57 +00:00
|
|
|
.macro .profilable
|
|
|
|
#ifdef __PG__
|
|
|
|
1382:
|
|
|
|
#if defined(__MFENTRY__)
|
|
|
|
call __fentry__
|
|
|
|
#elif defined(__PIC__) || defined(__PIE__)
|
2021-02-08 17:19:00 +00:00
|
|
|
// nopw 0x00(%rax,%rax,1)
|
2020-06-15 14:18:57 +00:00
|
|
|
.byte 0x66,0x0f,0x1f,0x44,0x00,0x00
|
|
|
|
#else
|
2021-02-08 17:19:00 +00:00
|
|
|
// nopl 0x00(%rax,%rax,1)
|
2020-06-15 14:18:57 +00:00
|
|
|
.byte 0x0f,0x1f,0x44,0x00,0x00
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Pushes RVA on stack of linktime mergeable string literal.
|
|
|
|
// @see popstr
|
2020-06-15 14:18:57 +00:00
|
|
|
.macro pushstr text
|
2021-01-10 21:30:48 +00:00
|
|
|
.section .rodata.str1.1,"aSM",@progbits,1
|
2020-06-15 14:18:57 +00:00
|
|
|
.Lstr\@: .asciz "\text"
|
|
|
|
.endobj .Lstr\@
|
2021-01-10 21:30:48 +00:00
|
|
|
.previous
|
2020-06-15 14:18:57 +00:00
|
|
|
push $.Lstr\@ - IMAGE_BASE_VIRTUAL
|
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Pops off stack string address.
|
|
|
|
// @see pushstr
|
2020-06-15 14:18:57 +00:00
|
|
|
.macro popstr dest:req
|
|
|
|
addl $IMAGE_BASE_VIRTUAL,(%rsp)
|
|
|
|
pop \dest
|
|
|
|
.endm
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Loads address of linktime mergeable string literal into register.
|
2020-06-15 14:18:57 +00:00
|
|
|
.macro loadstr text:req reg:req regsz bias=0
|
2021-01-10 21:30:48 +00:00
|
|
|
.section .rodata.str1.1,"aSM",@progbits,1
|
2020-06-15 14:18:57 +00:00
|
|
|
.type .Lstr\@,@object
|
|
|
|
.Lstr\@: .asciz "\text"
|
|
|
|
.Lstr\@.size = .-.Lstr\@ - 1
|
|
|
|
.size .Lstr\@,.-.Lstr\@
|
2021-01-10 21:30:48 +00:00
|
|
|
.previous
|
2020-06-15 14:18:57 +00:00
|
|
|
ezlea .Lstr\@,\reg
|
|
|
|
.ifnb \regsz
|
|
|
|
#ifdef __OPTIMIZE_SIZE__
|
|
|
|
.if .Lstr\@.size + \bias < 128
|
|
|
|
pushpop .Lstr\@.size,%r\regsz
|
|
|
|
.else
|
|
|
|
mov $.Lstr\@.size,%e\regsz
|
|
|
|
.endif
|
|
|
|
#else
|
|
|
|
mov $.Lstr\@.size,%e\regsz
|
|
|
|
#endif
|
|
|
|
.endif
|
|
|
|
.endm
|
2022-03-18 09:33:37 +00:00
|
|
|
|
|
|
|
.macro .poison name:req kind:req
|
2022-03-21 10:46:16 +00:00
|
|
|
#ifdef __SANITIZE_ADDRESS__
|
2022-03-18 09:33:37 +00:00
|
|
|
2323: .quad 0
|
|
|
|
.init.start 304,"_init_\name\()_poison_\@"
|
|
|
|
push %rdi
|
|
|
|
push %rsi
|
|
|
|
ezlea 2323b,di
|
|
|
|
mov $8,%esi
|
|
|
|
mov $\kind,%edx
|
|
|
|
call __asan_poison
|
|
|
|
pop %rsi
|
|
|
|
pop %rdi
|
|
|
|
.init.end 304,"_init_\name\()_poison_\@"
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
|
|
|
.macro .underrun
|
2022-03-21 10:46:16 +00:00
|
|
|
#ifdef __SANITIZE_ADDRESS__
|
2022-03-18 19:43:21 +00:00
|
|
|
.poison __BASE_FILE__, kAsanGlobalUnderrun
|
2022-03-18 09:33:37 +00:00
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
|
|
|
.macro .overrun
|
2022-03-21 10:46:16 +00:00
|
|
|
#ifdef __SANITIZE_ADDRESS__
|
2022-03-18 19:43:21 +00:00
|
|
|
.poison __BASE_FILE__, kAsanGlobalUnderrun
|
2022-03-18 09:33:37 +00:00
|
|
|
#endif
|
|
|
|
.endm
|