1 /* Assembler macros for i386.
2    Copyright (C) 1991-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <sysdeps/x86/sysdep.h>
20 
21 /* It is desirable that the names of PIC thunks match those used by
22    GCC so that multiple copies are eliminated by the linker.  Because
23    GCC 4.6 and earlier use __i686 in the names, it is necessary to
24    override that predefined macro.  */
25 #if defined __i686 && defined __ASSEMBLER__
26 #undef __i686
27 #define __i686 __i686
28 #endif
29 
30 #ifdef	__ASSEMBLER__
31 # define GET_PC_THUNK(reg) __x86.get_pc_thunk.reg
32 #else
33 # define GET_PC_THUNK_STR(reg) "__x86.get_pc_thunk." #reg
34 #endif
35 
36 #ifdef	__ASSEMBLER__
37 
38 /* Syntactic details of assembler.  */
39 
40 /* If compiled for profiling, call `mcount' at the start of each function.  */
41 #ifdef	PROF
42 /* The mcount code relies on a normal frame pointer being on the stack
43    to locate our caller, so push one just for its benefit.  */
44 #define CALL_MCOUNT \
45   pushl %ebp; cfi_adjust_cfa_offset (4); movl %esp, %ebp; \
46   cfi_def_cfa_register (ebp); call JUMPTARGET(mcount); \
47   popl %ebp; cfi_def_cfa (esp, 4);
48 #else
49 #define CALL_MCOUNT		/* Do nothing.  */
50 #endif
51 
52 #define	PSEUDO(name, syscall_name, args)				      \
53   .globl syscall_error;							      \
54 lose: SYSCALL_PIC_SETUP							      \
55   jmp JUMPTARGET(syscall_error);					      \
56   ENTRY (name)								      \
57   DO_CALL (syscall_name, args);						      \
58   jb lose
59 
60 # define SETUP_PIC_REG(reg) \
61   .ifndef GET_PC_THUNK(reg);						      \
62   .section .text.GET_PC_THUNK(reg),"axG",@progbits,GET_PC_THUNK(reg),comdat;  \
63   .globl GET_PC_THUNK(reg);						      \
64   .hidden GET_PC_THUNK(reg);						      \
65   .p2align 4;								      \
66   .type GET_PC_THUNK(reg),@function;					      \
67 GET_PC_THUNK(reg):							      \
68   movl (%esp), %e##reg;							      \
69   ret;									      \
70   .size GET_PC_THUNK(reg), . - GET_PC_THUNK(reg);			      \
71   .previous;								      \
72   .endif;								      \
73   call GET_PC_THUNK(reg)
74 
75 # define LOAD_PIC_REG(reg) \
76   SETUP_PIC_REG(reg); addl $_GLOBAL_OFFSET_TABLE_, %e##reg
77 
78 #undef JUMPTARGET
79 #ifdef PIC
80 #define JUMPTARGET(name)	name##@PLT
81 #define SYSCALL_PIC_SETUP \
82     pushl %ebx;								      \
83     cfi_adjust_cfa_offset (4);						      \
84     call 0f;								      \
85 0:  popl %ebx;								      \
86     cfi_adjust_cfa_offset (-4);						      \
87     addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx;
88 
89 #else
90 #define JUMPTARGET(name)	name
91 #define SYSCALL_PIC_SETUP	/* Nothing.  */
92 #endif
93 
94 #else /* __ASSEMBLER__ */
95 
96 # define SETUP_PIC_REG_STR(reg)						\
97   ".ifndef " GET_PC_THUNK_STR (reg) "\n"				\
98   ".section .text." GET_PC_THUNK_STR (reg) ",\"axG\",@progbits,"	\
99     GET_PC_THUNK_STR (reg) ",comdat\n"					\
100   ".globl " GET_PC_THUNK_STR (reg) "\n"					\
101   ".hidden " GET_PC_THUNK_STR (reg) "\n"				\
102   ".p2align 4\n"							\
103   ".type " GET_PC_THUNK_STR (reg) ",@function\n"			\
104 GET_PC_THUNK_STR (reg) ":"						\
105   "movl (%%esp), %%e" #reg "\n"						\
106   "ret\n"								\
107   ".size " GET_PC_THUNK_STR (reg) ", . - " GET_PC_THUNK_STR (reg) "\n"	\
108   ".previous\n"								\
109   ".endif\n"								\
110   "call " GET_PC_THUNK_STR (reg)
111 
112 # define LOAD_PIC_REG_STR(reg) \
113   SETUP_PIC_REG_STR (reg) "\naddl $_GLOBAL_OFFSET_TABLE_, %%e" #reg
114 
115 #endif	/* __ASSEMBLER__ */
116