This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: return statement in a function with the naked attribute
- From: David Brown <david at westcontrol dot com>
- To: Geert Bosch <bosch at gnat dot com>
- Cc: reed kotler <rkotler at mips dot com>, Ian Lance Taylor <iant at google dot com>, <gcc at gcc dot gnu dot org>, <rdsandiford at googlemail dot com>
- Date: Fri, 3 May 2013 13:57:41 +0200
- Subject: Re: return statement in a function with the naked attribute
- References: <5182EC4D dot 4070207 at mips dot com> <CAKOQZ8wgXQfgpu9kv74c2orXMQw-jVczARicMhXMOgQpyaBNpg at mail dot gmail dot com> <5183361A dot 90705 at mips dot com> <CAKOQZ8z3rAkAD369vHu5Nt0ZfQOMPauaxxXc-C8Nnqxy2WbgXg at mail dot gmail dot com> <518339E0 dot 6060608 at mips dot com> <4F21C2F7-37E4-4D35-AAD2-F5F3CE59A998 at gnat dot com>
On 03/05/13 06:40, Geert Bosch wrote:
>
> On May 3, 2013, at 00:15, reed kotler <rkotler@mips.com> wrote:
>
>> There was some confusion on the llvm list because some tests were run on targets that did not support the naked attribute.
>>
>> I think we are thinking now that the return statement should not be emitted unless explicitly requested.
>>
>> It's not totally clear in the gcc manual so that is why I was asking.
>
> I clearly is an error to have a function that doesn't return.
That is certainly not true.
In the world of application programming, functions will typically
return. But in low-level systems programming and embedded systems
programming, there are a number of times when a function will not
return. There is a gcc attribute "noreturn" to make that explicit.
Typical cases include the main loop of an embedded system (which
normally never exits), main loops in threads, and functions that exit in
an odd way (such as by starting a new program, reseting the system,
doing a longjmp).
When you use the "naked" attribute, you are telling the compiler that
you know exactly what you are doing, and it should give you exactly what
you ask for - and no more. You are writing in assembly - if you want
the function to return, you write "ret" - or "reti" or whatever
variation of "return" is appropriate. You don't want the compiler to
guess and pick the return form it thinks might fit.
There are also cases when you want assembly code that runs off the end,
without returning - you use sections along with a linker script to tie
them all together without calls and returns. In such code, an
unexpected return would be a disaster.
For an example of such usage, see this section of the AVR gcc port
documentation:
<http://www.nongnu.org/avr-libc/user-manual/mem_sections.html#c_sections>
> So, what you really asking is: "What are the semantics of a (naked)
> functon that doesn't return?"
>
> I think it would make sense for a compiler to emit a special error-return
> (such as abort()) at the end of such a function, with the expectation that
> this code usually would be unreachable and optimized away.
>
> I don't think it makes sense to try and define any other semantics
> for a funciotn that doesn't explicitly return a value.
>