2 /*--------------------------------------------------------------------*/
3 /*--- Trampoline code page stuff. m_trampoline.S ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2017 Julian Seward
12 Copyright (C) 2006-2017 OpenWorks LLP
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of the
18 License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, see <http://www.gnu.org/licenses/>.
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics_asm.h"
32 #include "pub_core_vkiscnums_asm.h"
34 /* ------------------ SIMULATED CPU HELPERS ------------------ */
36 Replacements for some functions to do with vsyscalls and signals.
37 This code runs on the simulated CPU.
40 /*---------------------- x86-linux ----------------------*/
41 #if defined(VGP_x86_linux)
43 # define UD2_16 ud2 ; ud2 ; ud2 ; ud2 ;ud2 ; ud2 ; ud2 ; ud2
44 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
45 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
46 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
47 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
49 /* a leading page of unexecutable code */
52 .global VG_(trampoline_stuff_start)
53 VG_(trampoline_stuff_start):
55 .global VG_(x86_linux_SUBST_FOR_sigreturn)
56 VG_(x86_linux_SUBST_FOR_sigreturn):
57 /* This is a very specific sequence which GDB uses to
58 recognize signal handler frames. Also gcc: see
59 x86_fallback_frame_state() in
60 gcc-4.1.0/gcc/config/i386/linux-unwind.h */
62 movl $ __NR_sigreturn, %eax
66 .global VG_(x86_linux_SUBST_FOR_rt_sigreturn)
67 VG_(x86_linux_SUBST_FOR_rt_sigreturn):
68 /* Likewise for rt signal frames */
69 movl $ __NR_rt_sigreturn, %eax
73 /* There's no particular reason that this needs to be handwritten
74 assembly, but since that's what this file contains, here's a
75 simple index implementation (written in C and compiled by gcc.)
77 unsigned char* REDIR_FOR_index ( const char* s, int c )
79 unsigned char ch = (unsigned char)((unsigned int)c);
80 unsigned char* p = (unsigned char*)s;
82 if (*p == ch) return p;
83 if (*p == 0) return 0;
88 .global VG_(x86_linux_REDIR_FOR_index)
89 .type VG_(x86_linux_REDIR_FOR_index), @function
90 VG_(x86_linux_REDIR_FOR_index):
111 .size VG_(x86_linux_REDIR_FOR_index), .-VG_(x86_linux_REDIR_FOR_index)
113 /* There's no particular reason that this needs to be handwritten
114 assembly, but since that's what this file contains, here's a
115 simple strlen implementation (written in C and compiled by gcc.)
117 .global VG_(x86_linux_REDIR_FOR_strlen)
118 .type VG_(x86_linux_REDIR_FOR_strlen), @function
119 VG_(x86_linux_REDIR_FOR_strlen):
131 .size VG_(x86_linux_REDIR_FOR_strlen), .-VG_(x86_linux_REDIR_FOR_strlen)
134 .global VG_(trampoline_stuff_end)
135 VG_(trampoline_stuff_end):
137 /* and a trailing page of unexecutable code */
146 /*---------------------- amd64-linux ----------------------*/
148 #if defined(VGP_amd64_linux)
150 # define UD2_16 ud2 ; ud2 ; ud2 ; ud2 ;ud2 ; ud2 ; ud2 ; ud2
151 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
152 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
153 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
154 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
156 /* a leading page of unexecutable code */
159 .global VG_(trampoline_stuff_start)
160 VG_(trampoline_stuff_start):
162 .global VG_(amd64_linux_SUBST_FOR_rt_sigreturn)
163 VG_(amd64_linux_SUBST_FOR_rt_sigreturn):
164 /* This is a very specific sequence which GDB uses to
165 recognize signal handler frames. */
166 movq $__NR_rt_sigreturn, %rax
170 .global VG_(amd64_linux_REDIR_FOR_vgettimeofday)
171 .type VG_(amd64_linux_REDIR_FOR_vgettimeofday), @function
172 VG_(amd64_linux_REDIR_FOR_vgettimeofday):
174 movq $__NR_gettimeofday, %rax
178 .size VG_(amd64_linux_REDIR_FOR_vgettimeofday), .-.LfnB2
180 .global VG_(amd64_linux_REDIR_FOR_vtime)
181 .type VG_(amd64_linux_REDIR_FOR_vtime), @function
182 VG_(amd64_linux_REDIR_FOR_vtime):
184 movq $__NR_time, %rax
188 .size VG_(amd64_linux_REDIR_FOR_vtime), .-.LfnB3
190 .global VG_(amd64_linux_REDIR_FOR_vgetcpu)
191 .type VG_(amd64_linux_REDIR_FOR_vgetcpu), @function
192 VG_(amd64_linux_REDIR_FOR_vgetcpu):
194 movq $__NR_getcpu, %rax
198 .size VG_(amd64_linux_REDIR_FOR_vgetcpu), .-.LfnB4
200 /* There's no particular reason that this needs to be handwritten
201 assembly, but since that's what this file contains, here's a
202 simple strlen implementation (written in C and compiled by gcc.)
204 .global VG_(amd64_linux_REDIR_FOR_strlen)
205 .type VG_(amd64_linux_REDIR_FOR_strlen), @function
206 VG_(amd64_linux_REDIR_FOR_strlen):
219 .size VG_(amd64_linux_REDIR_FOR_strlen), .-VG_(amd64_linux_REDIR_FOR_strlen)
221 /* There's no particular reason that this needs to be handwritten
222 assembly, but since that's what this file contains, here's a
225 .global VG_(amd64_linux_REDIR_FOR_strcmp)
226 .type VG_(amd64_linux_REDIR_FOR_strcmp), @function
227 VG_(amd64_linux_REDIR_FOR_strcmp):
230 movzbl (%rdi, %rcx), %eax
231 movzbl (%rsi, %rcx), %edx
242 .size VG_(amd64_linux_REDIR_FOR_strcmp), .-VG_(amd64_linux_REDIR_FOR_strcmp)
244 .global VG_(amd64_linux_REDIR_FOR_index)
245 .type VG_(amd64_linux_REDIR_FOR_index), @function
246 VG_(amd64_linux_REDIR_FOR_index):
265 .size VG_(amd64_linux_REDIR_FOR_index), .-VG_(amd64_linux_REDIR_FOR_index)
269 /* A CIE for the above four functions, followed by their FDEs */
270 .section .eh_frame,"a",@progbits
272 .long .LEcie1-.LScie1
290 .long .LEfde2-.LASfde2
292 .long .LASfde2-.Lframe1
299 .long .LEfde3-.LASfde3
301 .long .LASfde3-.Lframe1
308 .long .LEfde4-.LASfde4
310 .long .LASfde4-.Lframe1
317 .long .LEfde5-.LASfde5
319 .long .LASfde5-.Lframe1
327 .global VG_(trampoline_stuff_end)
328 VG_(trampoline_stuff_end):
330 /* and a trailing page of unexecutable code */
339 /*---------------- ppc32-linux ----------------*/
341 #if defined(VGP_ppc32_linux)
343 # define UD2_16 trap ; trap ; trap; trap
344 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
345 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
346 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
347 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
349 /* a leading page of unexecutable code */
352 .global VG_(trampoline_stuff_start)
353 VG_(trampoline_stuff_start):
355 .global VG_(ppc32_linux_SUBST_FOR_sigreturn)
356 VG_(ppc32_linux_SUBST_FOR_sigreturn):
359 .long 0 /*illegal insn*/
361 .global VG_(ppc32_linux_SUBST_FOR_rt_sigreturn)
362 VG_(ppc32_linux_SUBST_FOR_rt_sigreturn):
363 li 0,__NR_rt_sigreturn
365 .long 0 /*illegal insn*/
367 /* There's no particular reason that this needs to be handwritten
368 assembly, but since that's what this file contains, here's a
369 simple strlen implementation (written in C and compiled by gcc.)
371 .global VG_(ppc32_linux_REDIR_FOR_strlen)
372 .type VG_(ppc32_linux_REDIR_FOR_strlen), @function
373 VG_(ppc32_linux_REDIR_FOR_strlen):
386 .size VG_(ppc32_linux_REDIR_FOR_strlen), .-VG_(ppc32_linux_REDIR_FOR_strlen)
389 .global VG_(ppc32_linux_REDIR_FOR_strcmp)
390 .type VG_(ppc32_linux_REDIR_FOR_strcmp), @function
391 VG_(ppc32_linux_REDIR_FOR_strcmp):
424 .size VG_(ppc32_linux_REDIR_FOR_strcmp), .-VG_(ppc32_linux_REDIR_FOR_strcmp)
426 /* Ditto index/strchr */
427 .global VG_(ppc32_linux_REDIR_FOR_strchr)
428 .type VG_(ppc32_linux_REDIR_FOR_strchr), @function
429 VG_(ppc32_linux_REDIR_FOR_strchr):
448 .size VG_(ppc32_linux_REDIR_FOR_strchr),.-VG_(ppc32_linux_REDIR_FOR_strchr)
450 .global VG_(trampoline_stuff_end)
451 VG_(trampoline_stuff_end):
453 /* and a trailing page of unexecutable code */
462 /*---------------- ppc64-linux ----------------*/
464 #if defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
466 # define UD2_16 trap ; trap ; trap; trap
467 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
468 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
469 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
470 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
472 /* a leading page of unexecutable code */
475 .global VG_(trampoline_stuff_start)
476 VG_(trampoline_stuff_start):
478 .global VG_(ppc64_linux_SUBST_FOR_rt_sigreturn)
479 VG_(ppc64_linux_SUBST_FOR_rt_sigreturn):
480 li 0,__NR_rt_sigreturn
482 .long 0 /*illegal insn*/
484 /* See comment in pub_core_trampoline.h for what this is for */
485 .global VG_(ppctoc_magic_redirect_return_stub)
486 VG_(ppctoc_magic_redirect_return_stub):
489 /* this function is written using the "dotless" ABI convention */
491 .globl VG_(ppc64_linux_REDIR_FOR_strlen)
492 #if !defined VGP_ppc64be_linux || _CALL_ELF == 2
493 /* Little Endian uses ELF version 2 */
494 .type VG_(ppc64_linux_REDIR_FOR_strlen),@function
495 VG_(ppc64_linux_REDIR_FOR_strlen):
497 /* Big Endian uses ELF version 1 */
500 VG_(ppc64_linux_REDIR_FOR_strlen):
501 .quad .L.VG_(ppc64_linux_REDIR_FOR_strlen),.TOC.@tocbase,0
503 .size VG_(ppc64_linux_REDIR_FOR_strlen), \
504 .L0end-.L.VG_(ppc64_linux_REDIR_FOR_strlen)
505 .type VG_(ppc64_linux_REDIR_FOR_strlen), @function
507 .L.VG_(ppc64_linux_REDIR_FOR_strlen):
510 0: addis 2,12,.TOC.-0b@ha
512 .localentry VG_(ppc64_linux_REDIR_FOR_strlen), .-VG_(ppc64_linux_REDIR_FOR_strlen)
528 #if !defined VGP_ppc64be_linux || _CALL_ELF == 2
529 .size VG_(ppc64_linux_REDIR_FOR_strlen),.-VG_(ppc64_linux_REDIR_FOR_strlen)
531 .size VG_(ppc64_linux_REDIR_FOR_strlen),.-.L.VG_(ppc64_linux_REDIR_FOR_strlen)
534 .byte 0,0,0,0,0,0,0,0
537 /* this function is written using the "dotless" ABI convention */
539 .globl VG_(ppc64_linux_REDIR_FOR_strchr)
540 #if !defined VGP_ppc64be_linux || _CALL_ELF == 2
541 .type VG_(ppc64_linux_REDIR_FOR_strchr),@function
542 VG_(ppc64_linux_REDIR_FOR_strchr):
546 VG_(ppc64_linux_REDIR_FOR_strchr):
547 .quad .L.VG_(ppc64_linux_REDIR_FOR_strchr),.TOC.@tocbase,0
549 .size VG_(ppc64_linux_REDIR_FOR_strchr), \
550 .L1end-.L.VG_(ppc64_linux_REDIR_FOR_strchr)
551 .type VG_(ppc64_linux_REDIR_FOR_strchr),@function
553 .L.VG_(ppc64_linux_REDIR_FOR_strchr):
556 0: addis 2,12,.TOC.-0b@ha
558 .localentry VG_(ppc64_linux_REDIR_FOR_strchr), .-VG_(ppc64_linux_REDIR_FOR_strchr)
567 #if !defined VGP_ppc64be_linux || _CALL_ELF == 2
568 .size VG_(ppc64_linux_REDIR_FOR_strchr),.-VG_(ppc64_linux_REDIR_FOR_strchr)
570 .size VG_(ppc64_linux_REDIR_FOR_strchr),.-.L.VG_(ppc64_linux_REDIR_FOR_strchr)
584 .byte 0,0,0,0,0,0,0,0
588 .global VG_(trampoline_stuff_end)
589 VG_(trampoline_stuff_end):
591 /* and a trailing page of unexecutable code */
600 /*---------------- arm-linux ----------------*/
602 #if defined(VGP_arm_linux)
604 # define UD2_4 .word 0xFFFFFFFF
605 # define UD2_16 UD2_4 ; UD2_4 ; UD2_4 ; UD2_4
606 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
607 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
608 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
609 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
611 /* a leading page of unexecutable code */
614 .global VG_(trampoline_stuff_start)
615 VG_(trampoline_stuff_start):
617 .global VG_(arm_linux_SUBST_FOR_sigreturn)
618 .type VG_(arm_linux_SUBST_FOR_sigreturn),#function
619 VG_(arm_linux_SUBST_FOR_sigreturn):
620 mov r7, # __NR_sigreturn
622 .long 0xFFFFFFFF /*illegal insn*/
623 .size VG_(arm_linux_SUBST_FOR_sigreturn), .-VG_(arm_linux_SUBST_FOR_sigreturn)
625 .global VG_(arm_linux_SUBST_FOR_rt_sigreturn)
626 .type VG_(arm_linux_SUBST_FOR_rt_sigreturn),#function
627 VG_(arm_linux_SUBST_FOR_rt_sigreturn):
628 mov r7, # __NR_rt_sigreturn
630 .long 0xFFFFFFFF /*illegal insn*/
631 .size VG_(arm_linux_SUBST_FOR_rt_sigreturn), .-VG_(arm_linux_SUBST_FOR_rt_sigreturn)
633 .global VG_(arm_linux_REDIR_FOR_strlen)
634 VG_(arm_linux_REDIR_FOR_strlen):
636 ldrb r0, [r0, #0] @ zero_extendqisi2
637 @ lr needed for prologue
643 ldrb r3, [r0, r2] @ zero_extendqisi2
649 .global VG_(arm_linux_REDIR_FOR_index)
650 VG_(arm_linux_REDIR_FOR_index):
651 ldrb r3, [r0, #0] @ zero_extendqisi2
654 @ lr needed for prologue
658 ldrb r3, [r0, #1]! @ zero_extendqisi2
670 .global VG_(arm_linux_REDIR_FOR_memcpy)
671 VG_(arm_linux_REDIR_FOR_memcpy):
672 stmfd sp!, {r4, r5, lr}
687 ldrb r3, [r1, #4] @ zero_extendqisi2
690 ldrb r2, [r1, #3] @ zero_extendqisi2
693 ldrb r3, [r1, #2] @ zero_extendqisi2
696 ldrb r2, [r1, #1] @ zero_extendqisi2
708 ldrb r3, [r0], #-1 @ zero_extendqisi2
714 ldmfd sp!, {r4, r5, pc}
721 ldrb r3, [r1, #0] @ zero_extendqisi2
724 ldrb r2, [r1, #1] @ zero_extendqisi2
727 ldrb r3, [r1, #2] @ zero_extendqisi2
729 ldrb r2, [r1, #3] @ zero_extendqisi2
740 ldrb r3, [r1], #1 @ zero_extendqisi2
745 ldmfd sp!, {r4, r5, pc}
748 .global VG_(arm_linux_REDIR_FOR_strcmp)
749 VG_(arm_linux_REDIR_FOR_strcmp):
751 ldrb r3, [r0], #1 @ zero_extendqisi2
752 ldrb r2, [r1], #1 @ zero_extendqisi2
764 .global VG_(trampoline_stuff_end)
765 VG_(trampoline_stuff_end):
767 /* and a trailing page of unexecutable code */
777 /*---------------- arm64-linux ----------------*/
779 #if defined(VGP_arm64_linux)
781 # define UD2_4 .word 0xFFFFFFFF
782 # define UD2_16 UD2_4 ; UD2_4 ; UD2_4 ; UD2_4
783 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
784 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
785 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
786 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
788 /* a leading page of unexecutable code */
791 .global VG_(trampoline_stuff_start)
792 VG_(trampoline_stuff_start):
794 .global VG_(arm64_linux_SUBST_FOR_rt_sigreturn)
795 .type VG_(arm64_linux_SUBST_FOR_rt_sigreturn),#function
796 VG_(arm64_linux_SUBST_FOR_rt_sigreturn):
797 mov x8, # __NR_rt_sigreturn
799 .long 0xFFFFFFFF /*illegal insn*/
800 .size VG_(arm64_linux_SUBST_FOR_rt_sigreturn), \
801 .-VG_(arm64_linux_SUBST_FOR_rt_sigreturn)
803 .global VG_(arm64_linux_REDIR_FOR_strlen)
804 .type VG_(arm64_linux_REDIR_FOR_strlen),#function
805 VG_(arm64_linux_REDIR_FOR_strlen):
818 .size VG_(arm64_linux_REDIR_FOR_strlen), .-VG_(arm64_linux_REDIR_FOR_strlen)
820 .global VG_(arm64_linux_REDIR_FOR_index)
821 .type VG_(arm64_linux_REDIR_FOR_index),#function
822 VG_(arm64_linux_REDIR_FOR_index):
837 .size VG_(arm64_linux_REDIR_FOR_index), .-VG_(arm64_linux_REDIR_FOR_index)
839 .global VG_(arm64_linux_REDIR_FOR_strcmp)
840 .type VG_(arm64_linux_REDIR_FOR_strcmp),#function
841 VG_(arm64_linux_REDIR_FOR_strcmp):
862 .size VG_(arm64_linux_REDIR_FOR_strcmp), .-VG_(arm64_linux_REDIR_FOR_strcmp)
864 .global VG_(trampoline_stuff_end)
865 VG_(trampoline_stuff_end):
867 /* and a trailing page of unexecutable code */
877 /*---------------- x86-freebsd ----------------*/
879 #if defined(VGP_x86_freebsd)
881 .global VG_(trampoline_stuff_start)
882 VG_(trampoline_stuff_start):
884 .global VG_(x86_freebsd_SUBST_FOR_sigreturn)
885 VG_(x86_freebsd_SUBST_FOR_sigreturn):
887 * PJF this magic number is fairly flakey
888 * If ever it is wrong sys_fake_sigreturn will fail
889 * the sigreturn will be to some bogus EIP address and
890 * the client will likely terminate with a SIGILL
891 * In this case adding some printfs to sigframe_create
892 * and sys_fake_sigreturn should help debugging to
893 * find the right offset
895 lea 0x1c(%esp), %eax /* args to sigreturn(ucontext_t *) */
897 pushl %eax /* fake return addr */
898 /* movl 0x44(%eax), %gs ; restore %gs, not done by sigreturn */
899 movl $__NR_fake_sigreturn, %eax
903 .global VG_(trampoline_stuff_end)
904 VG_(trampoline_stuff_end):
907 #if defined(VGP_amd64_freebsd)
909 # define UD2_16 ud2 ; ud2 ; ud2 ; ud2 ;ud2 ; ud2 ; ud2 ; ud2
910 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
911 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
912 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
913 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
915 /* a leading page of unexecutable code */
918 .global VG_(trampoline_stuff_start)
919 VG_(trampoline_stuff_start):
921 .global VG_(amd64_freebsd_SUBST_FOR_sigreturn)
922 VG_(amd64_freebsd_SUBST_FOR_sigreturn):
923 /* This is a very specific sequence which GDB uses to
924 recognize signal handler frames. */
926 movq $__NR_fake_sigreturn, %rax
928 /* running the preprocessor on
929 * src/amd64.amd64/lib/libc/sigreturn.S
931 * .text; .p2align 4,0x90; .globl __sys_sigreturn;
932 * .type __sys_sigreturn,@function; __sys_sigreturn:;
933 * .cfi_startproc; .weak sigreturn; .equ sigreturn,__sys_sigreturn;
934 * .weak _sigreturn; .equ _sigreturn,__sys_sigreturn;
937 * syscall; jb .cerror; ret; .size __sys_sigreturn, . - __sys_sigreturn;
942 * sigframe in sigframe-amd64-freebsd.c
943 * differs from sigframe in /usr/include/x86/sigframe.h
944 * by having the address to this trampoline first
945 * so I assume that the +8 is to get back to the
953 .global VG_(trampoline_stuff_end)
954 VG_(trampoline_stuff_end):
956 /* and a trailing page of unexecutable code */
965 /*---------------- arm64-freebsd ----------------*/
967 #if defined(VGP_arm64_freebsd)
969 # define UD2_4 .word 0xFFFFFFFF
970 # define UD2_16 UD2_4 ; UD2_4 ; UD2_4 ; UD2_4
971 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
972 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
973 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
974 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
976 /* a leading page of unexecutable code */
979 .global VG_(trampoline_stuff_start)
980 VG_(trampoline_stuff_start):
982 .global VG_(arm64_freebsd_SUBST_FOR_sigreturn)
983 .type VG_(arm64_freebsd_SUBST_FOR_sigreturn),#function
984 VG_(arm64_freebsd_SUBST_FOR_sigreturn):
985 mov x8, # __NR_fake_sigreturn
988 .long 0xFFFFFFFF /*illegal insn*/
989 .size VG_(arm64_freebsd_SUBST_FOR_sigreturn), \
990 .-VG_(arm64_freebsd_SUBST_FOR_sigreturn)
992 .global VG_(trampoline_stuff_end)
993 VG_(trampoline_stuff_end):
995 /* and a trailing page of unexecutable code */
1007 /*---------------- x86-darwin ----------------*/
1009 #if defined(VGP_x86_darwin)
1011 /* a leading page of unexecutable code */
1012 .fill 2048, 2, 0x0b0f /* `ud2` */
1014 .globl VG_(trampoline_stuff_start)
1015 VG_(trampoline_stuff_start):
1017 .globl VG_(x86_darwin_SUBST_FOR_sigreturn)
1018 VG_(x86_darwin_SUBST_FOR_sigreturn):
1019 /* XXX does this need to have any special form? (cf x86-linux
1021 movl $ __NR_DARWIN_FAKE_SIGRETURN, %eax
1025 .globl VG_(x86_darwin_REDIR_FOR_strlen)
1026 VG_(x86_darwin_REDIR_FOR_strlen):
1038 .globl VG_(x86_darwin_REDIR_FOR_strcat)
1039 VG_(x86_darwin_REDIR_FOR_strcat):
1062 .globl VG_(x86_darwin_REDIR_FOR_strcmp)
1063 VG_(x86_darwin_REDIR_FOR_strcmp):
1083 .globl VG_(x86_darwin_REDIR_FOR_strcpy)
1084 VG_(x86_darwin_REDIR_FOR_strcpy):
1105 .globl VG_(x86_darwin_REDIR_FOR_strlcat)
1106 VG_(x86_darwin_REDIR_FOR_strlcat):
1115 leal (%ecx,%esi), %eax
1135 call VG_(x86_darwin_REDIR_FOR_strlen)
1153 leal (%edi,%eax), %eax
1160 .globl VG_(trampoline_stuff_end)
1161 VG_(trampoline_stuff_end):
1163 /* a trailing page of unexecutable code */
1164 .fill 2048, 2, 0x0b0f /* `ud2` */
1167 /*---------------- amd64-darwin ----------------*/
1169 #if defined(VGP_amd64_darwin)
1171 /* a leading page of unexecutable code */
1172 .fill 2048, 2, 0x0b0f /* `ud2` */
1174 .globl VG_(trampoline_stuff_start)
1175 VG_(trampoline_stuff_start):
1177 .globl VG_(amd64_darwin_SUBST_FOR_sigreturn)
1178 VG_(amd64_darwin_SUBST_FOR_sigreturn):
1179 /* XXX does this need to have any special form? (cf x86-linux
1181 movq $ __NR_DARWIN_FAKE_SIGRETURN, %rax
1185 .globl VG_(amd64_darwin_REDIR_FOR_strlen)
1186 VG_(amd64_darwin_REDIR_FOR_strlen):
1197 .globl VG_(amd64_darwin_REDIR_FOR_strcat)
1198 VG_(amd64_darwin_REDIR_FOR_strcat):
1217 .globl VG_(amd64_darwin_REDIR_FOR_strcmp)
1218 VG_(amd64_darwin_REDIR_FOR_strcmp):
1235 .globl VG_(amd64_darwin_REDIR_FOR_strcpy)
1236 VG_(amd64_darwin_REDIR_FOR_strcpy):
1253 .globl VG_(amd64_darwin_REDIR_FOR_strlcat)
1254 VG_(amd64_darwin_REDIR_FOR_strlcat):
1256 leaq (%rdx,%rdi), %rax
1279 call VG_(amd64_darwin_REDIR_FOR_strlen)
1296 leaq (%rbx,%rax), %rax
1302 .globl VG_(amd64_darwin_REDIR_FOR_arc4random)
1303 VG_(amd64_darwin_REDIR_FOR_arc4random):
1304 /* not very random, hope dyld won't mind */
1305 movq $0x76616c6772696e64, %rax
1308 .globl VG_(amd64_darwin_REDIR_FOR_strchr)
1309 VG_(amd64_darwin_REDIR_FOR_strchr):
1331 .globl VG_(trampoline_stuff_end)
1332 VG_(trampoline_stuff_end):
1334 /* a trailing page of unexecutable code */
1335 .fill 2048, 2, 0x0b0f /* `ud2` */
1338 /*---------------- s390x-linux ----------------*/
1340 #if defined(VGP_s390x_linux)
1342 /* a leading page of unexecutable code */
1343 .fill 2048, 2, 0x0000
1345 .global VG_(trampoline_stuff_start)
1346 VG_(trampoline_stuff_start):
1348 .global VG_(s390x_linux_SUBST_FOR_sigreturn)
1349 VG_(s390x_linux_SUBST_FOR_sigreturn):
1353 .global VG_(s390x_linux_SUBST_FOR_rt_sigreturn)
1354 VG_(s390x_linux_SUBST_FOR_rt_sigreturn):
1355 /* Old gcc unwinding code checks for a sig(_rt)_return svc and then
1356 for ra = cfa to decide if it is a sig_rt_frame or not. Since we
1357 set ra to this trampoline, but the cfa is still in the stack,
1358 the unwinder thinks, that this is a non-rt frame and causes a
1359 crash in the gcc unwinder - which is used by the thread library
1360 and others. Therefore we add a lr 1,1 nop, to let the gcc
1361 unwinder bail out gracefully. This might also affect unwinding
1362 across the signal frame - tough luck. fixs390 */
1364 svc __NR_rt_sigreturn
1367 .global VG_(s390x_linux_REDIR_FOR_index)
1368 .type VG_(s390x_linux_REDIR_FOR_index),@function
1369 VG_(s390x_linux_REDIR_FOR_index):
1371 # %r2 = address of string
1372 # %r3 = character to find
1375 ngr %r0,%r3 # r0 = (unsigned char)r3
1378 llgc %r1,0(%r2) # r1 = byte from string
1379 cr %r1,%r0 # compare
1380 ber %r14 # return if found
1381 cr %r1,%r4 # end of string ?
1383 aghi %r2,1 # increment r2
1385 .L2: lghi %r2,0 # return value 0
1387 .size VG_(s390x_linux_REDIR_FOR_index), .-VG_(s390x_linux_REDIR_FOR_index)
1389 .globl VG_(trampoline_stuff_end)
1390 VG_(trampoline_stuff_end):
1391 .fill 2048, 2, 0x0000
1393 /*---------------------- mips32-linux ----------------------*/
1395 #if defined(VGP_mips32_linux)
1397 # define UD2_16 trap ; trap ; trap; trap
1398 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
1399 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
1400 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
1401 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
1404 .global VG_(trampoline_stuff_start)
1405 VG_(trampoline_stuff_start):
1407 .global VG_(mips32_linux_SUBST_FOR_sigreturn)
1408 VG_(mips32_linux_SUBST_FOR_sigreturn):
1409 li $v0,__NR_sigreturn
1412 .long 0 /*illegal insn*/
1414 .global VG_(mips32_linux_SUBST_FOR_rt_sigreturn)
1415 VG_(mips32_linux_SUBST_FOR_rt_sigreturn):
1416 li $v0,__NR_rt_sigreturn
1419 .long 0 /*illegal insn*/
1421 /* There's no particular reason that this needs to be handwritten
1422 assembly, but since that's what this file contains, here's a
1423 simple index() and strlen() implementations.
1429 .global VG_(mips32_linux_REDIR_FOR_index)
1430 .type VG_(mips32_linux_REDIR_FOR_index), @function
1431 VG_(mips32_linux_REDIR_FOR_index):
1435 beq $t0, $a1, index_end
1437 bne $t0, $zero, index_loop
1443 .size VG_(mips32_linux_REDIR_FOR_index), .-VG_(mips32_linux_REDIR_FOR_index)
1445 .global VG_(mips32_linux_REDIR_FOR_strlen)
1446 .type VG_(mips32_linux_REDIR_FOR_strlen), @function
1447 VG_(mips32_linux_REDIR_FOR_strlen):
1451 bne $t0, $zero, strlen_loop
1456 .size VG_(mips32_linux_REDIR_FOR_strlen), .-VG_(mips32_linux_REDIR_FOR_strlen)
1460 .global VG_(trampoline_stuff_end)
1461 VG_(trampoline_stuff_end):
1470 /*---------------------- nanomips-linux --------------------*/
1472 #if defined(VGP_nanomips_linux)
1474 .global VG_(trampoline_stuff_start)
1475 VG_(trampoline_stuff_start):
1480 .global VG_(nanomips_linux_SUBST_FOR_rt_sigreturn)
1481 VG_(nanomips_linux_SUBST_FOR_rt_sigreturn):
1482 li $t4, __NR_rt_sigreturn
1486 .global VG_(nanomips_linux_REDIR_FOR_index)
1487 .type VG_(nanomips_linux_REDIR_FOR_index), @function
1488 VG_(nanomips_linux_REDIR_FOR_index):
1491 beqc $t0, $a1, index_end
1493 bnec $t0, $zero, index_loop
1497 .size VG_(nanomips_linux_REDIR_FOR_index), .-VG_(nanomips_linux_REDIR_FOR_index)
1499 .global VG_(nanomips_linux_REDIR_FOR_strlen)
1500 .type VG_(nanomips_linux_REDIR_FOR_strlen), @function
1501 VG_(nanomips_linux_REDIR_FOR_strlen):
1506 bnec $t0, $zero, strlen_loop
1511 .size VG_(nanomips_linux_REDIR_FOR_strlen), .-VG_(nanomips_linux_REDIR_FOR_strlen)
1515 .global VG_(trampoline_stuff_end)
1516 VG_(trampoline_stuff_end):
1519 /*---------------------- mips64-linux ----------------------*/
1521 #if defined(VGP_mips64_linux)
1523 # define UD2_16 trap ; trap ; trap; trap
1524 # define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
1525 # define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
1526 # define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
1527 # define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
1529 .global VG_(trampoline_stuff_start)
1530 VG_(trampoline_stuff_start):
1532 .global VG_(mips64_linux_SUBST_FOR_rt_sigreturn)
1533 VG_(mips64_linux_SUBST_FOR_rt_sigreturn):
1534 li $2,__NR_rt_sigreturn
1537 .long 0 /*illegal insn*/
1539 /* There's no particular reason that this needs to be handwritten
1540 assembly, but since that's what this file contains, here's a
1541 simple index() and strlen() implementations.
1547 .global VG_(mips64_linux_REDIR_FOR_index)
1548 .type VG_(mips64_linux_REDIR_FOR_index), @function
1549 VG_(mips64_linux_REDIR_FOR_index):
1553 beq $t0, $a1, index_end
1555 bne $t0, $zero, index_loop
1561 .size VG_(mips64_linux_REDIR_FOR_index), .-VG_(mips64_linux_REDIR_FOR_index)
1563 .global VG_(mips64_linux_REDIR_FOR_strlen)
1564 .type VG_(mips64_linux_REDIR_FOR_strlen), @function
1565 VG_(mips64_linux_REDIR_FOR_strlen):
1569 bne $t0, $zero, strlen_loop
1574 .size VG_(mips64_linux_REDIR_FOR_strlen), .-VG_(mips64_linux_REDIR_FOR_strlen)
1578 .global VG_(trampoline_stuff_end)
1579 VG_(trampoline_stuff_end):
1588 /*---------------- x86-solaris ----------------*/
1590 #if defined(VGP_x86_solaris)
1592 .global VG_(trampoline_stuff_start)
1593 VG_(trampoline_stuff_start):
1595 /* int strcmp(const char *s1, const char *s2); */
1596 .global VG_(x86_solaris_REDIR_FOR_strcmp)
1597 .type VG_(x86_solaris_REDIR_FOR_strcmp), @function
1598 VG_(x86_solaris_REDIR_FOR_strcmp):
1599 pushl %ebp /* establish a stack frame */
1601 movl 8(%ebp), %edx /* get s1 */
1602 movl 12(%esp), %ecx /* get s2 */
1603 jmp 2f /* go compare the first characters */
1605 incl %edx /* skip to the next s1 character */
1606 incl %ecx /* skip to the next s2 character */
1608 movzbl (%edx), %eax /* load a character from s1 */
1609 testb %al, %al /* is it null? */
1610 jz 3f /* yes, exit */
1611 cmpb (%ecx), %al /* are the characters equal? */
1612 je 1b /* yes, proceed with next characters */
1614 movzbl (%ecx), %edx /* load a character from s2 */
1615 subl %edx, %eax /* calculate the return value */
1616 popl %ebp /* destroy the stack frame */
1617 ret /* return to the caller */
1618 .size VG_(x86_solaris_REDIR_FOR_strcmp), .-VG_(x86_solaris_REDIR_FOR_strcmp)
1620 /* size_t strlen(const char *s); */
1621 .global VG_(x86_solaris_REDIR_FOR_strlen)
1622 .type VG_(x86_solaris_REDIR_FOR_strlen), @function
1623 VG_(x86_solaris_REDIR_FOR_strlen):
1624 pushl %ebp /* establish a stack frame */
1626 movl 8(%ebp), %edx /* get s */
1627 movl %edx, %eax /* copy s */
1628 jmp 2f /* go handle the first character */
1630 incl %eax /* skip to the next s character */
1632 cmpb $0, (%eax) /* is the s character null? */
1633 jne 1b /* no, go process the next character */
1634 subl %edx, %eax /* calculate the return value */
1635 popl %ebp /* destroy the stack frame */
1636 ret /* return to the caller */
1637 .size VG_(x86_solaris_REDIR_FOR_strlen), .-VG_(x86_solaris_REDIR_FOR_strlen)
1639 .global VG_(trampoline_stuff_end)
1640 VG_(trampoline_stuff_end):
1642 /*---------------- amd64-solaris ----------------*/
1644 #if defined(VGP_amd64_solaris)
1646 .global VG_(trampoline_stuff_start)
1647 VG_(trampoline_stuff_start):
1649 /* char *strcpy(char *restrict s1, const char *restrict s2); */
1650 .global VG_(amd64_solaris_REDIR_FOR_strcpy)
1651 .type VG_(amd64_solaris_REDIR_FOR_strcpy), @function
1652 VG_(amd64_solaris_REDIR_FOR_strcpy):
1653 pushq %rbp /* establish a stack frame */
1655 movq %rdi, %rdx /* copy s1 */
1657 movzbl (%rsi), %eax /* load one input character */
1658 movb %al, (%rdx) /* copy to output/s1 */
1659 incq %rsi /* skip to the next output character */
1660 incq %rdx /* skip to the next input character */
1661 testb %al, %al /* is the copied character null? */
1662 jnz 1b /* no, copy the next character */
1663 leave /* destroy the stack frame */
1664 movq %rdi, %rax /* set s1 as the return value */
1665 ret /* return to the caller */
1666 .size VG_(amd64_solaris_REDIR_FOR_strcpy), .-VG_(amd64_solaris_REDIR_FOR_strcpy)
1668 /* char *strncpy(char *restrict s1, const char *restrict s2, size_t n); */
1669 .global VG_(amd64_solaris_REDIR_FOR_strncpy)
1670 .type VG_(amd64_solaris_REDIR_FOR_strncpy), @function
1671 VG_(amd64_solaris_REDIR_FOR_strncpy):
1672 pushq %rbp /* establish a stack frame */
1674 movq %rdi, %rcx /* copy s1 */
1676 testq %rdx, %rdx /* is the remaining size zero? */
1677 jz 3f /* yes, all done */
1678 movzbl (%rsi), %eax /* load one input character */
1679 movb %al, (%rcx) /* copy to output/s1 */
1680 decq %rdx /* decrement the remaining size */
1681 incq %rsi /* skip to the next output character */
1682 incq %rcx /* skip to the next input character */
1683 testb %al, %al /* is the copied character null? */
1684 jnz 1b /* no, copy the next character */
1686 testq %rdx, %rdx /* is the remaining size zero? */
1687 jz 3f /* yes, all done */
1688 movb $0, (%rdx) /* copy null to output/s2 */
1689 decq %rdx /* decrement the remaining size */
1690 incq %rsi /* skip to next output character */
1691 jmp 2b /* proceed with the next character */
1693 leave /* destroy the stack frame */
1694 movq %rdi, %rax /* set s1 as the return value */
1695 ret /* return to the caller */
1696 .size VG_(amd64_solaris_REDIR_FOR_strncpy), .-VG_(amd64_solaris_REDIR_FOR_strncpy)
1698 /* int strcmp(const char *s1, const char *s2); */
1699 .global VG_(amd64_solaris_REDIR_FOR_strcmp)
1700 .type VG_(amd64_solaris_REDIR_FOR_strcmp), @function
1701 VG_(amd64_solaris_REDIR_FOR_strcmp):
1702 pushq %rbp /* establish a stack frame */
1704 jmp 2f /* go compare the first characters */
1706 incq %rdi /* skip to the next s1 character */
1707 incq %rsi /* skip to the next s2 character */
1709 movzbl (%rdi), %eax /* load a character from s1 */
1710 testb %al, %al /* is it null? */
1711 jz 3f /* yes, exit */
1712 cmpb (%rsi), %al /* are the characters equal? */
1713 je 1b /* yes, proceed with next characters */
1715 movzbl (%rsi), %edx /* load a character from s2 */
1716 subl %edx, %eax /* calculate the return value */
1717 leave /* destroy the stack frame */
1718 ret /* return to the caller */
1719 .size VG_(amd64_solaris_REDIR_FOR_strcmp), .-VG_(amd64_solaris_REDIR_FOR_strcmp)
1721 /* char *strcat(char *restrict s1, const char *restrict s2); */
1722 .global VG_(amd64_solaris_REDIR_FOR_strcat)
1723 .type VG_(amd64_solaris_REDIR_FOR_strcat), @function
1724 VG_(amd64_solaris_REDIR_FOR_strcat):
1725 pushq %rbp /* establish a stack frame */
1727 movq %rdi, %rdx /* copy s1 */
1728 jmp 2f /* go handle the first character */
1730 incq %rdx /* skip to the next s1 character */
1732 cmpb $0, (%rdx) /* is the s1 character null? */
1733 jne 1b /* no, go check the next character */
1735 movzbl (%rsi), %eax /* load a character from s2 */
1736 movb %al, (%rdx) /* copy the s2 character to s1 */
1737 incq %rdx /* skip to the next s1 character */
1738 incq %rsi /* skip to the next s2 character */
1739 testb %al, %al /* was the character null? */
1740 jnz 3b /* no, go copy the next character */
1741 movq %rdi, %rax /* set s1 as the return value */
1742 leave /* destroy the stack frame */
1743 ret /* return to the caller */
1744 .size VG_(amd64_solaris_REDIR_FOR_strcat), .-VG_(amd64_solaris_REDIR_FOR_strcat)
1746 /* size_t strlen(const char *s); */
1747 .global VG_(amd64_solaris_REDIR_FOR_strlen)
1748 .type VG_(amd64_solaris_REDIR_FOR_strlen), @function
1749 VG_(amd64_solaris_REDIR_FOR_strlen):
1750 pushq %rbp /* establish a stack frame */
1752 movq %rdi, %rax /* copy s */
1753 jmp 2f /* go handle the first character */
1755 incq %rax /* skip to the next s character */
1757 cmpb $0, (%rax) /* is the s character null? */
1758 jne 1b /* no, go process the next character */
1759 subq %rdi, %rax /* calculate the return value */
1760 leave /* destroy the stack frame */
1761 ret /* return to the caller */
1762 .size VG_(amd64_solaris_REDIR_FOR_strlen), .-VG_(amd64_solaris_REDIR_FOR_strlen)
1764 .global VG_(trampoline_stuff_end)
1765 VG_(trampoline_stuff_end):
1767 /*---------------- unknown ----------------*/
1769 # error Unknown platform
1789 /* Let the linker know we don't need an executable stack */
1792 /*--------------------------------------------------------------------*/
1794 /*--------------------------------------------------------------------*/