Surprised by odd (unaligned) entrypoint into dylib

I've been exploring dynamic linking, and tried the following:

#include <stdio.h>

int main() {
  printf("Address of main:   %lx\n", (unsigned long)main);
  printf("Address of printf: %lx\n", (unsigned long)printf);
  return 0;
}

When I run this, I get

Address of main:   107264f20
Address of printf: 7ff81651af0b

The address of printf loaded from the GOT is in the shared library, but it is odd. I was expecting it to be 8-byte aligned.

I must be missing something obvious, but what?

I presume you’re testing this on Intel. In that case, the result you’re seeing is perfectly reasonable. Intel instructions do not have alignment requirements [1].

I set a breakpoint on the return statement in your code and saw this:

Address of main:   100003f20
Address of printf: 7ff81d13df0b
(lldb) disas -s 7ff81d13df0b
libsystem_c.dylib`printf:
    0x7ff81d13df0b <+0>:  pushq  %rbp
    0x7ff81d13df0c <+1>:  movq   %rsp, %rbp
    0x7ff81d13df0f <+4>:  subq   $0xd0, %rsp
    0x7ff81d13df16 <+11>: movq   %rdi, %r10
    0x7ff81d13df19 <+14>: testb  %al, %al

As you can see, printf just happens to start at an odd address [2]. That’s perfectly normal an Intel.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Well, there may be some obscure ones that do — I’m not enough of an expert on that ISA to say for sure one way or the other — so I’m just talking about the standard instructions you use day-to-day.

[2] The specific address is different due to ASLR.

Surprised by odd (unaligned) entrypoint into dylib
 
 
Q