What is the difference between "Local Linker Symbols" & "Local Program Variables"? - linker

I want to know the difference between "Local Linker Symbols" & "Local Program Variables"?
I'm reading a book which has this:
alt text http://img682.imageshack.us/img682/9816/symbols.jpg
What does each mean? Any examples?

/* This function has global scope within this file (module). It is represented
* by a "local linker symbol", since the linker will need to resolve its address
* if it is referenced by any other function.
*/
static void some_function()
{
/* These "local program variables" are of no interest to the linker
* since they are not visible outside the current function, so no other
* part of the program can possibly reference them.
*/
int a, b, c;
}

Local Linker symbols are defined and referenced exclusively by a module.
Basically static variable and function are linker symbols and those variables or functions are not visible to other module except itself and those variables and functions are included in symbol table as local symbol in relocatable object module.
Local variables are variables defined locally which means they are not global variables.

Related

Static global variable vs function static variable C language

my first question here.
can anyone explain to me the differnce between a global static variable (that is, defined above the functions and the "main" combined with "static") and a static variable that is defined inside a function?
as i understand it, they do the same - both enlarge the variable scope to the whole file, and both restrict it to the specific file. so... what am i missing?
plus another question (that came up when looking for a solution) - what is extern? i thought that defining a variable above the main would make it global (as in to other files too), but then i read someone explaining that in order to do so, i must add "extern" before the varible defining.
For variables defined outside of a function, the static keyword limits the variable to being referenced using its identifier from the file in which it is defined. Variables of this type will be assigned a fixed address by the linker (outside of the heap and stack), but will not be assigned a global symbol. If variables of this type are defined in multiple files with the same identifier, the linker should allocate separate memory in each case and should compile without warnings.
Variables defined with the static keyword inside of a function have similar memory allocation, but references to the variable using its identifier are limited to within that function. You could have multiple functions defining static variables with the same identifier and each will be allocated separately.
If an initializer is used for static variables, the variable is initialized once before the program begins executed and the initializer is evaluated at compile time. The initializer must evaluate to a constant expression in this case (no function calls or parameter or variable references).
The compiler will typically build each C file independent of all of the others in a program using symbols to define external dependencies, such as variables and functions located in other files. After each source file is compiled, the linker processes the compiler output to replace symbols with fixed addresses and build the entire program. In order to properly execute the build, the compiler must know the types and sizes of all external functions and variables. The type and number of parameters and return type of functions are typically shared in a header file using function prototype declarations. Function prototype declarations are implicitly identified as external by the compiler. The extern keyword is used to specify the type and size of variables declared in files outside of the file referencing the variable. Declaring a variable without the external keyword would cause it to be defined within the module where it is declared. If two variables with the same identifier are declared in multiple files without the static keyword, then the compiler will typically generate an error since the same global symbol is used multiple times in the program.

How are the symbols for static variables named in GCC?

I'm experimenting with static globals in C. I tried this code and ran nm on it:
#include <stdio.h>
static int global_static = 12345;
int main(void)
{
static int local_static = 12345;
printf("%d\n", global_static);
printf("%d\n", local_static);
return 0;
}
Here's a snippet of the nm output:
00004020 d global_static
00004024 d local_static.1905
000011a9 T main
I have two questions about this:
Where does the name for the local static variable come from? Is it a process ID or a random number?
Does the fact that global_static has no invalid characters in it imply that I could do extern static int global_static; in another file and read global_static?
When I say invalid characters, I mean characters that can't be part of variable names in C, i.e. ., $, %, #, etc.
Where does the name for the local static variable come from? Is it a process ID or a random number?
In gcc's langhooks.c, the default set_decl_assembler_name hook implementation (which is used directly for the C language) contains:
/* By default, assume the name to use in assembly code is the same
as that used in the source language. (That's correct for C, and
GCC used to set DECL_ASSEMBLER_NAME to the same value as
DECL_NAME in build_decl, so this choice provides backwards
compatibility with existing front-ends. This assumption is wrapped
in a target hook, to allow for target-specific modification of the
identifier.
Can't use just the variable's own name for a variable whose scope
is less than the whole compilation. Concatenate a distinguishing
number - we use the DECL_UID. */
if (TREE_PUBLIC (decl) || DECL_FILE_SCOPE_P (decl))
id = targetm.mangle_decl_assembler_name (decl, DECL_NAME (decl));
else
{
const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
char *label;
ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
id = get_identifier (label);
}
And the comment on the macro DECL_UID says:
/* Every ..._DECL node gets a unique number. */
So the number is some identifier invented by gcc which is guaranteed to be different for every declaration seen in the translation unit (including declarations in #include-d files). This is enough to make sure if different scopes use local static variables with the same name, they will have different mangled symbol names in the assembly and object code.
Does the fact that global_static has no invalid characters in it imply that I could do extern static int global_static; in another file and read global_static?
No. For one thing, it's illegal to combine extern and static, since these give conflicting linkages to the variable. Note that static has two entirely different meanings in C: Inside a function, it means the variable has static storage duration. Outside a function, it means the variable or function has internal linkage. (A variable which is not local to a function always has static storage duration.)
So from the C language point of view, the static on global_static means that the variable has internal linkage, which means it is never to be considered the same variable as anything in any other translation unit, so there is no way to directly access it from another *.c file. When translating to ELF objects or other common object formats, this is done by making the symbol for the variable a "local" symbol instead of a "global" symbol. When linking executables or loading dynamic libraries, a global symbol can satisfy an undefined symbol from another object, but a local symbol never does.
Note the nm tool prints uppercase symbol type letters for global symbols and lowercase symbol type letters for local symbols, so the d next to the variables in your output means that both are local symbols and cannot possibly be directly used by other objects.
It's a unique identifier within the translation unit (source/object file), so that same-named static with different local scopes would not refer to the same object.
No. Symbols not tagged as global in an assembly/object file cannot be used to resolve references from other files at link time; they're ignored. (The lowercase d from nm indicates that it's a local symbol not a global one.) Within the same assembly/object file, the C source level rule that you can't have both external and static objects with same identifier at file scope rules it out.

How global and local with same static variable names stored in C internally memory?

#include<stdio.h>
static int a=5;
main()
{
static int a=15;
printf("%d\n",a);
}
So, how are both variables a stored in internal memory?
How are global and local variables with the same variable names stored internally in memory?
#include<stdio.h>
static int a=5;
int main()
{
printf("%p\n",(void *)&a);
static int a=15;
printf("%p\n",(void *)&a);
return 0;
}
Output for the upper program is
0x564e6b67a030
0x564e6b67a034
So you can see that both are stored in different addresses. As one is a global variable and other is local.
The names are only of interest to the human reader and the compiler/linker translating that code to machine executable code. The final object code resolves these to addresses and the names no longer exist.
The compiler distinguishes these the same way you do - by scope; when two identical symbols in the same namespace are in scope simultaneously, the symbol with the most restrictive scope is visible (i.e. may be accessed via the name).
For symbols with external linkage (in your example there are none other then main), the compiler retains the symbol name in order to resolve links between separately compiled modules. In the fully linked executable the symbol names cease to exist (except in debug build symbol meta-data).
The thing is the scope don't let them mess up. The first one has file scope and the other has block scope. (They are different variables - they are stored in separate memories.)
When you use it in the block - compiler checks whether this reference is resolved by anything in the same block. It gets one. And done.
And in case it is in some other function - if it doesn't find anything named a - the search ends in file scope where it finds the name a. That is where the story ends.
Both being static their storage duration is same. They live till the program exists. But their scope is different. If the scope was same too - compiler would have shown you error message.
Here if you compile with -Wshadow option - it will warn you about shadowing a variable. You shadowed the outer a with the inner on that block. That's it.
The facetious answer is that they are stored in different places.
Remember that the names of variables do not (normally) form part of the compiled program, so the compiler just follows the normal rules of variable shadowing. So in your case your print function (that's not a standard C function by the way - did you mean printf?) outputs the a declared in main. The fact that you've used the same name will not bother the compiler at all.
Finally C provides no way of accessing the global scoped a once the other declaration is encountered in main as it's static. (It is wasn't static you could use extern.) See How can I access a shadowed global variable in C?

Global variables in c library

How can someone ensure whether source code of some c library (.a) uses global variables or not? Is there any tool which can parse such details?
A library consist of modules (compiled C files). A module can declare a global variable. Any module (inside or outside the library) referencing the global variable will cause the module that declares the global variable to be included in your build.
A global variable can be static. It is then only visible in that module.
A global variable can be extern. That means it does not exist in the current module, and could not exist in the library at all in which case the user must provide the variable.
In the first and the third case, the variable will be listed in the symbol table of the library or in the fixup table of the library. The first is the list of symbols available to a callee; the second a list of variables whose exact address must still be fixed at load time. There may be variables that must be fixed-up that are not exported. Symbols always include the name, fix-ups don't need to be listed with their name.
So a tool to look at the symbol table of the library is probably what can answer your question.
Note: the fact that a global variable is listed in the symbol table of the library does not imply the variable is used by any function in the library.
By hand:
In case you've already known how to open a .a file, take a look in it.
If in the file, there is:
A variable is used without declaration (so it is defined in another file)
A variable is declared out of every function
So this file contains global variable.
By tool:
Every compiler have this option for you (I believe)
For example:
With GCC, using GDB, type info variables to list all the global variable.
With VS, open Class View, find Global Functions and Variable

Just want to make sure I'm understanding what an external variable is

For our assignment we have to code a program in C, but it says not to use external variables in the program. Does this mean variables in other files brought into the main code, or am I unable to use variables in the same file if they're not in the same function? (ie: could I pass a value into a function as an argument and have it return a value that may have to do with a variable in that function and set the return value equal to something, or is that using external variables?)
I've Googled around but it's not exactly clear, and I want to make sure, as this is rather important.
Just to be sure, I'd make all my variables part of a function, and either pass them as parameters or return them from the functions.
There are at least two interpretations of external variables.
First off, we have the extern keyword, which basically symbolizes what you would call a global variable. It's a variable declared in multiple translation unit, but it only exists in one place in memory. It is initialized in a single file and all subsequent changes affect every scope the variable is used in:
//globals.cpp
int x = 1337;
//main.cpp
extern int x;
int main()
{
//x is 1337 here
return 0;
}
The second meaning could be a variable that is declared and defined in class scope, but not used as extern. However, you could consider it external to the methods.
//main.cpp
int x = 1337; //is this external?
//could be, remove it just to be safe
int main()
{
return 0;
}
Let's go with all the possible cases, because from your question I'm not too sure either:
There is extern as a keyword. In order to understand this, you need to understand compilation units. Basically, each file is a compilation unit - so each .c is compiled to a .o with the headers substituted in place. In each compilation unit, you forward declare symbols you expect to use - functions belonging to other compilation units, for example.
Now, if you declare a global variable in one .c file, it is global wrt that file, but does not exist as a symbol in any other file at all - the compiler will error because it doesn't know where that variable was declared.
(Of course, if you declare the variable in a header - it will exist in all of the objects the header is included in, and then the linker will sulk, because when it links all the objects up some of the symbols will have the same name).
To get around this, it is possible to define a variable with extern int x;, for example. This tells the compiler a) int x should be available to this compilation unit, b) int x is not in this compilation unit and c) the linker should check it exists somewhere in all the units you've put together to form a library or program.
Conceptually, you're doing this all the time with forward-declarations of functions. There's just no way to forward declare a variable. In fact, you can do this with functions too and not bother r.e shared headers, although this is not really a good idea.
The other case is that "external variables" mean something external to a certain scope or module you have. I would check your assignment very carefully and if in doubt ask - whoever set it should be able to explain to you exactly what they mean.
An external variable is a variable with external linkage.
A variable with external linkage is a variable defined at file scope without the static keyword.
int bla = 0; // external variable
static int blop = 1; // non-external variable
int main(void)
{
return bla
}
Note that a variable declared with the extern keyword doesn't necessarily have external linkage. Like const does not mean constant in C, extern does not mean external.
People often use the word external to say that a variable is actually declared in the current translation unit but is defined in another translation unit.

Resources