【问题标题】:Forward reference (_telldir)前向引用 (_telldir)
【发布时间】:2020-02-02 12:36:10
【问题描述】:

在 MacOSX10.14 的 /usr/include/dirent.h 文件中,我看到了这一行:

struct _telldir;        /* forward reference */

在同一个文件中使用了标识符:

 struct _telldir *__dd_td;

包含文件中没有其他地方定义了_telldir,但在同一个文件中声明了函数telldir:

long telldir(DIR *) __DARWIN_ALIAS_I(telldir);

我看到 __DARWIN_ALIAS_I 宏被定义为

#define __DARWIN_ALIAS_I(sym)       __asm("_" __STRING(sym) __DARWIN_SUF_64_BIT_INO_T __DARWIN_SUF_UNIX03)**

我知道这是“内联汇编程序”,它定义了 _telldir,但我想了解更多关于这个定义的信息。

提前致谢

【问题讨论】:

    标签: macos include inline-assembly forward-reference


    【解决方案1】:

    它并不完全像 instructions 中的内联 asm,它只是覆盖了符号名称的名称修饰。 (例如,MacOS 上的 static int foo 通常具有 _foo 的符号名称。例如,在编译器 asm 输出中,您将拥有 _foo: .long 0)。

    在 GNU C 中,int foo asm("foobar");asm 符号名称foobar,而不是默认的 _foo。请参阅https://gcc.gnu.org/onlinedocs/gcc/Asm-Labels.html 和这个 SO 答案:Adding leading underscores to assembly symbols with GCC on Win32?

    如果您有兴趣,请在 https://godbolt.org/ 上亲自尝试。


    在这种情况下,我们有宏扩展的C 预处理器字符串文字连接。例如

    "_" "telldir" "_ino64"
    

    在所有情况下都完全等同于"_telldir_ino64",包括
    long telldir(DIR *dirp) asm("_" "telldir" "_ino64");

    这个用例似乎是通过基于其他几个 CPP 宏设置名称来选择 libc 中的 telldir 实现。因此,如果__DARWIN_SUF_64_BIT_INO_T 定义为"_ino64" 并且__DARWIN_SUF_UNIX03 定义为空或/**/,那么这会将符号名称设置为"_telldir_ino64"

    (不知道 MacOS 通常如何实际定义这些其他宏,但您显示的行的语法只是 asm("") 内的字符串文字连接以设置符号名称。)

    【讨论】:

      猜你喜欢
      • 2010-10-16
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      相关资源
      最近更新 更多