gpt4 book ai didi

c++ - `-fno-semantic-interposition` 与 `-fvisibility=protected` 究竟有何不同?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:26 27 4
gpt4 key购买 nike

来自 https://gcc.gnu.org/onlinedocs/gcc-8.1.0/gcc/Optimize-Options.html#index-fsemantic-interposition

-fsemantic-interposition

Some object formats, like ELF, allow interposing of symbols by the dynamic linker. This means that for symbols exported from the DSO, the compiler cannot perform interprocedural propagation, inlining and other optimizations in anticipation that the function or variable in question may change. While this feature is useful, for example, to rewrite memory allocation functions by a debugging implementation, it is expensive in the terms of code quality. With -fno-semantic-interposition the compiler assumes that if interposition happens for functions the overwriting function will have precisely the same semantics (and side effects). Similarly if interposition happens for variables, the constructor of the variable will be the same. The flag has no effect for functions explicitly declared inline (where it is never allowed for interposition to change semantics) and for symbols explicitly declared weak.

来自 https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-visibility-function-attribute

protected

Protected visibility is like default visibility except that it indicates that references within the defining module bind to the definition in that module. That is, the declared entity cannot be overridden by another module.

这听起来完全一样。

除了 -fno-semantic-interposition 将标记为 default 的任何函数明确更改为 之外,是否有任何真正的区别 protected ?

最佳答案

这个问题很难回答,因为目前 -fsemantic-interposition 并没有真正起作用。在这个例子中,

int a;

int
f1 (int a)
{
return a;
}

int
f2 (void)
{
return f1 (a) - a;
}

f2 的主体被有效地优化为 return 0; 即使使用 -O2 -fsemantic-interposition(以及 -O2 -fno-semantic-interposition,以防文档中选项的意义相反)。 I filed a bug.

可能的意图是 -fsemantic-interposition 会禁用此类优化,因此您最终会调用 f1 结果的显式计算。

ELF 符号可见性与此无关,主要是因为它仅适用于动态链接。链接编辑器仍然可以在静态链接时插入符号(例如,使用 -z muldefs 选项),因此编译器应该真正提供一种方法来启用插入而不改变符号属性(所以除了 __attribute__ ((weak)) 之外的其他东西)。

关于c++ - `-fno-semantic-interposition` 与 `-fvisibility=protected` 究竟有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51227608/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com