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