开发者

Generating link-time error for deprecated functions

开发者 https://www.devze.com 2023-02-05 05:38 出处:网络
Is there a way with gcc and GNU binutils to mark some functions开发者_StackOverflow such that they will generate an error at link-time if used? My situation is that I have some library functions which

Is there a way with gcc and GNU binutils to mark some functions开发者_StackOverflow such that they will generate an error at link-time if used? My situation is that I have some library functions which I am not removing for the sake of compatibility with existing binaries, but I want to ensure that no newly-compiled binary tries to make use of the functions. I can't just use compile-time gcc attributes because the offending code is ignoring my headers and detecting the presence of the functions with a configure script and prototyping them itself. My goal is to generate a link-time error for the bad configure scripts so that they stop detecting the existence of the functions.

Edit: An idea.. would using assembly to specify the wrong .type for the entry points be compatible with the dynamic linker but generate link errors when trying to link new programs?


FreeBSD 9.x does something very close to what you want with the ttyslot() function. This function is meaningless with utmpx. The trick is that there are only non-default versions of this symbol. Therefore, ld will not find it, but rtld will find the versioned definition when an old binary is run. I don't know what happens if an old binary has an unversioned reference, but it is probably sensible if there is only one definition.

For example,

__asm__(".symver hidden_badfunc, badfunc@MYLIB_1.0");

Normally, there would also be a default version, like

__asm__(".symver new_badfunc, badfunc@@MYLIB_1.1");

or via a Solaris-compatible version script, but the trick is not to add one.

Typically, the asm directive is wrapped into a macro.

The trick depends on the GNU extensions to define symbol versions with the .symver assembler directive, so it will probably only work on Linux and FreeBSD. The Solaris-compatible version scripts can only express one definition per symbol.

More information: .symver directive in info gas, Ulrich Drepper's "How to write shared libraries", the commit that deprecated ttyslot() at http://gitorious.org/freebsd/freebsd/commit/3f59ed0d571ac62355fc2bde3edbfe9a4e722845


One idea could be to generate a stub library that has these symbols but with unexpected properties.

  • perhaps create objects that have the name of the functions, so the linker in the configuration phase might complain that the symbols are not compatible
  • create functions that have a dependency "dont_use_symbol_XXX" that is never resolved
  • or fake a .a file with a global index that would have your functions but where the .o members in the archive have the wrong format


The best way to generate a link-time error for deprecated functions that you do not want people to use is to make sure the deprecated functions are not present in the libraries - which makes them one stage beyond 'deprecated'.

Maybe you can provide an auxilliary library with the deprecated function in it; the reprobates who won't pay attention can link with the auxilliary library, but people in the mainstream won't use the auxilliary library and therefore won't use the functions. However, it is still taking it beyond the 'deprecated' stage.

Getting a link-time warning is tricky. Clearly, GCC does that for some function (mktemp() et al), and Apple has GCC warn if you run a program that uses gets(). I don't know what they do to make that happen.


In the light of the comments, I think you need to head the problem off at compile time, rather than waiting until link time or run time.

The GCC attributes include (from the GCC 4.4.1 manual):

error ("message")

If this attribute is used on a function declaration and a call to such a function is not eliminated through dead code elimination or other optimizations, an error which will include message will be diagnosed. This is useful for compile time checking, especially together with __builtin_constant_p and inline functions where checking the inline function arguments is not possible through extern char [(condition) ? 1 : -1]; tricks. While it is possible to leave the function undefined and thus invoke a link failure, when using this attribute the problem will be diagnosed earlier and with exact location of the call even in presence of inline functions or when not emitting debugging information.

warning ("message")

If this attribute is used on a function declaration and a call to such a function is not eliminated through dead code elimination or other optimizations, a warning which will include message will be diagnosed. This is useful for compile time checking, especially together with __builtin_constant_p and inline functions. While it is possible to define the function with a message in .gnu.warning* section, when using this attribute the problem will be diagnosed earlier and with exact location of the call even in presence of inline functions or when not emitting debugging information.

If the configuration programs ignore the errors, they're simply broken. This means that new code could not be compiled using the functions, but the existing code can continue to use the deprecated functions in the libraries (up until it needs to be recompiled).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号