7.9

function main print 0x55 on my machine.

modify char main to unsigned int main here

/*
 * bar6.c
 */
#include <stdio.h>

unsigned int main;

void p2() {
  printf("0x%x\n", main);
}
/*
 * foo6.c
 */
void p2(void);

void offset(void) {
  return;
}

int main(int argc, char* argv[]) {
  p2();
  return 0;
}


(cd ./site/content/chapter7/code/7.9; make && ./main)

output:

gcc foo6.c bar6.c -o main
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.4/../../../../x86_64-pc-linux-gnu/bin/ld: Warning: alignment 1 of symbol `main' in /tmp/ccTBhRjm.o is smaller than 4 in /tmp/ccc3SjbF.o

0xe5894855

using objdump inspect

objdump -d main

find function main

000000000040055d <main>:
  40055d:	55                   	push   %rbp
  40055e:	48 89 e5             	mov    %rsp,%rbp
  400561:	48 83 ec 10          	sub    $0x10,%rsp
  400565:	89 7d fc             	mov    %edi,-0x4(%rbp)
  400568:	48 89 75 f0          	mov    %rsi,-0x10(%rbp)
  40056c:	e8 07 00 00 00       	callq  400578 <p2>
  400571:	b8 00 00 00 00       	mov    $0x0,%eax
  400576:	c9                   	leaveq 
  400577:	c3                   	retq   

0xe5894855 is first 2 instructions content of function main.

works same like

/*
 * another-bar6.c
 */
#include <stdio.h>

int main(int argc, char* argv[]);

void p2() {
  printf("0x%x\n", * (unsigned int *)main);
}
comments powered by Disqus