0

i call glibc function, for example, printf:

mov $some_string, %rdi
call printf   //<----crash

in some cases, such a call will crash. I add xor %rax, %rax this solves the problem.

I have two questions:

  1. Why, in some cases, calling printf without xor %rax leads to a crash, and in some it does not?

  2. Somewhere I saw such a call printf:

    lea  some_string(%rip), %rdi
    xor %eax, %eax
    call printf
    

Why this used rip register and how is this passing of an argument different from the usual?

Jester
  • 56,577
  • 4
  • 81
  • 125
xperious
  • 239
  • 3
  • 10
  • 3
    1) `printf` expects stack to be aligned to 16 bytes. If you don't do that, sometimes it will be aligned, other times it will not. If `eax` is not zero, `printf` will try to access arguments in `xmm` registers and may use instructions that require alignment. 2) that's just position independent rip-relative addressing – Jester Jan 09 '22 at 12:01
  • 1
    [This answer](https://stackoverflow.com/questions/52714408/why-is-rax-not-used-to-pass-a-parameter-in-system-v-amd64-abi/52724015#52724015) describes how `%rax` is used for variadic functions, such as `printf`. – Brett Hale Jan 09 '22 at 12:23
  • @Jester 1) hm, how to set align just for stack? not for all program 2) why write like that? Is there any reason? – xperious Jan 09 '22 at 15:42

0 Answers0