Bug 24863 - New __attribute__((alias("target"))) requirement break aliasing assembler functions
Summary: New __attribute__((alias("target"))) requirement break aliasing assembler fun...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2005-11-14 21:47 UTC by Jens Arnold
Modified: 2009-03-30 01:19 UTC (History)
1 user (show)

See Also:
Host:
Target: all
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jens Arnold 2005-11-14 21:47:03 UTC
The new requirement for (weak) symbol aliasing:

<Quote from GCC 4.0.x release notes>
Given __attribute__((alias("target"))) it is now an error if target is not a
symbol, defined in the same translation unit. This also applies to aliases
created by #pragma weak alias=target. This is because it's meaningless to define
an alias to an undefined symbol. On Solaris, the native assembler would have
caught this error, but GNU as does not.
</Quote>

breaks compilation of code like this (example from an sh-elf target, but the
effect is architecture-independent):


extern __attribute__((weak,alias("UIE11"))) void NMI(void);
void UIE11 (void);

asm(
    "_UIE11:\tbsr\t_UIE\n\tmov.l\t@r15+,r4\t\n"
);


which is perfectly valid and worked in all of gcc 3.0.x, gcc 3.3.x and gcc 3.4.x
Comment 1 Andrew Pinski 2005-11-14 22:19:55 UTC
I don't think this is valid to do this, you are hiding away a lot of code.
Comment 2 Jens Arnold 2005-11-14 22:55:36 UTC
I am indeed hiding a lot of code, but hopefully in order to pinpoint the issue.
This code snippet is taken from the rockbox project http://www.rockbox.org/ , precisely from 
http://www.rockbox.org/viewcvs.cgi/firmware/system.c?annotate=1.73
Check lines 652-1019.

This is part of the exception handling on the SH1 target CPU. All exceptions are
handled in one C handler function, UIE(). However, the only way to get the
exception number on SH1 is checking the called handler address, so all
exceptions that aren't handled by other code are handled by tiny assembler
snippets which just fetch their callee address and pass it to UIE(), which then
checks the address it was called from.
Comment 3 Andrew Pinski 2005-11-14 23:02:03 UTC
(In reply to comment #2)
This seems like a hack instead of using different files for the asm and C function and then only link in the files which are needed.
Comment 4 Jens Arnold 2005-11-14 23:16:37 UTC
How do you mean, it seems like a hack? Obviously we can't put the asm in a different file, because then the symbols would clearly be defined in a different translation unit. As-is they are not, but gcc 4.0.x errors because it doesn't see the symbols in the inline asm.

There are two main points which require the code to work like it does:

(1) As mentioned, the exception number can only be derived from the called address. UIE() is there to catch all unexpected interrupts and exceptions, i.e. those for which there are no specific handlers. That's why all _UIE##number symbols are defined in the asm block, in a regular structure that allows to compute the vector number from the called address easily.

(2) We need to weak-alias these asm symbols because they should only be used when there is no dedicated handler for the specific interrupt or exception defined in any other source file. Perhaps it would be possible to hard code which handlers are implemented elsewhere and which are not, but that would be rather hard to maintain.
Comment 5 Joseph S. Myers 2009-03-30 01:19:00 UTC
I think it's correct for the compiler to reject this and require that you
make the function definition visible to the compiler.  You can do that using
a naked function; if naked functions aren't supported for your target, that's
a separate feature request for that target to add support for that attribute.