xref: /aosp_15_r20/bionic/libc/private/bionic_asm.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #pragma once
30*8d67ca89SAndroid Build Coastguard Worker 
31*8d67ca89SAndroid Build Coastguard Worker /* https://github.com/android/ndk/issues/1422 */
32*8d67ca89SAndroid Build Coastguard Worker #include <features.h>
33*8d67ca89SAndroid Build Coastguard Worker 
34*8d67ca89SAndroid Build Coastguard Worker #include <asm/unistd.h> /* For system call numbers. */
35*8d67ca89SAndroid Build Coastguard Worker #define MAX_ERRNO 4095  /* For recognizing system call error returns. */
36*8d67ca89SAndroid Build Coastguard Worker 
37*8d67ca89SAndroid Build Coastguard Worker #define __bionic_asm_custom_entry(f)
38*8d67ca89SAndroid Build Coastguard Worker #define __bionic_asm_custom_end(f)
39*8d67ca89SAndroid Build Coastguard Worker #define __bionic_asm_function_type @function
40*8d67ca89SAndroid Build Coastguard Worker #define __bionic_asm_custom_note_gnu_section()
41*8d67ca89SAndroid Build Coastguard Worker 
42*8d67ca89SAndroid Build Coastguard Worker #if defined(__aarch64__)
43*8d67ca89SAndroid Build Coastguard Worker #include <private/bionic_asm_arm64.h>
44*8d67ca89SAndroid Build Coastguard Worker #elif defined(__arm__)
45*8d67ca89SAndroid Build Coastguard Worker #include <private/bionic_asm_arm.h>
46*8d67ca89SAndroid Build Coastguard Worker #elif defined(__i386__)
47*8d67ca89SAndroid Build Coastguard Worker #include <private/bionic_asm_x86.h>
48*8d67ca89SAndroid Build Coastguard Worker #elif defined(__riscv)
49*8d67ca89SAndroid Build Coastguard Worker #include <private/bionic_asm_riscv64.h>
50*8d67ca89SAndroid Build Coastguard Worker #elif defined(__x86_64__)
51*8d67ca89SAndroid Build Coastguard Worker #include <private/bionic_asm_x86_64.h>
52*8d67ca89SAndroid Build Coastguard Worker #endif
53*8d67ca89SAndroid Build Coastguard Worker 
54*8d67ca89SAndroid Build Coastguard Worker // Starts a normal assembler routine.
55*8d67ca89SAndroid Build Coastguard Worker #define ENTRY(__f) __ENTRY_WITH_BINDING(__f, .globl)
56*8d67ca89SAndroid Build Coastguard Worker 
57*8d67ca89SAndroid Build Coastguard Worker // Starts an assembler routine with hidden visibility.
58*8d67ca89SAndroid Build Coastguard Worker #define ENTRY_PRIVATE(__f)           \
59*8d67ca89SAndroid Build Coastguard Worker   __ENTRY_WITH_BINDING(__f, .globl); \
60*8d67ca89SAndroid Build Coastguard Worker   .hidden __f;
61*8d67ca89SAndroid Build Coastguard Worker 
62*8d67ca89SAndroid Build Coastguard Worker // Starts an assembler routine that's weak so native bridges can override it.
63*8d67ca89SAndroid Build Coastguard Worker #define ENTRY_WEAK_FOR_NATIVE_BRIDGE(__f) __ENTRY_WITH_BINDING(__f, .weak)
64*8d67ca89SAndroid Build Coastguard Worker 
65*8d67ca89SAndroid Build Coastguard Worker // Starts an assembler routine with hidden visibility and no DWARF information.
66*8d67ca89SAndroid Build Coastguard Worker // Only used for internal functions passed via sa_restorer.
67*8d67ca89SAndroid Build Coastguard Worker // TODO: can't we just delete all those and let the kernel do its thing?
68*8d67ca89SAndroid Build Coastguard Worker #define ENTRY_NO_DWARF_PRIVATE(__f) \
69*8d67ca89SAndroid Build Coastguard Worker   __ENTRY_NO_DWARF(__f, .globl);    \
70*8d67ca89SAndroid Build Coastguard Worker   .hidden __f;
71*8d67ca89SAndroid Build Coastguard Worker 
72*8d67ca89SAndroid Build Coastguard Worker // (Implementation detail.)
73*8d67ca89SAndroid Build Coastguard Worker #define __ENTRY_NO_DWARF(__f, __binding) \
74*8d67ca89SAndroid Build Coastguard Worker   .text;                                 \
75*8d67ca89SAndroid Build Coastguard Worker   __binding __f;                         \
76*8d67ca89SAndroid Build Coastguard Worker   .balign __bionic_asm_align;            \
77*8d67ca89SAndroid Build Coastguard Worker   .type __f, __bionic_asm_function_type; \
78*8d67ca89SAndroid Build Coastguard Worker   __f:                                   \
79*8d67ca89SAndroid Build Coastguard Worker   __bionic_asm_custom_entry(__f);
80*8d67ca89SAndroid Build Coastguard Worker 
81*8d67ca89SAndroid Build Coastguard Worker // (Implementation detail.)
82*8d67ca89SAndroid Build Coastguard Worker #define __ENTRY_WITH_BINDING(__f, __binding) \
83*8d67ca89SAndroid Build Coastguard Worker   __ENTRY_NO_DWARF(__f, __binding);          \
84*8d67ca89SAndroid Build Coastguard Worker   .cfi_startproc;
85*8d67ca89SAndroid Build Coastguard Worker 
86*8d67ca89SAndroid Build Coastguard Worker // Ends a normal assembler routine.
87*8d67ca89SAndroid Build Coastguard Worker #define END(__f) \
88*8d67ca89SAndroid Build Coastguard Worker   .cfi_endproc;  \
89*8d67ca89SAndroid Build Coastguard Worker   END_NO_DWARF(__f)
90*8d67ca89SAndroid Build Coastguard Worker 
91*8d67ca89SAndroid Build Coastguard Worker // Ends an assembler routine with no DWARF information.
92*8d67ca89SAndroid Build Coastguard Worker // Only used for internal functions passed via sa_restorer.
93*8d67ca89SAndroid Build Coastguard Worker // TODO: can't we just delete all those and let the kernel do its thing?
94*8d67ca89SAndroid Build Coastguard Worker #define END_NO_DWARF(__f) \
95*8d67ca89SAndroid Build Coastguard Worker   .size __f, .- __f;      \
96*8d67ca89SAndroid Build Coastguard Worker   __bionic_asm_custom_end(__f)
97*8d67ca89SAndroid Build Coastguard Worker 
98*8d67ca89SAndroid Build Coastguard Worker // Creates an alias `alias` for the symbol `original`.
99*8d67ca89SAndroid Build Coastguard Worker #define ALIAS_SYMBOL(alias, original) \
100*8d67ca89SAndroid Build Coastguard Worker   .globl alias;                       \
101*8d67ca89SAndroid Build Coastguard Worker   .equ alias, original
102*8d67ca89SAndroid Build Coastguard Worker 
103*8d67ca89SAndroid Build Coastguard Worker // Creates an alias `alias` for the symbol `original` that's weak so it can be
104*8d67ca89SAndroid Build Coastguard Worker // separately overridden by native bridges.
105*8d67ca89SAndroid Build Coastguard Worker #define ALIAS_SYMBOL_WEAK_FOR_NATIVE_BRIDGE(alias, original) \
106*8d67ca89SAndroid Build Coastguard Worker   .weak alias;                                               \
107*8d67ca89SAndroid Build Coastguard Worker   .equ alias, original
108*8d67ca89SAndroid Build Coastguard Worker 
109*8d67ca89SAndroid Build Coastguard Worker // Adds a GNU property ELF note. Important on arm64 to declare PAC/BTI support.
110*8d67ca89SAndroid Build Coastguard Worker #define NOTE_GNU_PROPERTY() __bionic_asm_custom_note_gnu_section()
111*8d67ca89SAndroid Build Coastguard Worker 
112*8d67ca89SAndroid Build Coastguard Worker // Gives local labels a more convenient and readable syntax.
113*8d67ca89SAndroid Build Coastguard Worker #define L(__label) .L##__label
114