How can I install a DYLD loader command that is not explicitly supported by ld?
Asked Answered
C

2

11

On Mac OS X, binary executables in the DYLD format contain "loader commands" that instruct the library loading system how to handle the contents of the file. In particular, the loader command instruct the system where dependent libraries should be searched for, etc.

You can see the complete list of loader commands for any binary on your system by running "otool -l /path/to/your/app".

Generally speaking these loader commands are set by the "ld" tool during the link phase of a project's compilation.

My question is, what do I need to do to add loader commands for publicized types that are not supported (apparently) by ld?

In particular, I would like to take advantage of the LC_DYLD_ENVIRONMENT loader commmand, which can be used to specify a string in the loader commands table of a binary that should be loaded and evaluated as environment variable settings in the context of the executable.

I don't see any argument to ld that would facilitate this. Something like "-sectcreate" but for specifically adding to the content of the loader commands, is what I'm after.

I know this is possible because at least one standard app on Mac OS X uses it: Safari. But I don't know whether they achieve this by some kind of post-link massage of the binary, if they're using a custom version of ld that knows how to build and chain the custom loader command in, or if they are exploiting a general-purpopse feature of the ld command that I haven't been able to figure out.

Calfee answered 18/11, 2011 at 20:27 Comment(0)
S
15

It looks like you can use -dyld_env, like so: "-dyld_env DYLD_FRAMEWORK_PATH=/". This isn't documented in the man page, but can be found in ld64's Options.cpp and mentioned in the Changelog file. If you're trying to do it from Xcode, you'll probably have to do it like this: "-Xlinker -dyld_env -Xlinker DYLD_FRAMEWORK_PATH=/".

One thing to note: if you look at dyld's dyld.cpp, you'll see that it only allows environment variables that start with DYLD_ and ends with _PATH.

Standee answered 18/11, 2011 at 20:56 Comment(1)
Great! Thanks. I also got some feedback via Twitter that took care to point out that the LC_DYLD_ENVIRONMENT does not actually let you define arbitrary environment variables, only ones that affect DYLD loading. So to that extent the LSEnvironment recommendation from Ned below is more suited for my immediate needs, but your answer is most appropriate reference for the gist of the question.Calfee
A
8

If your executable is structured as part of a standard OS X application bundle (i.e. a .app that can be launched by a user), the conventional way to specify application specific environment variables is through its plist file using the LSEnvironment key. See here for more information.

Archiearchiepiscopacy answered 18/11, 2011 at 20:42 Comment(1)
Thanks, Ned! This is not a canonical answer for the question as posed, but indeed it gets to the heart of my current interest in the DYLD loader command feature. I'll use LSEnvironment for the purposes described!Calfee

© 2022 - 2024 — McMap. All rights reserved.