3

I have a following program:

int b2[4];
int foo()
{
  static int b2[10];
  b2[5] = 4;
}
int main()
{
  static int b2[10];
  int b[5];
  b[0] = 1;
  b2[9] = 4;
  int *ptr = b;
  int c = *(ptr + 10);
  foo();
  return 0;
}

I want to distinguish between different b2 arrays defined in functions foo, main and globally. I can collect all the symbols using:

symbols = set(currentProgram.getSymbolTable().getAllSymbols(True))
    for s in symbols:
        print(s.getName())

But, there is no way to distinguish between static (in functional namespace) and global symbols.

Ghidra GUI shows me something like: b2.1913 <- main , b2.1917 <- foo, b2 <- global.

Thus I can easily distinguish between these symbols using GUI.

Thanks in advanced for your help.

1 Answer 1

3

The Symbol object has a couple different ways you could go about this. Here are a few options.

symbols = set(currentProgram.getSymbolTable().getAllSymbols(True))
for s in symbols:
    if s.getName() == "b2":
        print(s.getName(), s.getName(True), s.getParentSymbol().getName(), s.getParentNamespace())

Output:

GetSymbolType.py> Running...
(u'b2', u'b2', u'global', Global)
(u'b2', u'foo()::b2', u'foo()', foo())
(u'b2', u'main::b2', u'main', main)

Explanation:

  1. s.getName(true) - Includes the parent namespace in the output (you'll notice that for global symbols, this has no effect)
  2. s.getParentSymbol().getName(), name of the parent Symbol, which will be the namespace Symbol that contains s
  3. s.getParentNamespace() - return the parent Namespace object

More details in the API docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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