1

In Learning Linux Binary Analysis by Ryan "elfmaster" O'Neill. On Page 33, the author compiles a program with a symbol reference and no definition,

Let's take a look at the source code:

_start()
  {
    foo();
  }

We see that it calls the foo() function. However, the foo() function is not located directly within that source code file; so, upon compiling, there will be a relocation entry created that is necessary for later satisfying the symbolic reference:

$ objdump -d obj1.o
obj1.o:
file format elf32-i386
Disassembly of section .text:
00000000 <func>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 08 sub $0x8,%esp
6: e8 fc ff ff ff call 7 <func+0x7>
b: c9 leave
c: c3 ret

**Even after changing _start() {} to void start() {}, what flags do I use to compile the same thing? When I try, I get..

gcc -nostdlib app.c -o test
app.c: In function ‘_start’:
app.c:3:3: warning: implicit declaration of function ‘foo’ [-Wimplicit-function-declaration]
   foo();
   ^~~
/tmp/ccMBITVZ.o: In function `_start':
app.c:(.text+0xa): undefined reference to `foo'
collect2: error: ld returned 1 exit status
4

Compiling your source code file like this also involves a call to the linker - remember that the "gcc" program is only the compiler front end program, which calls the preprocessor, compiler passes, assembler, and linker as required.

You can obtain the object file "app.o" using the "-c" compiler option like this:

gcc -nostdlib -c app.c -o app.o

Using "-c", the compiler is instructed to stop after generating the object file, so that the linker is not invoked. To generate an executable ELF file, you then have to invoke the linker separately (app.o is the default output file name, so the "-o app.o" parameter can be omitted).

  • You're welcome - and yes, I think it's a shame so many people don't really get to use a compiler from the command line at all nowadays, since so much development takes place in complex IDEs like Eclipse. But this might just be an old Unix greybeard ranting :). – user22967 Jan 31 '18 at 1:10
  • Still not working, I get the same error. Did it work for you? – Evan Carroll Jan 31 '18 at 1:10
  • With -c I do not get /tmp/ccr2dk4u.o: In function _start': app.c:(.text+0xa): undefined reference to foo' collect2: error: ld returned 1 exit status. However, I still get app.c: In function ‘_start’: app.c:3:3: warning: implicit declaration of function ‘foo’ [-Wimplicit-function-declaration], and with -c I still don't have a .o – Evan Carroll Jan 31 '18 at 1:11
  • 1
    Do you get the actual error message (undefined reference) or just the compiler warning? The warning simply results from neglecting to provide a prototype for foo(); - adding "void foo(void);" at the start of the file should get rid of the warning. – user22967 Jan 31 '18 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.