Home > Software engineering >  What is a symbol in programmation as referred in ar command
What is a symbol in programmation as referred in ar command

Time:11-07

I see this term a lot, 'symbol', and after searching on google I still can't find a definition that makes sense to me.

For example in the manual of ar command on Linux, it's said :

ar creates an index to the symbols defined in relocatable object modules in the archive when you specify the modifier s.

Are function declarations / variable declarations / defines / structure declarations etc, symbols ? Or is a symbol a term for .o files ?

In this context, what is a symbol exactly ? Act like I'm a complete beginner who knows nothing when you form your answer please !

CodePudding user response:

A symbol is the name of things (functions, variables etc). Here is an example file:

#include <stdlib.h>

struct foo {
    char *a;
};
struct foo global_foo;
static struct foo static_foo;

struct foo *foo_create() {
     return malloc(sizeof(struct foo));
}

which I then compile and make an archive and then use nm to list its symbols:

$ gcc -c 1.c && ar -rc 1.a 1.o && nm -s 1.a

Archive index:
global_foo in 1.o
foo_create in 1.o

1.o:
0000000000000000 T foo_create
0000000000000000 B global_foo
                 U _GLOBAL_OFFSET_TABLE_
                 U malloc
0000000000000008 b static_foo

CodePudding user response:

It depends on how you want to define symbol. If you define symbol as "something that stands for or suggests something else by reason of relationship, association, convention, or accidental resemblance" (definition from Merriam-Webster dictionary), then yes, you can say that variable names, function names, struct names, etc. are symbols because they symbolize variables, functions, struct, etc.

If you define symbol as a data type, then that is something a little different. A symbol in this case would be something you create that you can guarantee it to be unique. This is very useful for cases where you want to store a bunch of names, but you want to make sure that no names conflict with each other by making them unique.

The particular example that you gave us about the ar command is talking about symbols as a data type. The ar command stores references to archived files that are internally mapped in a data structure called a symbol table, which essentially means that ar guarantees that each and every names of file that are archived are unique (the way they do this is by mapping the names of each file to an index, so even if they were files with the same name, it would be unique to a specific number that it is mapped to).

  • Related