-2

I have a bool type in a CPP source file. The variable cannot be made static. I want the variable placed in an initialized data segment.

According to the OS X ABI Mach-O File Format Reference, I believe the place I want the variable to reside is __DATA,__data from Table 2.

How can I force a variable an initialized data segment on OS X using Apple compilers?

I realize that I'm probably going to have to use something platform specific. I also realize it won't be portable C++.

jww
  • 83,594
  • 69
  • 338
  • 732
  • Also, post a [MCVE](http://stackoverflow.com/help/mcve) that gives the valgrind message – M.M Jul 13 '16 at 00:08
  • Could be a legitimate bug in your code them. – M.M Jul 13 '16 at 00:10
  • static initialization order would not be a problem - all static variables are zero-initialized before any dynamic initialization occurs – M.M Jul 13 '16 at 00:12
  • seems unlikely unless you actually wrote that attribute in your code – M.M Jul 13 '16 at 01:33
  • @M.M - no, its not there at the moment. I'm cutting in the changes now for testing. – jww Jul 13 '16 at 01:41
  • `bool g_x = true;` should also achieve the aim; if it doesn't seem to then there is probably some other problem in your code that still needs addressing. Seems like a large amount of code in your solution for unclear benefit – M.M Jul 13 '16 at 12:25
  • @M.M - `__attribute__((section("..."))` worked as expected. It cleared most of the Valgrind issues when testing above `-O1`. Most is 32 out of 36. The remaining ones are an empty string but they need a slightly different technique. Thanks for your help. – jww Jul 13 '16 at 12:25
  • @M.M - *"`bool g_x = true` should also achieve the aim..."* - right, but that's not how things work on OS X. For what its worth, that did work on every other platform and compiler I test. OS X is the reason I avoid these protracted debates. Apple does things differently, and many Linux or Windows developers have not really experienced the issues first hand. – jww Jul 13 '16 at 12:26
  • That's how it works in C++. You seem to be claiming the OSX version of clang is bugged, then. – M.M Jul 13 '16 at 12:31
  • @M.M - paint it however you like, but the issue is what it is. I suggest you purchase a Mac and try some of these things. Its a rude awakening, as they say. – jww Jul 13 '16 at 12:32

1 Answers1

0

To force data into an initialized data segment when available on OS X and Linux (but not GNU Hurd), perform the following. Note that this technique is platform specific, but it side steps C/C++ limitations on visibility and storage classes.

#if defined(__clang__ ) && !defined(__apple_build_version__)
   #define LLVM_CLANG_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
#elif defined(__clang__ ) && defined(__apple_build_version__)
   #define APPLE_CLANG_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
#elif defined(__GNUC__)
   #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif
...


#if ((__MACH__ >= 1) && ((LLVM_CLANG_VERSION >= 30600) || (APPLE_CLANG_VERSION >= 70100) || (GCC_VERSION >= 40300)))
   #define INIT_SECTION __attribute__((section ("__DATA,__data")))
#elif ((__ELF__ >= 1) && (GCC_VERSION >= 40300))
   #define INIT_SECTION __attribute__((section ("nocommon")))
#else
   #define INIT_SECTION
#endif

foo.h:

extern bool g_x;
extern bool g_y;

foo.cpp:

bool INIT_SECTION g_x, INIT_SECTION g_y;

I'm still working on the compiler version numbers, so they may not be as accurate as they could be. They tested OK under LLVM Clang, Apple Clang, GCC, and MacPorts GCC. But I suspect Clang will be able to move down, and GCC may need to move up.

jww
  • 83,594
  • 69
  • 338
  • 732