Home | History | Annotate | Line # | Download | only in gdb.arch
riscv64-unwind-prologue-with-ld-lw-foo.s revision 1.1.1.1.4.2
      1  1.1.1.1.4.2  perseant /* Copyright 2021-2023 Free Software Foundation, Inc.
      2  1.1.1.1.4.2  perseant 
      3  1.1.1.1.4.2  perseant    This program is free software; you can redistribute it and/or modify
      4  1.1.1.1.4.2  perseant    it under the terms of the GNU General Public License as published by
      5  1.1.1.1.4.2  perseant    the Free Software Foundation; either version 3 of the License, or
      6  1.1.1.1.4.2  perseant    (at your option) any later version.
      7  1.1.1.1.4.2  perseant 
      8  1.1.1.1.4.2  perseant    This program is distributed in the hope that it will be useful,
      9  1.1.1.1.4.2  perseant    but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  1.1.1.1.4.2  perseant    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  1.1.1.1.4.2  perseant    GNU General Public License for more details.
     12  1.1.1.1.4.2  perseant 
     13  1.1.1.1.4.2  perseant    You should have received a copy of the GNU General Public License
     14  1.1.1.1.4.2  perseant    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     15  1.1.1.1.4.2  perseant 
     16  1.1.1.1.4.2  perseant /* This testcase contains a function where the 'ld', 'c.ld', 'lw' or 'c.lw'
     17  1.1.1.1.4.2  perseant    instruction is used in the prologue before the RA register have been saved
     18  1.1.1.1.4.2  perseant    on the stack.
     19  1.1.1.1.4.2  perseant 
     20  1.1.1.1.4.2  perseant    This mimics a pattern observed in the __pthread_clockjoin_ex function
     21  1.1.1.1.4.2  perseant    in libpthread.so.0 (from glibc-2.33-0ubuntu5) where a canary value is
     22  1.1.1.1.4.2  perseant    loaded and placed on the stack in order to detect stack smashing.
     23  1.1.1.1.4.2  perseant 
     24  1.1.1.1.4.2  perseant    The skeleton for this file was generated using the following command:
     25  1.1.1.1.4.2  perseant 
     26  1.1.1.1.4.2  perseant       gcc -x c -S -c -o - - <<EOT
     27  1.1.1.1.4.2  perseant         static long int __canary = 42;
     28  1.1.1.1.4.2  perseant         extern int bar ();
     29  1.1.1.1.4.2  perseant         int foo () { return bar(); }
     30  1.1.1.1.4.2  perseant       EOT
     31  1.1.1.1.4.2  perseant 
     32  1.1.1.1.4.2  perseant    The result of this command is modified in the following way:
     33  1.1.1.1.4.2  perseant      - The prologue is adapted to reserve 16 more bytes on the stack.
     34  1.1.1.1.4.2  perseant      - A part that simulates the installation of a canary on the stack is
     35  1.1.1.1.4.2  perseant        added.  The canary is loaded multiple times to simulate the use of
     36  1.1.1.1.4.2  perseant        various instructions that could do the work (ld or c.ld for a 64 bit
     37  1.1.1.1.4.2  perseant        canary, lw or c.lw for a 32 bit canary).
     38  1.1.1.1.4.2  perseant      - The epilogue is adjusted to be able to return properly.  The epilogue
     39  1.1.1.1.4.2  perseant        does not check the canary value since this testcase is only interested
     40  1.1.1.1.4.2  perseant        in ensuring GDB can scan the prologue.  */
     41  1.1.1.1.4.2  perseant 
     42  1.1.1.1.4.2  perseant 	.option pic
     43  1.1.1.1.4.2  perseant 	.text
     44  1.1.1.1.4.2  perseant 	.data
     45  1.1.1.1.4.2  perseant 	.align	3
     46  1.1.1.1.4.2  perseant 	.type	__canary, @object
     47  1.1.1.1.4.2  perseant 	.size	__canary, 8
     48  1.1.1.1.4.2  perseant __canary:
     49  1.1.1.1.4.2  perseant 	.dword	42
     50  1.1.1.1.4.2  perseant 	.text
     51  1.1.1.1.4.2  perseant 	.align	1
     52  1.1.1.1.4.2  perseant 	.globl	foo
     53  1.1.1.1.4.2  perseant 	.type	foo, @function
     54  1.1.1.1.4.2  perseant foo:
     55  1.1.1.1.4.2  perseant 	addi	sp,sp,-32
     56  1.1.1.1.4.2  perseant 	lla	a5,__canary  # Load the fake canary address.
     57  1.1.1.1.4.2  perseant 	lw	t4,0(a5)     # Load a 32 bit canary (use t4 to force the use of
     58  1.1.1.1.4.2  perseant 			     # the non compressed instruction).
     59  1.1.1.1.4.2  perseant 	ld	t4,0(a5)     # Load a 64 bit canary (use t4to force the use of
     60  1.1.1.1.4.2  perseant 			     # the non compressed instruction).
     61  1.1.1.1.4.2  perseant 	c.lw 	a4,0(a5)     # Load a 32 bit canary using the compressed insn.
     62  1.1.1.1.4.2  perseant 	c.ld 	a4,0(a5)     # Load a 64 bit canary using the compressed insn.
     63  1.1.1.1.4.2  perseant 	sd	a4,0(sp)     # Place the fake canary on the stack.
     64  1.1.1.1.4.2  perseant 	sd	ra,16(sp)
     65  1.1.1.1.4.2  perseant 	sd	s0,8(sp)
     66  1.1.1.1.4.2  perseant 	addi	s0,sp,32
     67  1.1.1.1.4.2  perseant 	call	bar@plt
     68  1.1.1.1.4.2  perseant 	mv	a5,a0
     69  1.1.1.1.4.2  perseant 	mv	a0,a5
     70  1.1.1.1.4.2  perseant 	ld	ra,16(sp)
     71  1.1.1.1.4.2  perseant 	ld	s0,8(sp)
     72  1.1.1.1.4.2  perseant 	addi	sp,sp,32
     73  1.1.1.1.4.2  perseant 	jr	ra
     74  1.1.1.1.4.2  perseant 	.size	foo, .-foo
     75  1.1.1.1.4.2  perseant 	.section	.note.GNU-stack,"",@progbits
     76