> How does an executable open itself? That's where portability goes out the window.
It's a way easier nut to crack than parsing executable formats. Various OSs have a method for this:
Linux: /proc/self/exe
Windows: GetModuleFileNameW(NULL, buf, MAX_PATH)
MacOS: _NSGetExecutablePath(buf, &size)
Solaris: getexecname()
BSDs: ...?
These are just "ask and you shall receive" APIs; nothing to analyze.
You may want to solve that problem anyway for other reasons: for instance, if you want your programming language to have a relocatable installation which finds related files using its own path.
On a system where you cannot solve this problem at all, you can use trick 1. Have an array somewhere in the program where you write the installation path. Your installer has to do this, and if the program is moved after installation, some utility program has to fix that up. (I am not crazy about the idea because it changes the checksum of the file at the installation site. You can no longer use an off-the-shelf program like sha256sum to check the integrity of the executable against the provisioned materials upstream.)
It's a way easier nut to crack than parsing executable formats. Various OSs have a method for this:
Linux: /proc/self/exe
Windows: GetModuleFileNameW(NULL, buf, MAX_PATH)
MacOS: _NSGetExecutablePath(buf, &size)
Solaris: getexecname()
BSDs: ...?
These are just "ask and you shall receive" APIs; nothing to analyze.
You may want to solve that problem anyway for other reasons: for instance, if you want your programming language to have a relocatable installation which finds related files using its own path.
On a system where you cannot solve this problem at all, you can use trick 1. Have an array somewhere in the program where you write the installation path. Your installer has to do this, and if the program is moved after installation, some utility program has to fix that up. (I am not crazy about the idea because it changes the checksum of the file at the installation site. You can no longer use an off-the-shelf program like sha256sum to check the integrity of the executable against the provisioned materials upstream.)