简体   繁体   中英

Symbol Resolution for ELF

I had a question regarding symbol resolution in C linking (in particular with the elf format).

Suppose I have two modules split into separate files module1.c and 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. 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.

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. This reduces the likelihood of conflict in large programs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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