1

コードで関数のアドレスを確認したいので、次のように hello world を記述します。

#include <stdio.h>

void myfn() {
  printf("I am myfn1\n");
  printf("I am myfn2\n");
  printf("I am myfn3\n");
  printf("I am myfn4\n");
  printf("I am myfn5\n");
}

typedef void (*MYFN)();

int main() {
  MYFN fn = (MYFN)myfn;
  printf("addr of fn: 0x%08X\n", (unsigned int)fn);
  fn();
  printf("just for %s\n", "test");
  return 0;
}

結果は次のとおりです。

# ./test
addr of fn: 0x00008461
I am myfn1
I am myfn2
I am myfn3
I am myfn4
I am myfn5
just for test

では、myfn のアドレスは0x00008461ですか?

次に、objdump を使用してダンプします。

84ae:   f7ff efb8   blx 8420 <printf@plt>
84b2:   f7ff ffd5   bl  8460 <printf@plt+0x40>
84b6:   4807        ldr r0, [pc, #28]   ; (84d4 <printf@plt+0xb4>)
84b8:   4907        ldr r1, [pc, #28]   ; (84d8 <printf@plt+0xb8>)
84ba:   4478        add r0, pc
84bc:   4479        add r1, pc
84be:   f7ff efb0   blx 8420 <printf@plt>

そこから myfn のアドレスは0x8460 ? その近く:

8460:   480a        ldr r0, [pc, #40]   ; (848c <printf@plt+0x6c>)
8462:   b510        push    {r4, lr}
8464:   4478        add r0, pc
8466:   f7ff efd6   blx 8414 <puts@plt>
846a:   4809        ldr r0, [pc, #36]   ; (8490 <printf@plt+0x70>)
846c:   4478        add r0, pc
846e:   f7ff efd2   blx 8414 <puts@plt>
8472:   4808        ldr r0, [pc, #32]   ; (8494 <printf@plt+0x74>)
8474:   4478        add r0, pc
8476:   f7ff efce   blx 8414 <puts@plt>
847a:   4807        ldr r0, [pc, #28]   ; (8498 <printf@plt+0x78>)
847c:   4478        add r0, pc
847e:   f7ff efca   blx 8414 <puts@plt>
8482:   4806        ldr r0, [pc, #24]   ; (849c <printf@plt+0x7c>)

実際のアドレスは0x8460、または0x8461、または0x8462でしょうか? 私を助けてください...

4

1 に答える 1