Bug creation and email sending has been disabled, file new bugs at gcc.gnu.org/bugzilla
Bug 114 - Add support for the GCC 'naked' attribute
Summary: Add support for the GCC 'naked' attribute
Status: RESOLVED FIXED
Alias: None
Product: GDC
Classification: Unclassified
Component: gdc (show other bugs)
Version: development
Hardware: All All
: --- enhancement
Assignee: Johannes Pfau
URL:
Depends on:
Blocks:
 
Reported: 2014-04-02 13:42 CEST by Mike
Modified: 2014-04-15 14:19 CEST (History)
1 user (show)

See Also:


Attachments
Patch to add @attribute("naked") (2.56 KB, patch)
2014-04-05 09:17 CEST, Johannes Pfau
Details | Diff
Patch to add @attribute("naked") (3.09 KB, patch)
2014-04-06 13:20 CEST, Johannes Pfau
Details | Diff
Patch to add target attributes into lookup table (2.12 KB, patch)
2014-04-07 13:19 CEST, Iain Buclaw
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Mike 2014-04-02 13:42:03 CEST
I'm targeting ARM Cortex-M bare metal, and I need my reset interrupt handler marked with the 'naked' attribute as it will be called before I've initialized my the hardware containing my stack.  

It can also be used to make a useful hard fault handler for finding elusive bugs as described here.  http://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html

I could do what I need in assembly, but then I'd break my "do it all in D" goal.

I propose the following syntax:

@attribute("naked") void myNakedFunction() { }

The GCC 'naked' attribute is documented here: gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Comment 1 Johannes Pfau 2014-04-05 09:17:43 CEST
Created attachment 64 [details]
Patch to add @attribute("naked")

Can you test this patch please?

(I was kinda surprised it didn't have an effect on x86 till I read that it's only supported on some architectures and not on x86...)

@Iain
Do you think we should include such system-specific attributes? Naked is also special in that dmd already has a 'generic' syntax for it, but it's tied to DMD-style inline asm.
I also had to add a noclone attribute because naked automatically sets noclone. Do you think we should somehow hide 'noclone' from @attribute / user code?
Comment 2 Mike 2014-04-05 16:04:13 CEST
Thank you for addressing this.

I had trouble applying this patch...

-----------------------------------------------------
patching file gcc/d/d-lang.cc
Hunk #2 FAILED at 58.
1 out of 3 hunks FAILED -- saving rejects to file gcc/d/d-lang.cc.rej
-----------------------------------------------------

... so I edited the d-lang.cc directly.  GDC built without any problems using the latest 4.8.2 branch and these instructions on the wiki:  http://wiki.dlang.org/Bare_Metal_ARM_Cortex-M_GDC_Cross_Compiler.

However, when compiling my program, I get the following error:
-----------------------------------------------------
<built-in>:0: internal compiler error: in register_scoped_attribute, at attribs.c:291
0x6325b4 register_scoped_attribute
	../../gcc-4.8.2/gcc/attribs.c:291
0x632645 register_scoped_attributes(attribute_spec const*, char const*)
	../../gcc-4.8.2/gcc/attribs.c:149
0x6329a8 init_attributes()
	../../gcc-4.8.2/gcc/attribs.c:254
0x6331bc decl_attributes(tree_node**, tree_node*, int)
	../../gcc-4.8.2/gcc/attribs.c:356
0x805dce add_builtin_function_common
	../../gcc-4.8.2/gcc/langhooks.c:557
0x8065f3 add_builtin_function(char const*, tree_node*, int, built_in_class, char const*, tree_node*)
	../../gcc-4.8.2/gcc/langhooks.c:577
0x650def do_build_builtin_fn
	../../gcc-4.8.2/gcc/d/d-builtins.c:1055
0x657138 d_init_builtins()
	../../gcc-4.8.2/gcc/builtins.def:195
0x664c66 d_backend_init()
	../../gcc-4.8.2/gcc/d/d-builtins.c:1765
0x633ef4 d_init
	../../gcc-4.8.2/gcc/d/d-lang.cc:257
0x8ee1d6 lang_dependent_init
	../../gcc-4.8.2/gcc/toplev.c:1688
0x8ee1d6 do_compile
	../../gcc-4.8.2/gcc/toplev.c:1850
-----------------------------------------------------
This occurs regardless of whether I use any attributes or not.
Comment 3 Johannes Pfau 2014-04-05 18:05:27 CEST
strange, it applies just fine here.

patch -p1 -i naked.patch
tested on revision fdf0c614b8aef10b846689bef39030c482692b5f

The error could be caused by failed/incorrect patching?
Comment 4 Mike 2014-04-06 00:38:58 CEST
I got the patch applied by downloading it from a different computer (had a line ending problem, I think).

Nevertheless, the error is the same.  I'm using the same revision (fdf0c614b8aef10b846689bef39030c482692b5f).

My program builds fine without this patch.

Have you tried this patch on one of the ARM cross-compilers?
Comment 5 Iain Buclaw 2014-04-06 09:50:40 CEST
If memory serves, there's a target hook which you can test if an attribute is handled by the backend. Use this instead of making naked a frontend attribute.
Comment 6 Johannes Pfau 2014-04-06 13:20:59 CEST
Created attachment 65 [details]
Patch to add @attribute("naked")

Updated patch. Naked is a 'target attribute' and needs to be handled in a special way. This should work now, @Iain can you review this?

(AFAICS we can't access the list of available target attributes, so @attribute("naked") will always compile but you'll get a warning from the backend if naked is not supported on this architecture)
Comment 7 Johannes Pfau 2014-04-06 13:27:37 CEST
@Iain there's TARGET_ATTRIBUTE_TABLE but I don't know if/how I could access that?
Comment 8 Mike 2014-04-06 14:18:16 CEST
The latest patch seems to work well.

/**** void OnReset() ****/
 8000024:	b580      	push	{r7, lr}
 8000026:	af00      	add	r7, sp, #0
 8000028:	2003      	movs	r0, #3
 800002a:	f000 f885 	bl	8000138 
 800002e:	f8df 2004 	ldr.w	r2, [pc, #4]	; 8000034
 8000032:	4710      	bx	r2

/**** @attribute("naked") void OnReset() ****/
_D5start7OnResetFZv():
 8000024:	2003      	movs	r0, #3
 8000026:	f000 f883 	bl	8000130 
 800002a:	f8df 2004 	ldr.w	r2, [pc, #4]	; 8000030 
 800002e:	4710      	bx	r2
Comment 9 Iain Buclaw 2014-04-06 14:49:53 CEST
targetm.attribute_table - when we initialise our string table of attributes, load them in too.
Comment 10 Iain Buclaw 2014-04-07 13:19:24 CEST
Created attachment 66 [details]
Patch to add target attributes into lookup table

Added patch which describes what I was talking about.

There used to be a TARGET_OPTION_VALID_ATTRIBUTE_P hook, but it seems to have been removed.