-3

I have a C source file that I'm not allowed to change and it is defined as follows:

int main(int argc, char *argv[])
{
    //doing something
    return 0
}

void __magic()
{
    __asm__("jmp %esp");
}

I do not use the fucntion __magic in my code, it is just declared after the main. I wish to find the address of the function __magic. How can I do that without having to declare the funtion before the main? I use gdb for debugging purposes.

P.S I'd also like to know whether this function is even saved in my process memory since there is no declaration/use of it. might the compiler just not add that function?

Lazybeem
  • 105
  • 6
  • 1
    What have you tried? For example, what happens if you do what you would if it *had* been declared before `main`? And if you can't change the code, then your code to find the address must be in a different file, in which case it shouldn't matter. – Scott Hunter Dec 12 '14 at 12:10
  • If I use it normally without declaring before main it wont complie.. I need to know the exact adress if this function in memory because I use stack overflow to inject a shellcode. – Lazybeem Dec 12 '14 at 12:13
  • 1
    @Lazybeem - How can you know the address of something which is not even running in memory? I mean the code does not even compile. Somethings missing in your question. – Sadique Dec 12 '14 at 12:20
  • `I need to know the exact adress if this function in memory because I use stack overflow to inject a shellcode` - That is way too herculian of a task for someone who is not able to compile his/her code. I mean then why the unnecessary constraint when you know what to do? – Sadique Dec 12 '14 at 12:22
  • @Lazybeem I am not sure, it will fulfill your requirement. In another function you include that c source file then use extern keyword to get that function and print the address of that function. – Karthikeyan.R.S Dec 12 '14 at 12:22
  • @al-Acme The code as shown in the question compiles. ScottHunter asked what happens if I use it if it had been declared, so I said it wouldn't compile. – Lazybeem Dec 12 '14 at 12:22
  • Yeah, it won't compile if you use it - that is the whole point and then your program cannot run. http://ideone.com/0uISXg – Sadique Dec 12 '14 at 12:23
  • That's why I am not using it. it compiles, and stop being rude. please read the question properly. I wish to find the address of that function in memory even though I don't use it in my code. – Lazybeem Dec 12 '14 at 12:26
  • @Lazybeem - Ok i apologize for my silliness but still i think there is something missing in the question – Sadique Dec 12 '14 at 12:27
  • @Lazybeem where do you want to find the address of `__magic` ? In another C source file ? – Jabberwocky Dec 12 '14 at 16:07
  • @Michael Walz no, the same file – Lazybeem Dec 12 '14 at 16:08
  • @Lazybeem you can't use the `__magic` symbol before it's declared, and if you want to get the address of `__magic`... well... you need to use this symbol. Normally the compiler should include the __magic function even if you don't use it, but the linker may strip it. – Jabberwocky Dec 12 '14 at 16:15

2 Answers2

1

You'll need to clarify what exactly you mean by "find the address of the function __magic". If you just want to see what the address is (i.e. not use it in code), then you can just use objdump to show the symbol value.

If, however, you need the address at compile time, then there's no easy way to do it. There's no guarantee that the compiler will place the code for the functions in any particular order, and even if it did, there may be an unknown amount of padding between functions. Since you're using %esp, I assume you're targeting 32-bit x86 - if you're targeting 64-bit however, then you'll also have to worry about ASLR.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
-1

simply define a prototype for your functions before main , its is a good way that professionals use to summuarize their efforts

 void ___magic(); // it is a prototype definition 

 int main(int argc, char *argv[])
{
    //doing something
    return 0
}

void __magic()
{
  __asm__("jmp %esp");
}
  • I am not allowed to add anything to the code. I know of function declarations, I just cant add one in my case – Lazybeem Dec 12 '14 at 12:14
  • 2
    @Omar Khaled - OP has already specified `without having to declare the funtion before the main` – Sadique Dec 12 '14 at 12:17