public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86)
@ 2011-08-30 20:34 congruwer at yahoo dot co.uk
  2011-08-30 20:50 ` [Bug target/50242] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: congruwer at yahoo dot co.uk @ 2011-08-30 20:34 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

             Bug #: 50242
           Summary: __attribute__((naked)) is ignored on IA32 (x86)
    Classification: Unclassified
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: congruwer@yahoo.co.uk


Given the follow c++ file:
---(start)---
__attribute__((naked)) void test(int a)
{
    asm("//Do stuff");
    asm("ret $4");
};

struct myclass
{
    __attribute__((naked)) ~myclass()
    {
        asm("//Do stuff");
        asm("ret");
    }

    virtual void virt() { };
};

void testmyclass()
{
    myclass x;
}
---(end)---
The compiler will give the following complaints:
testnaked.cpp:1:39: warning: 'naked' attribute directive ignored
testnaked.cpp:9:34: warning: 'naked' attribute directive ignored
Additionally, prolog and epilog code is generated. The relevant parts of the
assembler output with added commentary follow:
---(start)---
__Z4testi: //test(int)
/APP
 # 3 "testnaked.cpp" 1
    //Do stuff
 # 0 "" 2
 # 4 "testnaked.cpp" 1
    ret $4
 # 0 "" 2
/NO_APP
    ret // This ret shouldn't be here.
...(snip)...
__ZN7myclassD1Ev: //myclass::~myclass()
    movl    4(%esp), %eax // Destructor prolog that restores the vtable.
    movl    $__ZTV7myclass+8, (%eax) // These 2 lines shouldn't be present.
/APP
 # 11 "testnaked.cpp" 1
    //Do stuff
 # 0 "" 2
 # 12 "testnaked.cpp" 1
    ret
 # 0 "" 2
/NO_APP
    ret // This ret shouldn't be here.
---(end)---
My apologies for the AT&T syntax, but that's what -S yields.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug target/50242] __attribute__((naked)) is ignored on IA32 (x86)
  2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
@ 2011-08-30 20:50 ` pinskia at gcc dot gnu.org
  2011-08-31 16:11 ` [Bug target/50242] __attribute__((naked)) is not implemented " congruwer at yahoo dot co.uk
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-08-30 20:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |target

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-08-30 20:36:42 UTC ---
naked is a target specific attribute which has not been added for x86 yet.

Is there a reason why you want the naked attribute?


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug target/50242] __attribute__((naked)) is not implemented on IA32 (x86)
  2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
  2011-08-30 20:50 ` [Bug target/50242] " pinskia at gcc dot gnu.org
@ 2011-08-31 16:11 ` congruwer at yahoo dot co.uk
  2011-08-31 17:12 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: congruwer at yahoo dot co.uk @ 2011-08-31 16:11 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

--- Comment #2 from congruwer at yahoo dot co.uk 2011-08-31 15:46:19 UTC ---
Sometimes I want to implement an entire method or function in assembler. The
main reasons are:
1) I want to thunk to another function &c. in a way that is hard to do from
C++.
2) The compiler generated horrible code, because of an optimization bug for
example.
3) I need to execute a some specific opcodes.
For loose functions you can sometimes get away with using a definition in a
separate assembler module (although getting the name mangling right is a pain)
but for methods this is often impossible since C++ doesn't see the assembler
definition of the method (or constructor &c.) and that can cause problems.

The most recent problem that made me want this was a badly optimised destructor
that pulled in a lot of dependencies via the destructor prologue. I really want
the destructor in question to be inlined (since it's essentially empty) but
that doesn't work when the destructor is defined in a separate file. Trying to
use asm() won't work since the problem is in the prologue and the compiler
ignores the naked attribute. For now I've deleted it, but that is a cause of
potential bugs since a non-existent destructor doesn't have the protected:
visibility specifier.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug target/50242] __attribute__((naked)) is not implemented on IA32 (x86)
  2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
  2011-08-30 20:50 ` [Bug target/50242] " pinskia at gcc dot gnu.org
  2011-08-31 16:11 ` [Bug target/50242] __attribute__((naked)) is not implemented " congruwer at yahoo dot co.uk
@ 2011-08-31 17:12 ` jakub at gcc dot gnu.org
  2011-09-01  8:10 ` congruwer at yahoo dot co.uk
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-08-31 17:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-08-31 16:10:59 UTC ---
You can define whole functions in toplevel asms, like:
asm (".globl foobar; .type foobar, @function;\n"
     "foobar: whatever; ret; .size foobar, . - foobar;\n");


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug target/50242] __attribute__((naked)) is not implemented on IA32 (x86)
  2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
                   ` (2 preceding siblings ...)
  2011-08-31 17:12 ` jakub at gcc dot gnu.org
@ 2011-09-01  8:10 ` congruwer at yahoo dot co.uk
  2011-09-01  8:13 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: congruwer at yahoo dot co.uk @ 2011-09-01  8:10 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

--- Comment #4 from congruwer at yahoo dot co.uk 2011-09-01 08:09:59 UTC ---
Doesn't work:
---(start)---
inline __attribute__((always_inline)) void foobar();

asm (".globl __6foobarv; .type __6foobarv, @function;\n"
     "__6foobarv: ret; .size __6foobarv, . - __6foobarv;\n");

void test()
{
    foobar();
}
---(end)---
test.cpp:1:44: error: inline function 'void foobar()' used but never defined


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug target/50242] __attribute__((naked)) is not implemented on IA32 (x86)
  2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
                   ` (3 preceding siblings ...)
  2011-09-01  8:10 ` congruwer at yahoo dot co.uk
@ 2011-09-01  8:13 ` jakub at gcc dot gnu.org
  2011-09-01  9:16 ` congruwer at yahoo dot co.uk
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-09-01  8:13 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-09-01 08:12:30 UTC ---
Obviously it can't be inline...
You can't inline a naked function either.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug target/50242] __attribute__((naked)) is not implemented on IA32 (x86)
  2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
                   ` (4 preceding siblings ...)
  2011-09-01  8:13 ` jakub at gcc dot gnu.org
@ 2011-09-01  9:16 ` congruwer at yahoo dot co.uk
  2011-09-01  9:21 ` congruwer at yahoo dot co.uk
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: congruwer at yahoo dot co.uk @ 2011-09-01  9:16 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

--- Comment #6 from congruwer at yahoo dot co.uk 2011-09-01 09:16:18 UTC ---
Why not? In any case, without inline it also doesn't work:
test.s: Assembler messages:
test.s:628: Warning: .type pseudo-op used outside of .def/.endef ignored.
test.s:628: Error: junk at end of line, first unrecognized character is `_'
test.s:629: Warning: .size pseudo-op used outside of .def/.endef ignored.
test.s:629: Error: junk at end of line, first unrecognized character is `_'
I've tried fixing the problem, but made no headway. I can get gas so swallow it
by copying what the C++ compiler emits for a function, but that just gives a
linker error later on:
test.cpp:16: undefined reference to `foobar()'

In any case, the function really should be inline... what now? Use sed to edit
the .s file? Will that preserve C++ line number information? (Debugging would
be a real pain if all the line numbers were shifted by a byte or two.)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug target/50242] __attribute__((naked)) is not implemented on IA32 (x86)
  2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
                   ` (5 preceding siblings ...)
  2011-09-01  9:16 ` congruwer at yahoo dot co.uk
@ 2011-09-01  9:21 ` congruwer at yahoo dot co.uk
  2011-12-25 15:45 ` radoslaw.biernacki at gmail dot com
  2011-12-25 17:22 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: congruwer at yahoo dot co.uk @ 2011-09-01  9:21 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

--- Comment #7 from congruwer at yahoo dot co.uk 2011-09-01 09:21:33 UTC ---
Answering my own question: because of the ret instruction. Duh.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug target/50242] __attribute__((naked)) is not implemented on IA32 (x86)
  2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
                   ` (6 preceding siblings ...)
  2011-09-01  9:21 ` congruwer at yahoo dot co.uk
@ 2011-12-25 15:45 ` radoslaw.biernacki at gmail dot com
  2011-12-25 17:22 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: radoslaw.biernacki at gmail dot com @ 2011-12-25 15:45 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

Radoslaw Biernacki <radoslaw.biernacki at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |radoslaw.biernacki at gmail
                   |                            |dot com

--- Comment #8 from Radoslaw Biernacki <radoslaw.biernacki at gmail dot com> 2011-12-25 15:37:49 UTC ---
I will attach my comment to discussion.

I see the reason for naked attribute. I develop an embedded OS for small uC and
I would like to make a port for x86 Linux userspace. The scheduler for my OS
will behave as "fibres implementation". It will be similar to old fashioned
setcontext getcontext calls.

I need to compile following function:
void OS_NAKED OS_HOT os_context_switch(os_task_t *new_task)
{
   arch_contextstore_u();
   task_current = new_task;
   task_current->state = TASKSTATE_RUNNING;
   arch_contextrestore_u();
}

where arch_contextstore_u and arch_contextrestore_u are the architecture
depended function implemented by me in pure asm. arch_contextrestore_u will
make a return to address stored in task_current context (previously pushed on
stack by call (x86) instruction while calling os_context_switch).

The problem that I have is, that for x86 Linux environment, gcc does not
support the __attibute__ ((naked)). This attribute is supported on embedded
environments (bare metal) of all gcc ports that I found (like ARM, msp430-gcc,
avr-gcc etc).

So I'm the next person that wait for this implementation on x86 Linux.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug target/50242] __attribute__((naked)) is not implemented on IA32 (x86)
  2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
                   ` (7 preceding siblings ...)
  2011-12-25 15:45 ` radoslaw.biernacki at gmail dot com
@ 2011-12-25 17:22 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-12-25 17:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50242

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-12-25 17:18:17 UTC ---
Dup of much older bug 25967.

*** This bug has been marked as a duplicate of bug 25967 ***


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2011-12-25 17:20 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-30 20:34 [Bug c++/50242] New: __attribute__((naked)) is ignored on IA32 (x86) congruwer at yahoo dot co.uk
2011-08-30 20:50 ` [Bug target/50242] " pinskia at gcc dot gnu.org
2011-08-31 16:11 ` [Bug target/50242] __attribute__((naked)) is not implemented " congruwer at yahoo dot co.uk
2011-08-31 17:12 ` jakub at gcc dot gnu.org
2011-09-01  8:10 ` congruwer at yahoo dot co.uk
2011-09-01  8:13 ` jakub at gcc dot gnu.org
2011-09-01  9:16 ` congruwer at yahoo dot co.uk
2011-09-01  9:21 ` congruwer at yahoo dot co.uk
2011-12-25 15:45 ` radoslaw.biernacki at gmail dot com
2011-12-25 17:22 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).