0

I have a function on Windows to get the address of the module in buf:

GetModuleFileName(0, buf, buf_size);

I want to do the same on Linux (which I do not know much about). I found the function dladdr(X, &dlInfo) which seems to do the right thing. As I understand I get the name and other details (dli_sname, dli_saddr, dli_fname, ..) of X in dlInfo with this function.

But what is X? I know it it an address. But which one? How would I use this to obtain the same result as on Windows?

genpfault
  • 51,148
  • 11
  • 85
  • 139
user3443063
  • 1,455
  • 4
  • 23
  • 37
  • this does not get the address of "the module", it gets the filename of the program that is running. Perhaps you are looking for the virtual file `/proc/self/exe` – user253751 Mar 02 '23 at 15:33
  • Why do you want to do this? We're working in a bit of a vacuum here. – Paul Sanders Mar 02 '23 at 16:13
  • *"address of the module"* - Which module, specifically? A process generally has more than one module loaded. Also, `GetModuleFileName` doesn't return the (load) address of a module. What problem are you really trying to solve? – IInspectable Mar 03 '23 at 08:16

1 Answers1

1

X is any interesting address, usually the address of an interesting module function. If you want to get the current module name, X can be an address of the caller

void X() {
  // ... 
  dladdr(X, &dlInfo);
  // or dladdr(&X, &dlInfo);
}
273K
  • 29,503
  • 10
  • 41
  • 64