Skip to content

Commit

Permalink
notes: objdump (osx)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhoy committed Jun 10, 2018
1 parent 47d4f95 commit 3c5329c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions notes/2018_011_objdump/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test
*.o
14 changes: 14 additions & 0 deletions notes/2018_011_objdump/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.PHONY: clean

LIBS = -lmikey
CFLAGS = -Wall -Wextra -pedantic -std=c99
OBJECTS = addone.o

all: test

test: test.c $(OBJECTS)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

clean:
rm -f test
rm -f *.o
6 changes: 6 additions & 0 deletions notes/2018_011_objdump/addone.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>
#include <mikey/common.h>

int addone(int x) {
return x + 1;
}
6 changes: 6 additions & 0 deletions notes/2018_011_objdump/addone.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef _ADDONE_H
#define _ADDONE_H

int addone(int x);

#endif
69 changes: 69 additions & 0 deletions notes/2018_011_objdump/notes.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

* notes

I am running this on OSX; this is the llvm =objdump= which is
quite different from the linux utility (I believe).

* print the symbol table

For instance, on the =test= executable:

#+BEGIN_SRC sh :results verbatim
objdump -t test
#+END_SRC

#+RESULTS:
#+begin_example

test: file format Mach-O 64-bit x86-64

SYMBOL TABLE:
0000000100001018 l __DATA,__data _TESTING
0000000100000000 g F __TEXT,__text __mh_execute_header
0000000100000f40 g F __TEXT,__text _addone
0000000100000ee0 g F __TEXT,__text _main
0000000000000000 *UND* _printf
0000000000000000 *UND* dyld_stub_binder
#+end_example

here there is a reference to the static text in the data segment,
=TESTING=, the functions in the text segment, =addone= and
=main=. Note that =addone= comes from the =addone.o= object file:

#+begin_src sh :results verbatim
objdump -t addone.o
#+end_src

#+RESULTS:
:
: addone.o: file format Mach-O 64-bit x86-64
:
: SYMBOL TABLE:
: 0000000000000000 g F __TEXT,__text _addone

There are also references to the dynamically linked =printf=. I think
(but not sure) =*UND*= means undefined, as the dynamic linker will
determine at runtime the location of this function.

* disassembling

we can disassemble a particular symbol, for instance:

#+begin_src sh :results verbatim
objdump -D -macho -dis-symname=_addone addone.o
#+end_src

#+RESULTS:
#+begin_example
addone.o:
(__TEXT,__text) section
_addone:
0: 55 pushq %rbp
1: 48 89 e5 movq %rsp, %rbp
4: 89 7d fc movl %edi, -4(%rbp)
7: 8b 7d fc movl -4(%rbp), %edi
a: 83 c7 01 addl $1, %edi
d: 89 f8 movl %edi, %eax
f: 5d popq %rbp
10: c3 retq
#+end_example
13 changes: 13 additions & 0 deletions notes/2018_011_objdump/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include <mikey/common.h>
#include "addone.h"

static char* TESTING = "This is a static string.\n";

int main()
{
printf("Foo.\n");
printf("%s", TESTING);
printf("One plus two is: %d", addone(2));
return EXIT_SUCCESS;
}

0 comments on commit 3c5329c

Please sign in to comment.