This is the mail archive of the gdb@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Symbol lookup for global symbols in shared libraries


Hi All,

I am facing a problem while trying to debug shared library using GDB.
My application code and shared library both have a global variable
with same name. My understanding was that as per work done under
http://sourceware.org/ml/gdb-patches/2007-05/msg00155.html, GDB's
global symbol lookup first searches for symbol in current objfile. It
works fine for same name functions but in case of variables, it always
uses application's global name variable.

After debugging GDB, I have noticed that symbol lookup code tries to
lookup in current Object file's global block but it finds symbol
without any symbol information (sym->ginfo.section is -1 and
aclass_index 12). It actually iterates through global block of object
file and only finds symbol of matching domain. It returns this found
symbol (with no symbol info) but while evaluating expression,
default_read_var_value function checks for symbol class of variable
and on finding LOC_RESOLVED, it again looks up for symbol data. But
this symbol lookup code uses
default_iterate_over_objfiles_in_search_order method to iterate over
object files. This method searches the objfiles in the order they are
stored internally and ignores CURRENT_OBJFILE. Because of this, it
finds symbol in application's objfile and always returns its value.

I have taken dump of debugging symbol data in attached syms.txt. I've
noticed that block #000 of my shared library (libSharedlib.so)
contains two entries for global variable (globalTest). Why is it so?
If I place a condition in GDB's lookup_block_symbol function to ignore
symbols with no section info while iterating through block's symbol
then everything works fine. Below are code snippets of application and
SO I am using:

Application:

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

int globalTest = 0;

int foo(int a, int b)
{
   int s = a + b;
   printf("Main foo: %d and globalTest is %d\n", s, globalTest);
   return s;
}

int main(int argc, char **argv) {
  void *handle;
  int (*sum)(int,int);
  char *error;
  globalTest +=5;
  handle = dlopen
("/home/taimoor/Shared/sharedLib/Debug/libsharedLib.so", RTLD_LAZY);
  if (!handle) {
      fputs (dlerror(), stderr);
      exit(1);
  }

  sum = dlsym(handle, "foo");
  if ((error = dlerror()) != NULL)  {
      fputs(error, stderr);
      exit(1);
  }

  foo(1,2);
  printf ("%d and SharedLibraryTest globalTest is %d", sum(4,5), globalTest);
  dlclose(handle);
  return 0;
}



Shared Library:

#include <stdio.h>
int globalTest = 100;

int foo(int a, int b)
{
   int s = a + b;
   printf("%d and globalTest is %d\n", s, globalTest);
   return s;
}


Can anyone tell me if its some bug in GDB or I am doing something
wrong? I've used -Bsymbolic linker flag for my app and so.

Thanks,
Taimoor

Attachment: syms.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]