This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


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

#pragma weak conformance


	Is there any specific implementation of "#pragma weak" to which
GCC is attempting to conform?

	Although pragmas are compiler-dependent, the Sun C Compiler
documentation appears to limit weak aliases to declarations:

#pragma weak symbol1 = symbol2

defines symbol1 to be a weak symbol, which is an alias for the symbol
symbol2. This form of the pragma can only be used in the same translation
unit where symbol2 is defined, either in the sourcefiles or one of its
included headerfiles. Otherwise, a compilation error will result.

	The Sun C++ Compiler documentation mentions two forms: string
form and identifier form:

#pragma weak name1 = name2 

In the form #pragma weak name1 = name2, the symbol name1 becomes a weak
reference to name2. If name1 is not defined elsewhere, name1 will have the
value name2. If name1 is defined elsewhere, the linker uses that
definition and ignores the weak reference to name2. The following
directive instructs the linker to resolve any references to bar if it is
defined anywhere in the program, and to foo otherwise.

       #pragma weak bar = foo

In the identifier form, name2 must be declared and defined within the
current compilation unit. For example:

       extern void bar(int) {...}

       extern void _bar(int);

       #pragma weak _bar=bar

When you use the string form, the symbol does not need to be previously
declared. If both _bar and bar in the following example are extern "C",
the functions do not need to be declared. However, bar must be defined in
the same object.

       extern "C" void bar(int) {...}

       #pragma weak "_bar" = "bar"


	Looking at the GCC implementation in c-pragma:handle_pragma_weak()
and varasm.c:add_weak(), GCC does not distinguish between strings and
identifiers and behaves as if names always are strings:

	#pragma weak baz=bar
	foo () {}

is not an error for GCC.

	Does anyone consider this a bug?  Should GCC conform to Sun C/C++
definition of #pragma weak?  (Yes, I know that GCC's weak attribute is
better and more powerful, but that is not the question.)

	Can/Should GCC distinguish between the identifier and string forms
of #pragma weak? E.g., calling add_weak() for the string form (current
implementation) and looking up the declaration for the identifier form to
call handle_weak_attribute() and handle_alias_attribute()?

	Caveat: all of the Sun C/C++ examples show the #pragma weak
directives *after* the declaration, although the documentation does not
appear to require the ordering.  Deferring testing for the error until
after the entire compilation unit has been processed might be difficult
for GCC.

Thanks, David


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