在 macOS 上打印可执行文件的 rpath [英] Print rpath of an executable on macOS

查看:40
本文介绍了在 macOS 上打印可执行文件的 rpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 install_name_tool 更改可执行文件的 rpath,但我现在不知道 rpath 是什么.install_name_tool 要求在命令行中同时提供旧的和新的 rpath.我可以使用什么命令来打印 macOS 下可执行文件的 rpath?

I want to change the rpath of an executable using install_name_tool, but I can't figure out what the rpath is right now. install_name_tool requires both the old and the new rpath's to be given on the commandline. What command can I use to print the rpath of an executable under macOS?

推荐答案

首先,要了解可执行文件不包含单个 rpath 条目,而是包含一个或多个条目的数组.

First of all, understand that an executable doesn't contain a single rpath entry, but an array of one or more entries.

其次,您可以使用 otool 列出图像的 rpath 条目.使用 otool -l,您将获得如下输出,在其末尾是 rpath 条目:

Second, you can use otool to list an image's rpath entries. Using otool -l, you'll get output like the following, at the very end of which are the rpath entries:

Load command 34
          cmd LC_LOAD_DYLIB
      cmdsize 88
         name /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (offset 24)
   time stamp 2 Wed Dec 31 19:00:02 1969
      current version 1038.32.0
compatibility version 45.0.0

Load command 35
          cmd LC_RPATH
      cmdsize 40
         path @loader_path/../Frameworks (offset 12)

查找 LC_RPATH 命令并记下 path 条目下的路径.

Look for the LC_RPATH commands and note the path under the path entry.

关于 @loader_path 是什么:它是引用想要加载框架的 Mach-O 对象的通用和动态方式.

regarding what @loader_path is: it's a generic and dynamic way to refer to the Mach-O object that wants to do the loading of the framework.

虽然这是一个相当人为的例子,但我认为它应该能说明问题.假设我们有一个应用 MyApp.app,它使用框架 MyFramework.framework.我们还会说要正常运行,我需要将我的应用程序安装在/Applications 中而不是其他任何地方.因此,所述应用程序和框架的结构如下:

While this is a rather contrived example, I think it should get the point across. Let's say we have an app MyApp.app that uses a framework MyFramework.framework. We'll also say that to function properly, I require that my app is installed in the /Applications and nowhere else. So the structure of said app and framework would be the following:

/Applications/MyApp.app/Contents/MacOS/MyApp(可执行文件)/Applications/MyApp.app/Contents/Frameworks/MyFramework.framework/MyFramework (Mach-O dylib)

/Applications/MyApp.app/Contents/MacOS/MyApp (executable) /Applications/MyApp.app/Contents/Frameworks/MyFramework.framework/MyFramework (Mach-O dylib)

如果我们在可执行文件上运行 otool -L(注意大写的 L),它将显示以下关于 MyFramework 的内容:

If we were to run otool -L (note the capital L) on the executable it would show the following regarding MyFramework:

@rpath/MyFramework.framework/Versions/A/MyFramework
/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
/usr/lib/libobjc.A.dylib
/usr/lib/libSystem.B.dylib
....

请注意,因为 MyFramework.framework 使用 @rpath 安装名称/路径,我们需要使用运行时搜索路径条目来代替 @rpath 在运行时.现在,我可以有一个 rpath 条目:

Note that because the MyFramework.framework uses an @rpath install name/path, we'll need to have runtime search path entries that will be substituted in place of @rpath at runtime. Now, I could have a single rpath entry of:

/Applications/MyApp.app/Contents/Frameworks

那行得通,并且在运行时这两部分会放在一起:

That would work, and at runtime the two parts would be put together:

/Applications/MyApp.app/Contents/Frameworks + /MyFramework.framework/Versions/A/MyFramework ==

/Applications/MyApp.app/Contents/Frameworks/MyFramework.framework/Versions/A/MyFramework

显然,像这样对路径进行硬编码并不理想,因为简单地将应用程序移动到不同的文件夹或重命名应用程序本身会导致链接失败.

Obviously, hard-coding a path like this is not ideal, as simply moving the app to a different folder or renaming the app itself would cause linking to fail.

@loader_path 只是一种动态方式,可以在文件系统上的任何位置引用应用程序的可执行文件.在这种特殊情况下,在运行时,它将填充到正在运行的可执行文件的路径:/Applications/MyApp.app/Contents/MacOS/MyApp.然后我们可以说要找到 MyFramework.framework,您只需上一个目录并转到 Frameworks.

@loader_path is simply a dynamic way to refer to the app's executable wherever it may exist on the filesystem. In this particular case, at runtime it will be filled in with the path to the running executable: /Applications/MyApp.app/Contents/MacOS/MyApp. Then we can say that to find the MyFramework.framework, you simply go up a directory and over to Frameworks.

这篇关于在 macOS 上打印可执行文件的 rpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆