Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to determine if a symbol is a variable or function in C?

I am implementing some limited remote debugging functionality for an application written in C running on a Linux box. The goal is to communicate with the application and lookup the value of an arbitrary variable or run an arbitrary function.

I am able to lookup symbols through dlsym() calls, but I am unable to determine if the address returned refers to a function or a variable. Is there a way to determine typing information via this symbol table?

like image 482
dykeag Avatar asked Nov 20 '13 21:11

dykeag


People also ask

How do you check if a function is defined in C?

First, provide a fallback dummy function in a separate namespace. Then determine the return type of the function-call, inside a template parameter. According to the return-type, determine if this is the fallback function or the wanted function.

Can a variable have symbol?

A symbolic variable is a string of characters that you define as a symbol. Because the variable is a symbol, you can assign different values to it at different times.

What is meant by a variable in C language?

Variable is basically nothing but the name of a memory location that we use for storing data. We can change the value of a variable in C or any other language, and we can also reuse it multiple times.


1 Answers

On on x86 platforms, you can check for the instructions used to set up the stack for a function if you can look into it's address space. It is typically:

push ebp
mov ebp, esp

I'm not positive about x64 platforms, however I think it is similar:

push rbp
mov rbp, rsp

This describes the C calling convention

Keep in mind however, compiler optimizations may optimize out these instructions. If you want this to work, you may have to add a flag to disable this optimization. I believe for GCC, -fno-omit-frame-pointer will do the trick.

like image 155
chbaker0 Avatar answered Sep 28 '22 00:09

chbaker0