2

I am using different linker script. In some, a value is defined, in others, it is not defined:

DIRECTORY_ADDRESS = 0x80100000;
DIRECTORY_SIZE = 32M;

At execution, I want a default behavior when this value is not defined, and a special behavior when it is defined.

Classically, I get the values like this:

extern void * DIRECTORY_ADDRESS;
extern void * DIRECTORY_SIZE;

void f() { 
  void *dir_addr = &DIRECTORY_ADDRESS;
  int dir_size   = (int)&DIRECTORY_SIZE;
}

My first lead to conditionnaly execute code according to the existence of this value in linker script is the weak atttribute:

extern void * DIRECTORY_ADDRESS  __attribute__ ((weak)) = 0x0;
extern void * DIRECTORY_SIZE __attribute__ ((weak)) = 0x0;

void f() {
  if ( DIRECTORY_ADDRESS )
    // special code
  else
    // default code
}

But it can't work, as I am initializing the pointer value, and not its address: even an undifined weak symbol has an address. So directory address is always != NULL.

I am pretty sure this problem has already been solved, but I can't find any related problem on the web.

1
  • Can you just used shared libraries and check for failure to load the lib with your func in it?
    – John3136
    Commented Feb 7, 2012 at 10:27

2 Answers 2

2

My mistake!

I tried many combination, and this one is actually wrong:

As I am initliazing weak symbols, there are "half weak"

extern void * DIRECTORY_ADDRESS  __attribute__ ((weak)) = 0x0;
extern void * DIRECTORY_SIZE __attribute__ ((weak)) = 0x0;

To solve my problem, I just have to let weak symbols uninitialized, then the symbol address will be NULL if they are not defined with a strong symbol:

extern void * DIRECTORY_ADDRESS  __attribute__ ((weak));
extern void * DIRECTORY_SIZE __attribute__ ((weak));
1
  • They are not really "half weak"; weak symbols do have a default value. Commented Nov 23, 2020 at 11:03
2

Uninitialized globals are weak by default, so you can just use

void * DIRECTORY_ADDRESS;
void * DIRECTORY_SIZE;

If there is no external symbol defined for these names, the value will default to NULL. To force a different default value than NULL, you could use

void * DIRECTORY_ADDRESS __attribute__ ((weak)) = OTHER_VALUE;
void * DIRECTORY_SIZE __attribute__ ((weak)) = OTHER_VALUE;

Note that initializing an extern doesn't really make sense: extern means that the definition is external to the file, but initializing provides a definition. My compiler (gcc 4.4) warns the user of this, and then ignores the extern modifier. This means your example worked as expected for me (even when initializing to 0x0), but maybe your compiler handles this ambiguous situation differently.

1
  • Thanks. That's what I explain in my self-answer. Actually, the initialization of the weak symbol was one of the debugging step, it has nothing to do here!
    – Jérôme
    Commented Feb 22, 2012 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.