c++ - Static locals in methods and ELF OS ABI -


given program

$ cat main.cpp  #ifndef with_local_static static int y = 0; #endif  class x {   public:     void foo() { #ifdef with_local_static         static int y = 0; #endif         ++y;     } };  int main() {     x().foo();     return 0; } 

compiled in 2 different ways:

$ g++ main.cpp -o global $ g++ main.cpp -dwith_local_static -o local 

i 2 different binary formats:

$ file local local: elf 64-bit lsb executable, x86-64, version 1 (gnu/linux), dynamically linked (uses shared libs), gnu/linux 2.6.24, buildid[sha1]=0x8d8e998601e44115b5fa6c0e71f3ed97cb13a0bd, not stripped $ file global global: elf 64-bit lsb executable, x86-64, version 1 (sysv), dynamically linked (uses shared libs), gnu/linux 2.6.24, buildid[sha1]=0x3481ba7c6969ed9d1bd1c8ce0f052d574023d488, not stripped 

can explain why elfosabi_linux in 1 case, elfosabi_none in other? compiler gcc 4.7.2

the background in environment, loader rejects executables not elfosabi_none.

the gnu/linux format superseded system v object file format, , gcc believes minimum os/abi executable run gnu/linux.

that happens when program has symbols type stt_gnu_ifunc (a gnu extensions denotes indirect function), , these symbols typically coming glibc. when introduced local static variable gcc added (more) code translation unit handle initialisation , destruction (along lines of _zzz__static_initialization_and_destruction_iii), , relevant parts of glibc came play.

first of all, best bet follow advice in question: how avoid stt_gnu_ifunc symbols in binary?

second, have on box, both old gcc 4.4 , new clang 3.4 generate global , local binaries sysv standard elfs, either test case missing more of relevant bits , pieces, or possibly using custom configured , built gcc, custom linker, or non-standard glibc.

more avenues can pursue investigate how did end these gnu indirect functions in binary:

  • run nm on binary , identify indirect symbols, should have i type. (see below.)
  • generate link map and/or assembly output , trace these indirect symbols specifics of code.
  • additionally check if locating glibc ldd -v $(type -p gcc) points non-standard libc

i - pe format files indicates symbol in section specific implementation of dlls. elf format files indicates symbol indirect function. gnu extension standard set of elf symbol types. indicates symbol > if referenced relocation not evaluate address, instead must invoked @ runtime. runtime execution return value used in relocation.

https://sourceware.org/binutils/docs/binutils/nm.html


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -