c - `nm` 输出符号未被 `LC_SYMTAB` 引用

标签 c macos dyld mach-o nm

为什么?

我正在尝试重现 nm 的行为.

问题

命令nm /usr/libexec/sharingd输出一些奇怪的符号,例如:

[..]
00000001001d7570 t -[_TtC8sharingd19SDAirDropHandlerIPA canHandleTransfer]
00000001001d8070 t -[_TtC8sharingd19SDAirDropHandlerIPA initWithTransfer:bundleIdentifier:]
00000001001d7ed0 t -[_TtC8sharingd19SDAirDropHandlerIPA singleItemActionTitle]
00000001001d75f0 t -[_TtC8sharingd19SDAirDropHandlerIPA suitableContentsDescription]
00000001001d7580 t -[_TtC8sharingd19SDAirDropHandlerIPA transferTypes]
00000001001e45a0 t -[_TtC8sharingd27SDAirDropContactHashManager .cxx_destruct]
00000001001daf60 t -[_TtC8sharingd27SDAirDropContactHashManager acquireTokenForIdentifier:]
00000001001dcf70 t -[_TtC8sharingd27SDAirDropContactHashManager init]
00000001001be700 t <redacted function 7904>
00000001001beb50 t <redacted function 7905>
00000001001bebf0 t <redacted function 7906>
00000001001bf050 t <redacted function 7907>
00000001001bf100 t <redacted function 7908>
00000001001bf3a0 t <redacted function 7909>
00000001001bf490 t <redacted function 7910>
00000001001bf4d0 t <redacted function 7911>
00000001001bf540 t <redacted function 7912>
00000001001bf580 t <redacted function 7913>
00000001001bf590 t <redacted function 7914>
00000001001bf5b0 t <redacted function 7915>
00000001001bf5d0 t <redacted function 7916>
00000001001bf5e0 t <redacted function 7917>
00000001001bf5f0 t <redacted function 7918>
[..]

我的版本nm不输出这些符号。

它们从哪里来?

确定

1.

这些符号名称的一部分(即 _TtC8sharingd19SDAirDropHandlerIPAcanHandleTransfer )存在于 mach-o 文件的字符串表中。

但是,它们以不同的字符串形式出现。

2.

符号<redacted function xxx>不存在于 mach-o 文件中的任何位置。

3.

LC_SYMTABS 未引用这些符号加载命令。

最佳答案

我还重现了nm命令,但我也没有处理这种情况。感谢您的分享。

查看LLVM source code ,看来:

如果nm遇到LC_FUNCTION_STARTS(函数开始段),那么它将获得不同的(函数?)地址。

它检查这些地址中的每一个是否已经在符号表中。

如果不是,那么就是我们不处理的情况:

参见第 1672 行的注释:“查看此地址尚未在表符号中,为它伪造一个 nlist。”

        // See this address is not already in the symbol table fake up an
        // nlist for it.
        if (!found) {
          NMSymbol F = {};
          F.Name = "<redacted function X>";
          F.Address = FoundFns[f] + BaseSegmentAddress;
          F.Size = 0;
          // There is no symbol in the nlist symbol table for this so we set
          // Sym effectivly to null and the rest of code in here must test for
          // it and not do things like Sym.getFlags() for it.
          F.Sym = BasicSymbolRef();
          F.SymFlags = 0;
          F.NType = MachO::N_SECT;
          F.NSect = 0;
          StringRef SegmentName = StringRef();
          StringRef SectionName = StringRef();
          for (const SectionRef &Section : MachO->sections()) {
            Section.getName(SectionName);
            SegmentName = MachO->getSectionFinalSegmentName(
                                                Section.getRawDataRefImpl());
            F.NSect++;
            if (F.Address >= Section.getAddress() &&
                F.Address < Section.getAddress() + Section.getSize()) {
              F.Section = Section;
              break;
            }
          }
          if (SegmentName == "__TEXT" && SectionName == "__text")
            F.TypeChar = 't';
          else if (SegmentName == "__DATA" && SectionName == "__data")
            F.TypeChar = 'd';
          else if (SegmentName == "__DATA" && SectionName == "__bss")
            F.TypeChar = 'b';
          else
            F.TypeChar = 's';
          F.NDesc = 0;
          F.IndirectName = StringRef();
          SymbolList.push_back(F);
          if (FoundFns[f] == lc_main_offset)
            FOS << "<redacted LC_MAIN>";
          else
            FOS << "<redacted function " << f << ">";
          FOS << '\0';
          FunctionStartsAdded++;
        }

这就是这些符号的外观。

关于c - `nm` 输出符号未被 `LC_SYMTAB` 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58307098/

相关文章:

c - 以下 C 程序的输出是什么?

objective-c - 在自定义 View 中实现目标 - 操作模式

xcode - dyld:库未加载 6.0 模拟器/6.0 设备的不同行为

c - C中指针的数据分配

c - Windows 下 feof() 的奇怪行为

c - slerp 的输出四元数到底是什么?

macos - AppleScript:循环应用程序窗口;将每个放在前面并触发其他应用程序中的操作?

cocoa - 将 CoreData 与对象一起使用?

excel - 在 Mac OS X Sierra (CoolProp) 上设置 DYLD_LIBRARY_PATH

macos - OSX 等效于 SymGetLineFromAddr 或 SymGetLineFromAddr64