This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Linker version script turning weak symbols into strong symbols (workaround)


Dave Korn wrote:

>   So does the weak-symbol-plus-alias solution work for you just so long as you
> /don't/ declare the symbol local?

So for the sake of the list archives I figured I'd post the eventual
solution I'm using.

There are two modules, moduleA and moduleB, both are DSOs. moduleA
provides an API function (global symbol) that is canonically called
'foo', and moduleB is an optional consumer of that API function, but can
also operate without it if moduleA was not loaded at the time moduleB
got loaded. I have created some macros to produce the declarations and
definitions below, but the macros themselves are not important to this
list :-)

In moduleA:

int __ast_agi_register (struct ast_module *mod, agi_command *cmd)
{
...
}
__attribute__((alias("__ast_agi_register"))) typeof(__ast_agi_register)
ast_agi_register;

In moduleB:

static int __stub__ast_agi_register (struct ast_module *mod, agi_command
*cmd) { return (INT_MIN); }
static __attribute__((weakref("__ast_agi_register")))
typeof(__stub__ast_agi_register) __ref__ast_agi_register;
static __attribute__((unused)) typeof(__stub__ast_agi_register) *
ast_agi_register;
static void __attribute__((constructor)) __init__ast_agi_register(void)
{ ast_agi_register = __ref__ast_agi_register ? : __stub__ast_agi_register; }

This allows all the code in both modules to reference the function as
'ast_api_register' without having to care whether moduleA is providing
it or not; if it is not, then the stub implementation provides a default
return value so the caller of the function sees it as 'failure'.

This method eliminated the use of global symbols in moduleB entirely, so
now the linker version script no longer interferes.

-- 
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
skype: kpfleming | jabber: kpfleming@digium.com
Check us out at www.digium.com & www.asterisk.org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]