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]
Other format: [Raw text]

Re: return statement in a function with the naked attribute


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. 
> 


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