简体   繁体   English

ELF的符号分辨率

[英]Symbol Resolution for ELF

I had a question regarding symbol resolution in C linking (in particular with the elf format). 我对C链接中的符号解析(尤其是elf格式)有疑问。

Suppose I have two modules split into separate files module1.c and module2.c: 假设我有两个模块,分为两个单独的文件module1.c和module2.c:

// module1.c
int main() {
    return 0;
}

==========================

// module2.c
int main = 3;

int p2() {
    return 0;
}

Compiling these two together will give me a linker error because there is a duplicate symbol for main in both modules. 将这两个编译在一起会给我一个链接器错误,因为两个模块中的main都有重复的符号。 My question is, why does the linker not account for the fact that one is a function and one is a variable? 我的问题是,链接器为什么不解释一个是函数而一个是变量的事实呢? This information definitely exists in the symbol tables for the two: 该信息肯定存在于两个符号表中:

Symbol table '.symtab' contains 9 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS a.c
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    2 
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
     6: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
     7: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
     8: 0000000000000000    11 FUNC    GLOBAL DEFAULT    1 main

No version information found in this file.

===================================================================

Symbol table '.symtab' contains 10 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS b.c
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    2 
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
     6: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
     7: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
     8: 0000000000000000     4 OBJECT  GLOBAL DEFAULT    2 main
     9: 0000000000000000    11 FUNC    GLOBAL DEFAULT    1 p2

You can clearly see one is an object and the other is a function. 您可以清楚地看到一个是对象,另一个是函数。 My question is whether there is any specific reason linker's don't aim to make this distinction between functions and variables? 我的问题是,是否有任何特定原因导致链接程序不旨在在函数和变量之间进行区分? Does this mean in a huge program made of many modules, you can never declare a global variable with the same name as a function? 这是否意味着在一个由许多模块组成的庞大程序中,您永远无法声明一个与函数同名的全局变量?

It's just a rule of the C language: functions and objects don't have distinct namespaces. 这只是C语言的一个规则:函数和对象没有不同的命名空间。

So yes - you can't have a function and variable both with external linkage and the same name. 所以是的-您不能同时具有外部链接和相同名称的函数和变量。

Note that you can give both file-scope variables and functions static linkage using the static keyword, and it's OK to have a function with static linkage in one file and a variable of the same name with static linkage in another file. 请注意,您可以使用static关键字为文件作用域变量和函数提供静态链接,并且可以在一个文件中具有带有静态链接的函数,而在另一个文件中具有具有静态链接的同名变量。 This reduces the likelihood of conflict in large programs. 这减少了大型程序中发生冲突的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM