Symbol resolution by GNU ld

Symbol Resolution

When a symbol appeared multiple times in object files being combined asymbole resolution process is called by the link editor to determine whichsymbol is taken. The resolution of two symbols with the same namedepends on the symbol's attributes (its binding, defineness and size). Seepage 67 of [Gabi4].

Global
Global symbols are those whose binding is STB_GLOBAL. They are visible to all object files being combined.
Local
Local symbols are those whose binding is STB_LOCAL. They are not visible outside the object file containing their definition, so they do not participate in the symbol resolution.
Weak
Weak symbols are binding is STB_WEAK. They resemble global symbols, but their definitions have lower precedence. It can be further categorized into two kinds: undefined weak symbols and defined weak symbols.
Common
Common symbols are those whose section index is STN_COMMON. They are defined but have not been allocated. Its final size are not determined until the symbol resolution process is complete.
Undefined
Undefined symbols are those whose section index is STN_UNDEF.

The rules for symbol resolution is as follows.

  1. If a global symbol exists it can only appeared once in object files being combined. Multipe definitions of global symbols with the same name will cause an error. On the other hand, if a definition of a global symbol eixists, the appearence of weak symbols and/or common symbols with the same name will not cause an error. The link editor honors the global definition and ignores the weak and/or common ones.
  2. Otherwise, if a common symbol exists, the appearence of weak symbols with the same name will not cause an error. The link editor honors the common definition and ignores the weak ones. If muliple common symbols with the same name exists, the link editor honors the common definition with the biggest size.
  3. Otherwise, multiple appearences of weak symbols with the same name do not cause an error.
    1. If some of the weak symbols are defined (the section index is a positive integer), the link editor will honor the first found defined symbol and inogre the others.
    2. Otherwise, if all the weak symbols are undefined, the symbol will be left as an undefined weak symbol in the output file no matter what type of output file is being generated. In addition, if a executable is being generated, all the reference to the symbol will be assigned a value of zero. In the case of dynamic shared object, during process execution, the dynamic linker searches for this symbol. If the dynamic linker does not find a match, it binds a reference to a address of zero instead of generating a fatal runtime relocation error.

Testing Existence of Functionality

Undefined weak refenrenced symbols may provide a useful mechanism fortesting the existence of functionality. For example, the following C codefragment might have been used in the shared object libfoo.so.1:

#pragma weak foo

extern void foo(char *);

void bar(char *path)
{
    void (*fptr)(char *);
    if ((fptr = foo) != 0)
        (*fptr)(path);
}

When application is built against libfoo.so.1, the link editor willcomplete successfully regardless of whether a definition for the symbolfoo is found. If during execution of the application the functionaddress tests nonzero, the function is called. However, if the symboldefinition is not found, the function address tests zero and so it is notcalled. See page 45 of [Sun04].

Refenrences


Updated: 2011-03-04 16:14

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值