Re:Tearing apart printf() (2018)
by matheusmoreira on Tuesday November 12, 2019 @04:22PM
(#21518307)
The registers rdi, rsi and rdx are acknowledged in the article but they don't appear in the disassembly:
This is because they are set when the write function is called. The System V AMD64 ABI specifies that rdi, rsi and rdx are used for the 1st, 2nd and 3rd arguments of a function, perfectly matching the Linux system call ABI. The 5th and 6th also match: r8 and r9, respectively. However, the 4th argument doesn't match: system calls use r10 while System V uses rcx. I wish I knew why.
[ Parent
| on HN ]
Re:Tearing apart printf() (2018)
by gamozolabs on Tuesday November 12, 2019 @04:46PM
(#21518513)
The x86 `syscall` instruction stores the return address into `rcx` as well as `RFLAGS` into `r11`. These are unconditionally clobbered and thus cannot be saved or used in a `syscall` transition.
Isn't that disassembly a replacement of the call to printf with a call to the write system call? Most compilers have the printf family as builtins to allow removing them from the code entirely: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html