By looking at this answer: https://stackoverflow.com/a/4671482/1770034 I can use dlsym to get a global variable in C. Is it possible to get a member from a struct.
I'm guessing that if I had the following code in a shared object:
header.h
struct myStruct
{
int a;
int b;
};
imp.c
struct myStruct structure = { 123, 456 };
I could include the same header.h file and cast the whole pointer to a struct myStruct*. struct myStruct * capturedStructure = (struct myStruct*) dlsym(handle, "structure");
However, is there a way to get the address to member directly. I'm guessing I'm unable to do something like: int* c = (int*) dlsym(handle, "structure.b");
Since dlsym allows a person to grab a function or global by itself (without a header), I'm hoping that I can also grab a member without requiring a header.
int* b = (int*) (dlsym(handle, "structure") + 4);I suppose the answer is "No" because the compiler may reorder my members to reduce padding between members.int a; int b;Then version 2 of the library adds another memberint a; int c; int b;. The offset tobwill change. So even if you have the header for the structure, you need to use the correct version of the header for each version of the library.__attribute__((packed))[in gcc] to prevent reordering, but that attribute really only prevents padding from being inserted and you assume responsibility of aligning the data correctly.