多个弱符号的后果(C链接器) [英] consequences of multiple weak symbols(C Linker)

查看:269
本文介绍了多个弱符号的后果(C链接器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有一个关于多个弱符号的潜在问题的问题,这个问题来自我的教科书:

I just have a question about potential problem of multiple weak symbols, this question is from my textbook:

一个模块A:

int x;
int y;
p1() {...}

另一个模块B:

double x;
p2() {...}

并且我的教科书说 '在p2中写入x可能会覆盖y' 我可以有点教科书的想法(双倍x是int x大小的两倍,并且int y紧接在int x之后放置了in y,这是问题所在),但仍然遗漏了细节,我知道何时有多个弱点符号,链接器将随机选择一个,所以我的问题是,链接器选择的模块的哪个x将导致写入p2中的x会覆盖y.

and my textbook says that 'write to x in p2 might overwrite y' I can kind of get the idea of the textbook( double x is twice the size of int x, and int y is placed right after int x, here comes the problem), but still lost in details, I know when there are multiple weak symbols, the linker will just randomly pick one, so my question is, which x of module that the linker choose will result in writing to x in p2 will overwrite y.

这是我的理解:如果链接器选择模块A的 int x 将导致结果,因为那样,x,y都是4个字节,而p2(编译后的图像在那里是一个汇编代码movq与p1中的movl比较)将更改8个字节,因此覆盖y.

This is my understanding :if the linker choose the int x of module A will result in the consequence, because in that way x,y are both 4 bytes and the p2(image after compilation there is one assembly code movq compared by movl in p1 )will change 8 bytes therefore overwrite y.

但是我的讲师说,如果仅链接程序选择模块B的 double x ,这将导致覆盖y,这是怎么回事,我是正确的还是我的讲师是正确的?

But my instructor said if only the linker choose double x of module B, that will result in overwriting y, how come, am I correct or my instructor is correct?

推荐答案

根据ISO C,程序将调用未定义的行为.所使用的外部名称必须在程序中的某个位置完全具有一个定义. *

According to ISO C, the program invokes undefined behavior. An external name which is used must have exactly one definition somewhere in the program.*

弱符号"是某些动态库系统(例如GNU/Linux上的ELF)的概念.该术语在这里不适用.允许对外部符号进行多个定义的链接器据说正在实现松弛的ref/def"模型.该术语来自 ANSI C的6.1.2.2节原理.

"Weak symbols" are a concept in some dynamic library systems like ELF on GNU/Linux. That terminology does not apply here. A linker which allows multiple definitions of an external symbol is said to be implementing the "relaxed ref/def" model. This term comes from section 6.1.2.2 of the ANSI C rationale.

如果我们将宽松的ref/def模型视为文档化的语言扩展,则名称的多个定义将成为本地定义的行为.但是,如果输入不一致,该怎么办?情况类似于不良的类型别名的理由几乎可以肯定地定义了这一点.如果一个模块具有int x; int y;,而另一个模块具有double x,则通过double x别名进行的写操作将破坏y.这不是您可以依赖的东西.这是故意获得混叠效果的非常差的方法.您想在两个结构或类似结构之间使用union.

If we regard the relaxed ref/def model as a documented language extension, then the multiple definitions of a name become locally defined behavior. However, what if they are inconsistently typed? That is almost certainly undefined by the reasoning that the situation resembles bad type aliasing. It is possible that if one module has int x; int y; and the other has double x, that a write through the double x alias will clobber y. This isn't something you can portably rely on. It's a very poor way to obtain an aliasing effect on purpose; you want to use a union between two structures or some such.

现在有关弱符号":那些是共享库中的外部名称,可以用替代定义覆盖.例如,GNU/Linux系统上GNU C库中的大多数功能都是弱符号.例如,程序可以定义自己的read函数来替换POSIX.无论如何重新定义read,库本身都不会中断.当需要调用read时,它不使用弱符号read,而是使用一些内部别名,例如__libc_read. 这个机制很重要;它允许库符合ISOC.严格符合ISO C的程序可以使用read作为外部名称.

Now about "weak symbols": those are external names in shared libraries that can be overridden by alternative definitions. For instance, most of the functions in the GNU C library on a GNU/Linux system are weak symbols. A program can define its own read function to replace the POSIX one, for instance. The library itself will not break not matter how read is redefined; when it needs to call read, it doesn't use the weak symbol read but some internal alias like __libc_read. This mechanism is important; it allows the library to conform to ISO C. A strictly conforming ISO C program is allowed to use read as an external name.

*在ISO C99标准中,这是在 6.9外部定义中给出的:如果在表达式中使用了通过外部链接声明的标识符(不是作为操作数的一部分)一个sizeof运算符,其结果是一个整数常数),在整个程序中的某个位置,标识符应恰好有一个外部定义;否则,该标识符应不超过一个."

* In the ISO C99 standard, this was given in 6.9 External Definitions: "If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one."

这篇关于多个弱符号的后果(C链接器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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