private global vs. public global - nasm

What's the difference between the export of a symbol by using "public global" instead of "private global"?

See the NASM docs: private segments do not get combined, public segments do. Nothing to do with symbols directly AFAIK. global is used for exported symbols.

Related

C++ programmin with singleton

i have list of diff. classes.
in that man static variables is there.
i need to remove the static variables .. and singleton class instead of static variables.
How can i achieve this . any suggestions?

Why are nonstatic global variables defined in shared objects referenced using GOT?

I mean nonstatic global variables that are defined in the module, not externally.
Why does it have to be referenced through GOT? Why no use relative address?(since the distance between the global variable in the .data section and the reference in the .text section is a constant, it's also position-independent)
Since your variable isn't static, on ELF it can be interposed (think of LD_PRELOAD).
I also have the same confusion.
If there is one global variable with same name defined in both two DSOs and they are dynamically linked into one executable, will both libraries access the same copy of the variable?
And which library(library's data section) the variable locates depends on the order of libraries loading?

Create both static and shared library with GNU libtool?

I am using the GNU autotools (including automake!) for my project. I would like to know if I could create a static and a shared library using libtool? Or would the declarations be separate? Would this:
LT_INIT(shared static)
work?
Nothing besides LT_INIT is needed, it defaults to building both static and shared libraries. If you like, you can again explicitly state the defaults (but it is sort of redundant)
LT_INIT
AC_ENABLE_SHARED
AC_ENABLE_STATIC
edit: manual says LT_INIT([shared]) and LT_INIT([static]) (combined to LT_INIT([shared static]) shall also work. Also manual's more precise wording on what's default when LT_INIT is given:
this macro turns on shared libraries if they are
available, and also enables static libraries if they don't
conflict with the shared libraries.

Global Static Pointer Issue

This is a rather a problem from a convoluted situation. I have a static pointer sitting in one of my header files. Which is being included everywhere, this is why I put it as a static pointer. So that I can quickly initilize it in my main function so that other files can use it.
The problem is this, even after I initialize it and put stuff into it. Other files only find it NULL. It is like every file that includes the header with the static pointer makes a copy of it for itself and even when others initialize it, each file has their own separate copy. Negating ofcourse, the purpose of having a global variable.
How can I cope up with this?. Maybe I am understanding a static variable wrong, or maybe is it because its a pointer?
Should i be declaring it as: &variable = 5; or just as variable = 5; or &variable = (int)5?
It is like every file that includes the header with the static pointer makes a copy of it for itself and even when others initialize it, each file has their own separate copy.
That's what static means when applied to a variable at namespace scope: the variable is given internal linkage, making it "local" to a given translation unit (source file).
If you have a static variable at namespace scope in a header file and you include that header file in multiple .cpp files, there will be multiple instances of that variable: one for each of the .cpp files that include the header file.
If you want a global variable that is shared across multiple source files, you need to make it extern. Declare the variable as extern in the header file, then define the extern variable in exactly one of your .cpp files.
Yes, you are understanding the static keyword wrong. Static keyword at namespace and file scope declares a variable that is only visible inside the translation unit (basically a single .cpp file plus all of its includes) where it is declared. By declaring a static variable in header that you use in multiple .cpp files you are effectively defining multiple variables.
If you want a global pointer then declare it extern without static in the single header and define it without extern or static in a single .cpp file. E.g.:
In foo.h:
extern int * p;
and in main.cpp:
int * p = something;
Defining the global pointer static results in the C++ compiler creating a separate pointer for every translation unit, the definition is included in. I'm pretty sure it's not what you want and it's the cause of your problems.
You should declare it extern in a header and define once in one cpp file.
I can imagine the confusion came from the way, static keyword works inside a class or a struct definition. Indeed, it works differently there and it creates one variable, common for all instances of the class or struct.
Btw. In case you actually want a variable to be visible only in one translation unit the recommended way is to put ii into an anonymous namespace. static works too, but it's use is discouraged as deprecated.

Main Program and Shared Library initializes same static variable in __static_initialization_and_destruction_0

Does anyone know why a library initialized within dlopen() would initialize a static variable owned by the main program. Both the main program and shared library have a copy of the static variable, but for some reason the shared library re-initializes the main program's copy of the static variable and destructs it, causing a segfault when the main program attempts to destruct it.
Is this a case of bad name mangling in the symbol table?
This is a case where the runtime linker only wants a single active copy of a symbol in a process. If both a shared object and the executable have a copy of the symbol, the runtime linker will resolve all references to one of those.
What you can do to solve this problem is to use symbol reduction using the version command of the link editor when building the shared object. Make sure the symbol for the static variable is not global and you will get the behavior you are looking for.

Resources