Extern declarations of variables in a c program result in size 0 in ELF. Why isn't actual size stored in ELF when known? For cases like incomplete arrays I understand there is no size information but for other cases it should be possible to store size.
I tried some simple codes and verified in ELF size emitted is zero.
// file1.c
extern int var;
int main()
{
var = 2;
}
// file 2.c
long long int var = 8;
gcc -c file1.c
readelf -s file1.o
...
9: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND var
...
gcc -c file2.c
readelf -s file2.o
...
7: 0000000000000000 8 OBJECT GLOBAL DEFAULT 2 var
...
If size of var was stored as 4 in file1.o, linker can actually detect potential mismatch due to size when linking with file2.o.
So why isn't size emitted as it can help catch some subtle issues like this?