Path: blob/main/contrib/llvm-project/compiler-rt/lib/builtins/assembly.h
35260 views
//===-- assembly.h - compiler-rt assembler support macros -----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines macros for use in compiler-rt assembler source.9// This file is not part of the interface of this library.10//11//===----------------------------------------------------------------------===//1213#ifndef COMPILERRT_ASSEMBLY_H14#define COMPILERRT_ASSEMBLY_H1516#if defined(__linux__) && defined(__CET__)17#if __has_include(<cet.h>)18#include <cet.h>19#endif20#endif2122#if defined(__APPLE__) && defined(__aarch64__)23#define SEPARATOR %%24#else25#define SEPARATOR ;26#endif2728#if defined(__APPLE__)29#define HIDDEN(name) .private_extern name30#define LOCAL_LABEL(name) L_##name31// tell linker it can break up file at label boundaries32#define FILE_LEVEL_DIRECTIVE .subsections_via_symbols33#define SYMBOL_IS_FUNC(name)34#define CONST_SECTION .const3536#define NO_EXEC_STACK_DIRECTIVE3738#elif defined(__ELF__)3940#define HIDDEN(name) .hidden name41#define LOCAL_LABEL(name) .L_##name42#define FILE_LEVEL_DIRECTIVE43#if defined(__arm__) || defined(__aarch64__)44#define SYMBOL_IS_FUNC(name) .type name,%function45#else46#define SYMBOL_IS_FUNC(name) .type name,@function47#endif48#define CONST_SECTION .section .rodata4950#if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \51defined(__linux__)52#define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits53#else54#define NO_EXEC_STACK_DIRECTIVE55#endif5657#else // !__APPLE__ && !__ELF__5859#define HIDDEN(name)60#define LOCAL_LABEL(name) .L ## name61#define FILE_LEVEL_DIRECTIVE62#define SYMBOL_IS_FUNC(name) \63.def name SEPARATOR \64.scl 2 SEPARATOR \65.type 32 SEPARATOR \66.endef67#define CONST_SECTION .section .rdata,"rd"6869#define NO_EXEC_STACK_DIRECTIVE7071#endif7273#if defined(__arm__) || defined(__aarch64__)74#define FUNC_ALIGN \75.text SEPARATOR \76.balign 16 SEPARATOR77#else78#define FUNC_ALIGN79#endif8081// BTI and PAC gnu property note82#define NT_GNU_PROPERTY_TYPE_0 583#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc000000084#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI 185#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC 28687#if defined(__ARM_FEATURE_BTI_DEFAULT)88#define BTI_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_BTI89#else90#define BTI_FLAG 091#endif9293#if __ARM_FEATURE_PAC_DEFAULT & 394#define PAC_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_PAC95#else96#define PAC_FLAG 097#endif9899#define GNU_PROPERTY(type, value) \100.pushsection .note.gnu.property, "a" SEPARATOR \101.p2align 3 SEPARATOR \102.word 4 SEPARATOR \103.word 16 SEPARATOR \104.word NT_GNU_PROPERTY_TYPE_0 SEPARATOR \105.asciz "GNU" SEPARATOR \106.word type SEPARATOR \107.word 4 SEPARATOR \108.word value SEPARATOR \109.word 0 SEPARATOR \110.popsection111112#if BTI_FLAG != 0113#define BTI_C hint #34114#define BTI_J hint #36115#else116#define BTI_C117#define BTI_J118#endif119120#if (BTI_FLAG | PAC_FLAG) != 0121#define GNU_PROPERTY_BTI_PAC \122GNU_PROPERTY(GNU_PROPERTY_AARCH64_FEATURE_1_AND, BTI_FLAG | PAC_FLAG)123#else124#define GNU_PROPERTY_BTI_PAC125#endif126127#if defined(__clang__) || defined(__GCC_HAVE_DWARF2_CFI_ASM)128#define CFI_START .cfi_startproc129#define CFI_END .cfi_endproc130#else131#define CFI_START132#define CFI_END133#endif134135#if defined(__arm__)136137// Determine actual [ARM][THUMB[1][2]] ISA using compiler predefined macros:138// - for '-mthumb -march=armv6' compiler defines '__thumb__'139// - for '-mthumb -march=armv7' compiler defines '__thumb__' and '__thumb2__'140#if defined(__thumb2__) || defined(__thumb__)141#define DEFINE_CODE_STATE .thumb SEPARATOR142#define DECLARE_FUNC_ENCODING .thumb_func SEPARATOR143#if defined(__thumb2__)144#define USE_THUMB_2145#define IT(cond) it cond146#define ITT(cond) itt cond147#define ITE(cond) ite cond148#else149#define USE_THUMB_1150#define IT(cond)151#define ITT(cond)152#define ITE(cond)153#endif // defined(__thumb__2)154#else // !defined(__thumb2__) && !defined(__thumb__)155#define DEFINE_CODE_STATE .arm SEPARATOR156#define DECLARE_FUNC_ENCODING157#define IT(cond)158#define ITT(cond)159#define ITE(cond)160#endif161162#if defined(USE_THUMB_1) && defined(USE_THUMB_2)163#error "USE_THUMB_1 and USE_THUMB_2 can't be defined together."164#endif165166#if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5167#define ARM_HAS_BX168#endif169#if !defined(__ARM_FEATURE_CLZ) && !defined(USE_THUMB_1) && \170(__ARM_ARCH >= 6 || (__ARM_ARCH == 5 && !defined(__ARM_ARCH_5__)))171#define __ARM_FEATURE_CLZ172#endif173174#ifdef ARM_HAS_BX175#define JMP(r) bx r176#define JMPc(r, c) bx##c r177#else178#define JMP(r) mov pc, r179#define JMPc(r, c) mov##c pc, r180#endif181182// pop {pc} can't switch Thumb mode on ARMv4T183#if __ARM_ARCH >= 5184#define POP_PC() pop {pc}185#else186#define POP_PC() \187pop {ip}; \188JMP(ip)189#endif190191#if defined(USE_THUMB_2)192#define WIDE(op) op.w193#else194#define WIDE(op) op195#endif196#else // !defined(__arm)197#define DECLARE_FUNC_ENCODING198#define DEFINE_CODE_STATE199#endif200201#define GLUE2_(a, b) a##b202#define GLUE(a, b) GLUE2_(a, b)203#define GLUE2(a, b) GLUE2_(a, b)204#define GLUE3_(a, b, c) a##b##c205#define GLUE3(a, b, c) GLUE3_(a, b, c)206#define GLUE4_(a, b, c, d) a##b##c##d207#define GLUE4(a, b, c, d) GLUE4_(a, b, c, d)208209#define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)210211#ifdef VISIBILITY_HIDDEN212#define DECLARE_SYMBOL_VISIBILITY(name) \213HIDDEN(SYMBOL_NAME(name)) SEPARATOR214#define DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name) \215HIDDEN(name) SEPARATOR216#else217#define DECLARE_SYMBOL_VISIBILITY(name)218#define DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name)219#endif220221#define DEFINE_COMPILERRT_FUNCTION(name) \222DEFINE_CODE_STATE \223FILE_LEVEL_DIRECTIVE SEPARATOR \224.globl SYMBOL_NAME(name) SEPARATOR \225SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \226DECLARE_SYMBOL_VISIBILITY(name) \227DECLARE_FUNC_ENCODING \228SYMBOL_NAME(name):229230#define DEFINE_COMPILERRT_THUMB_FUNCTION(name) \231DEFINE_CODE_STATE \232FILE_LEVEL_DIRECTIVE SEPARATOR \233.globl SYMBOL_NAME(name) SEPARATOR \234SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \235DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR \236.thumb_func SEPARATOR \237SYMBOL_NAME(name):238239#define DEFINE_COMPILERRT_PRIVATE_FUNCTION(name) \240DEFINE_CODE_STATE \241FILE_LEVEL_DIRECTIVE SEPARATOR \242.globl SYMBOL_NAME(name) SEPARATOR \243SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \244HIDDEN(SYMBOL_NAME(name)) SEPARATOR \245DECLARE_FUNC_ENCODING \246SYMBOL_NAME(name):247248#define DEFINE_COMPILERRT_PRIVATE_FUNCTION_UNMANGLED(name) \249DEFINE_CODE_STATE \250.globl name SEPARATOR \251SYMBOL_IS_FUNC(name) SEPARATOR \252HIDDEN(name) SEPARATOR \253DECLARE_FUNC_ENCODING \254name:255256#define DEFINE_COMPILERRT_OUTLINE_FUNCTION_UNMANGLED(name) \257DEFINE_CODE_STATE \258FUNC_ALIGN \259.globl name SEPARATOR \260SYMBOL_IS_FUNC(name) SEPARATOR \261DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name) SEPARATOR \262DECLARE_FUNC_ENCODING \263name: \264SEPARATOR CFI_START \265SEPARATOR BTI_C266267#define DEFINE_COMPILERRT_FUNCTION_ALIAS(name, target) \268.globl SYMBOL_NAME(name) SEPARATOR \269SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \270DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR \271.set SYMBOL_NAME(name), SYMBOL_NAME(target) SEPARATOR272273#if defined(__ARM_EABI__)274#define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name) \275DEFINE_COMPILERRT_FUNCTION_ALIAS(aeabi_name, name)276#else277#define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name)278#endif279280#ifdef __ELF__281#define END_COMPILERRT_FUNCTION(name) \282.size SYMBOL_NAME(name), . - SYMBOL_NAME(name)283#define END_COMPILERRT_OUTLINE_FUNCTION(name) \284CFI_END SEPARATOR \285.size SYMBOL_NAME(name), . - SYMBOL_NAME(name)286#else287#define END_COMPILERRT_FUNCTION(name)288#define END_COMPILERRT_OUTLINE_FUNCTION(name) \289CFI_END290#endif291292#endif // COMPILERRT_ASSEMBLY_H293294295