[cfe-dev] Casting in GetExecutablePath? (not a bug)

<Alexander G. Riccio> via cfe-dev cfe-dev at lists.llvm.org
Sun Jan 24 19:57:49 PST 2016


GetExecutablePath does some funky stuff to get the name of a dynamically
loaded shared object:

if (!CanonicalPrefixes)
    return Argv0;

  // This just needs to be some symbol in the binary; C++ doesn't
  // allow taking the address of ::main however.
  void *P = (void*) (intptr_t) GetExecutablePath;
  return llvm::sys::fs::getMainExecutable(Argv0, P);

The getMainExecutable function, declared as:

std::string getMainExecutable(const char *argv0, void *MainAddr)

...and uses dladdr to get the name of the aforementioned shared object, if
MainAddr points to an address in a shared object:

#elif defined(HAVE_DLFCN_H)
  // Use dladdr to get executable path if available.
  Dl_info DLInfo;
  int err = dladdr(MainAddr, &DLInfo);
  if (err == 0)
    return "";

  // If the filename is a symlink, we need to resolve and return the location of
  // the actual executable.
  char link_path[MAXPATHLEN];
  if (realpath(DLInfo.dli_fname, link_path))
    return link_path;
#else

GetExecutablePath is casted to a void*, I think, because many other types
of objects are passed in (ClangTool::run, for example, passes an int), but
I find the explicit casting pretty confusing.

What's up with that? Is there some reason for the explicit casting?

I assume it's because getMainExecutable is not const-correct, but I can't
try fixing it because linking fails miserably when I constify MainAddr.

Sincerely,
Alexander Riccio
--
"Change the world or go home."
about.me/ariccio

<http://about.me/ariccio>
If left to my own devices, I will build more.
⁂
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160124/2a2a51e9/attachment.html>


More information about the cfe-dev mailing list