How to compile an ELF with matched calls and returns?

52 views
Skip to first unread message

Jeff Li

unread,
Oct 19, 2017, 9:28:46 PM10/19/17
to RISC-V SW Dev

Hello, I'm trying to develop something like a hardware supported shadow stack, and I want to test it with normal programs. So how to compile ELFs with totally matched calls and returns? Here are the details: I'm using riscv64-unknown-elf-gcc to compile source code and run them on FPGA. 

I was told the gcc will generate matched calls and returns by default, and it seems right in the object it self(by both llvm and gcc), but not in libraries.

So first question is: why it's not matched in libraries? optimization? Then how can I recompile all libraries with new optimization arguments, or llvm(in riscv-llvm, the guide in README only use clang to generate helloworld.S, the libraries are not changed)?

I've tested a simple HelloWorld.c and found the ELF is linked with flowing parts:
  1. crt0.o, crtbegin.o, crtend.o
  2. hello.o itself
  3. libraries in $RISCV/lib/gcc/riscv64-unknown-elf/6.1.0 and $RISCV/riscv64-unknown-elf/lib
Which part is more possible to contain unmatched calls and returns? What should I try next?

Thanks.

Sober Liu

unread,
Oct 20, 2017, 3:42:46 AM10/20/17
to Jeff Li, RISC-V SW Dev

Maybe search tail pseudo in spec doc.

--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.
To post to this group, send email to sw-...@groups.riscv.org.
Visit this group at https://groups.google.com/a/groups.riscv.org/group/sw-dev/.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/eed84536-1856-46dd-a6dc-fd4e682d87b2%40groups.riscv.org.


This email message is for the sole use of the intended recipient(s) and may contain confidential information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.

Jeff Li

unread,
Oct 20, 2017, 4:19:10 AM10/20/17
to RISC-V SW Dev
Thanks.

But tail pseudo is ended with jalr x0, x6, offset. So it seems it will not lead to unmatched call and ret? I'm not sure.

And further, I want to recompile the libraries with -O0,(rewrite them maybe) and see what happen, but I don't know how. I found CCFLAGS in configure in the riscv-tools, but I think the flag is used by gcc to compile riscv64-unknown-elf-gcc, not by riscv64-unknown-elf-gcc to compile libraries. Did I misunderstand?

在 2017年10月20日星期五 UTC+8下午3:42:46,soberl写道:

Andrew Waterman

unread,
Oct 20, 2017, 4:24:54 AM10/20/17
to Sober Liu, Jeff Li, RISC-V SW Dev
Tail calls don't generally lead to unmached call-return pairs; the
tail-callee's return will match the original call.

The main thing that screws up the return-address stack is context
switching (in the kernel, or in userspace via longjmp/setcontext).
> https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/0a54f828cf13497e837950fba737940c%40HKMAIL101.nvidia.com.

Jeff Li

unread,
Oct 20, 2017, 4:39:59 AM10/20/17
to RISC-V SW Dev, sob...@nvidia.com, read...@gmail.com
Are all source code of libs in $RISCV/riscv-gnu-toolchain/newlib? 

I found (by grep) "longjmp" only in setjmp.S, and no "setcontext". Does it means there is no unmatched  in libraries? So, I should check crt ?

Or some system calls often used will screw up?

I'm in a mess...

Thank you

在 2017年10月20日星期五 UTC+8下午4:24:54,waterman写道:

Sober Liu

unread,
Oct 20, 2017, 5:13:04 AM10/20/17
to Jeff Li, RISC-V SW Dev

OK. Maybe I made misunderstanding. I had thought the unmatched means several calls end up with single return.

Reply all
Reply to author
Forward
0 new messages