public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* section placement - grouping __attribute__((section (“NAME”))) statements
@ 2021-08-20  8:22 Werthmann Luca
  2021-08-20 18:55 ` David Brown
  0 siblings, 1 reply; 4+ messages in thread
From: Werthmann Luca @ 2021-08-20  8:22 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2411 bytes --]

Hello,

I am writing to ask if there is a chance with the GNU GCC Compiler to group __attribute__((section (“NAME”))) statements.

IAR Supports for example the following commands:

# pragma default_function_attributes = @ ".MY_FUNC"                 /* start placing following functions in section .MY_FUNC */
# pragma default_function_attributes =                                              /* stop placing functions in section*/

# pragma default_variable_attributes = @ ".MY_DATA"                  /* start placing following variables in section .MY_DATA */
# pragma default_variable_attributes =                                               /* stop placing functions in section*/

An example how to use these pragma directives is attached. <pragma_section_group_iar.c>

If I compile this *.c code I got some warnings that the GNU GCC does not support these statements.
After some research I found that the corresponding statement for GNU GCC is __attribute__((section (“.MY_FUNC”)))
I tried compiling it with these and it worked. <pragma_section_group_gcc.c>

I have more than 5k lines in my code and need to replace the IAR statements for the corresponding GCC Commands.
Is there a chance to group these statements like IAR supports or do I need to add __attribute__((section (“NAME”))) in each line

I look forward to hearing from you soon.

Best regards,













i.A.
Luca Werthmann
Trainee

Preh GmbH
Schweinfurter Straße 5-9| 97616Bad Neustadt a. d. Saale| Germany
Tel: +49 9771 92 3991
luca.werthmann@preh.de | www.preh.com

For legal and security reasons the information provided in this e-mail is not legally binding. Upon request we would be pleased to provide you with a legally binding confirmation in written form. Any form of unauthorised use, publication, reproduction, copying or disclosure of the content of this e-mail is not permitted. This message is exclusively for the person addressed or their representative. If you are not the intended recipient of this message and its contents, please notify the sender immediately. Preh GmbH; Vorsitzender des Aufsichtsrates: Jianfeng „Jeff“ Wang; Geschäftsführung: Zhengxin "Charlie" Cai (Vors.), Rui Marques Dias, Jochen Ehrenberg; Gesellschaft mit beschränkter Haftung mit Sitz in Bad Neustadt a. d. Saale; Registergericht Schweinfurt HRB 4491, UST-IdNr. DE 811147764


[-- Attachment #2: pragma_section_group_iar.c --]
[-- Type: text/plain, Size: 619 bytes --]

#pragma default_function_attributes = @ "MY_FUNC"

int fun1(int x)
{
    return x + 1;
}

int fun2(int x)
{
    return x - 1;
}

/* Stop placing functions in section MY_FUNC */
#pragma default_function_attributes =

int fun3(int x)
{
    return x + x;
}

/* Place following data in section MY_DATA */
#pragma default_variable_attributes = @ "MY_DATA"

int data1;
int data2;

/* Stop placing data in section MY_DATA */
#pragma default_variable_attributes =

int data3;

int main()
{
    data1 = fun1(5);
    data2 = fun2(5);
    data3 = fun3(5);

    return data1 + data2 + data3;
}

[-- Attachment #3: pragma_section_group_gcc.c --]
[-- Type: text/plain, Size: 481 bytes --]

int fun1(int x) __attribute__((section ("My_FUNC")));
int fun2(int x) __attribute__((section ("My_FUNC")));

int fun1(int x)
{
    return x + 1;
}

int fun2(int x)
{
    return x - 1;
}

int fun3(int x)
{
    return x + x;
}

int data1 __attribute__ ((section ("MY_DATA")));
int data2 __attribute__ ((section ("MY_DATA")));
int data3;

int main()
{
    data1 = fun1(5);
    data2 = fun2(5);
    data3 = fun3(5);

    return data1 + data2 + data3;
}


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

* Re: section placement - grouping __attribute__((section (“NAME”))) statements
  2021-08-20  8:22 section placement - grouping __attribute__((section (“NAME”))) statements Werthmann Luca
@ 2021-08-20 18:55 ` David Brown
  2021-08-20 21:02   ` Segher Boessenkool
  0 siblings, 1 reply; 4+ messages in thread
From: David Brown @ 2021-08-20 18:55 UTC (permalink / raw)
  To: Werthmann Luca, gcc-help

On 20/08/2021 10:22, Werthmann Luca via Gcc-help wrote:
> Hello,
> 
> I am writing to ask if there is a chance with the GNU GCC Compiler to group __attribute__((section (“NAME”))) statements.
> 
> IAR Supports for example the following commands:
> 
> # pragma default_function_attributes = @ ".MY_FUNC"                 /* start placing following functions in section .MY_FUNC */
> # pragma default_function_attributes =                                              /* stop placing functions in section*/
> 
> # pragma default_variable_attributes = @ ".MY_DATA"                  /* start placing following variables in section .MY_DATA */
> # pragma default_variable_attributes =                                               /* stop placing functions in section*/
> 
> An example how to use these pragma directives is attached. <pragma_section_group_iar.c>
> 
> If I compile this *.c code I got some warnings that the GNU GCC does not support these statements.
> After some research I found that the corresponding statement for GNU GCC is __attribute__((section (“.MY_FUNC”)))
> I tried compiling it with these and it worked. <pragma_section_group_gcc.c>
> 
> I have more than 5k lines in my code and need to replace the IAR statements for the corresponding GCC Commands.
> Is there a chance to group these statements like IAR supports or do I need to add __attribute__((section (“NAME”))) in each line
> 
> I look forward to hearing from you soon.
> 
> Best regards,
> 
 To the best of my knowledge, there is no way to do this in gcc - you
have to put the section attribute on each function.  (You can use a
macro to reduce the typing!)

If you are versed in linker files, it is possible to have a modified
linker setup to put the code or data from a specific file into
non-standard output sections.

I too would like to see this as a feature in gcc - it is one of the few
features that are common to most embedded compilers but missing from gcc.

David

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

* Re: section placement - grouping __attribute__((section (“NAME”))) statements
  2021-08-20 18:55 ` David Brown
@ 2021-08-20 21:02   ` Segher Boessenkool
  2021-08-21 18:56     ` David Brown
  0 siblings, 1 reply; 4+ messages in thread
From: Segher Boessenkool @ 2021-08-20 21:02 UTC (permalink / raw)
  To: David Brown; +Cc: Werthmann Luca, gcc-help

On Fri, Aug 20, 2021 at 08:55:54PM +0200, David Brown wrote:
> On 20/08/2021 10:22, Werthmann Luca via Gcc-help wrote:
> > I am writing to ask if there is a chance with the GNU GCC Compiler to group __attribute__((section (“NAME”))) statements.
> > 
> > IAR Supports for example the following commands:
> > 
> > # pragma default_function_attributes = @ ".MY_FUNC"                 /* start placing following functions in section .MY_FUNC */
> > # pragma default_function_attributes =                                              /* stop placing functions in section*/
> > 
> > # pragma default_variable_attributes = @ ".MY_DATA"                  /* start placing following variables in section .MY_DATA */
> > # pragma default_variable_attributes =                                               /* stop placing functions in section*/

>  To the best of my knowledge, there is no way to do this in gcc - you
> have to put the section attribute on each function.  (You can use a
> macro to reduce the typing!)

There are no such pragmas, correct.  And macros+attributes (as you
suggest) is a more flexible solution.  Is there some use case that would
make such pragmas super useful, overcoming all the obvious usability
downsides of it?

> If you are versed in linker files, it is possible to have a modified
> linker setup to put the code or data from a specific file into
> non-standard output sections.

Or you can play games with objcopy.

> I too would like to see this as a feature in gcc - it is one of the few
> features that are common to most embedded compilers but missing from gcc.

Do -ffunction-sections / -fdata-sections help your use case?


Segher

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

* Re: section placement - grouping __attribute__((section (“NAME”))) statements
  2021-08-20 21:02   ` Segher Boessenkool
@ 2021-08-21 18:56     ` David Brown
  0 siblings, 0 replies; 4+ messages in thread
From: David Brown @ 2021-08-21 18:56 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: Werthmann Luca, gcc-help



On 20/08/2021 23:02, Segher Boessenkool wrote:
> On Fri, Aug 20, 2021 at 08:55:54PM +0200, David Brown wrote:
>> On 20/08/2021 10:22, Werthmann Luca via Gcc-help wrote:
>>> I am writing to ask if there is a chance with the GNU GCC Compiler to group __attribute__((section (“NAME”))) statements.
>>>
>>> IAR Supports for example the following commands:
>>>
>>> # pragma default_function_attributes = @ ".MY_FUNC"                 /* start placing following functions in section .MY_FUNC */
>>> # pragma default_function_attributes =                                              /* stop placing functions in section*/
>>>
>>> # pragma default_variable_attributes = @ ".MY_DATA"                  /* start placing following variables in section .MY_DATA */
>>> # pragma default_variable_attributes =                                               /* stop placing functions in section*/
> 
>>  To the best of my knowledge, there is no way to do this in gcc - you
>> have to put the section attribute on each function.  (You can use a
>> macro to reduce the typing!)
> 
> There are no such pragmas, correct.  And macros+attributes (as you
> suggest) is a more flexible solution.  Is there some use case that would
> make such pragmas super useful, overcoming all the obvious usability
> downsides of it?

In embedded programming, it is not uncommon to have different areas of
memory that you need for different purposes.  For example, a
microcontroller is likely to have flash to use for holding the code.
But code that is used for erasing and re-programming the flash (for
in-system updates) cannot run from flash - it needs to be in ram.  So
you would have to mark all the functions involved as being in a section
that then gets linked to ram.

With gcc, you have to do that individually for all functions involved.
With other embedded compilers, you'd use a pragma - after all, these
functions are likely to be all within the same file.

In another case, such as the device I am using at the moment, the
processor (an ARM Cortex-M7) has "tightly coupled memories" as well as
main ram and flash.  Access to instructions and data in the TCM's is
significantly higher bandwidth, lower latency, and much lower jitter -
so you use it for critical tasks.  It is limited in size, so you can't
put /all/ the code in the instruction tightly-coupled memory.  But it is
not so limited that you only want a few functions there.  If there were
pragmas for function sections, it would be easy.  Individual function
attributes gets very tedious and ugly for so much code.  So I end up
using complicated linker setups.

In a perfect (for me) toolchain, you'd have pragmas for code and data
sections in addition to attributes.  These would affect the base name of
the section, which would still contain additional parts if
-ffunction-sections or -fdata-sections is used.  (These are very useful
if you are writing code that is more of a library, where the final
program will only use some of the functions - in small embedded systems
you don't use dynamic libraries at all.)

And I'd like to be able to have warnings (or errors) if code placed
within a specific section called functions (including library support
functions, such as software floating point routines, as well as normal
functions) that are outside that section.  See also
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88391>

I'd also like to have a "small data section", as is often found on
targets with lots of registers (like the PowerPC or Risc-V).  It would
not have a reserved base register, but the compiler would know that all
data smaller than the cut-off point (perhaps 16 bytes, configurable with
-msmall-data-limit) is within 16-bit offset from a base register.  And
it could be placed in small, fast memory while leaving big arrays and
buffers in the bigger, slower memories.


These kinds of features would be extremely useful to a lot of
small-system embedded programmers.  How feasible they would be to
implement, I don't know.  (I would expect pragmas for code and data
sections should be reasonable, at least.)

mvh.,

David



> 
>> If you are versed in linker files, it is possible to have a modified
>> linker setup to put the code or data from a specific file into
>> non-standard output sections.
> 
> Or you can play games with objcopy.
> 
>> I too would like to see this as a feature in gcc - it is one of the few
>> features that are common to most embedded compilers but missing from gcc.
> 
> Do -ffunction-sections / -fdata-sections help your use case?
> 
> 
> Segher
> 

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

end of thread, other threads:[~2021-08-21 18:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-20  8:22 section placement - grouping __attribute__((section (“NAME”))) statements Werthmann Luca
2021-08-20 18:55 ` David Brown
2021-08-20 21:02   ` Segher Boessenkool
2021-08-21 18:56     ` David Brown

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