User account creation filtered due to spam.

Bug 39456 - Functions/variables of a file in different named sections
Summary: Functions/variables of a file in different named sections
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 40113 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-03-13 14:03 UTC by etienne_lorrain
Modified: 2016-12-15 12:03 UTC (History)
2 users (show)

See Also:
Host: all-all-all
Target: all-all-all
Build: all-all-all
Known to work:
Known to fail:
Last reconfirmed: 2009-04-16 20:51:34


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description etienne_lorrain 2009-03-13 14:03:38 UTC
Just that I need to set each functions in a C file into different sections, with section names I can link in different segments.
 That can be done with:
__attribute__ ((section ("sect1_fct1"))) void fct1 (void) {}
__attribute__ ((section ("sect1_fct2"))) void fct2 (void) {}
 But cannot unfortunately be done in a macro like:
__attribute__ ((section ("sect1_" __FUNCTION__ ))) void fct1 (void) {}

 It would be nice to have some way to do that, like:
__attribute__ ((section_prepend ("sect1_"))) void fct1 (void) {}
 so that a bad cut&paste somewhere do not create problems because
 a function is in the wrong section (by forgeting to change the
 concat'ed string).
Comment 1 etienne_lorrain 2009-03-19 16:33:24 UTC
 Also, you cannot put function in another section and then
 use -ffunction-sections, i.e.:

etienne@gujin:~$ gcc --version
gcc (Debian 4.3.3-3) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

etienne@gujin:~$ cat tmp.c
__attribute__((section(".extracode"))) int fct1 (void) {
        return 1;
}
__attribute__((section(".extracode"))) int fct2 (void) {
        return 2;
}

etienne@gujin:~$ gcc -Os -fomit-frame-pointer -ffunction-sections -S tmp.c -o tmp.s
etienne@gujin:~$ cat tmp.s
        .file   "tmp.c"
        .section        .extracode,"ax",@progbits
.globl fct1
        .type   fct1, @function
fct1:
        movl    $1, %eax
        ret
        .size   fct1, .-fct1
.globl fct2
        .type   fct2, @function
fct2:
        movl    $2, %eax
        ret
        .size   fct2, .-fct2
        .ident  "GCC: (Debian 4.3.3-3) 4.3.3"
        .section        .note.GNU-stack,"",@progbits
etienne@gujin:~$

 So you cannot use garbage collection in the linker...
Comment 2 Andrew Pinski 2009-05-12 14:51:30 UTC
*** Bug 40113 has been marked as a duplicate of this bug. ***
Comment 3 Andrew Pinski 2012-01-02 04:25:03 UTC
What about doing something like:
#define function_def(base_section, funname, RET, ARGS) __attribute__((__section__(base_section ##funname))) RET funname ARGS

#define basesect "sect1_"

function_def(basesect, fct1, void, (void));

So you won't have a copy and paste error at all and it works without any real changes to the compiler.
Comment 4 etienne_lorrain 2012-01-03 09:53:59 UTC
Thanks for looking at my reports and happy new year!

I think the problem I had was the quoting and string concat, you need:
void fct2 (void) __attribute__((__section__("sect1_fct2"))) { return; }

The solution you give generates:
void fct1 (void) __attribute__((__section__(basesectfct1))) { return; }

You cannot give the compiler:
void fct2 (void) __attribute__((__section__("sect1_" "fct2"))) { return; }
because it would not concat the strings in this case (I did not re-check).

And:
#define function_def(base_section, funname, RET, ARGS) \
RET funname ARGS __attribute__((__section__(# base_section##funname)))
#define basesect sect1_
function_def(basesect, fct1, void, (void)) { return; }
will stringify #base_section before applying the ##

That was my problem...
Comment 5 etienne_lorrain 2016-12-15 12:03:08 UTC
Fixed (string concat into __attribute__((section("pre" "fix"))) ) on  g++ (GCC-Explorer-Build) 7.0.0 20161113 (experimental), result of:

void fct2 (void) __attribute__((__section__("sect1_" "fct2")));
void fct2 (void) { return; }

is:

        .section        sect1_fct2,"ax",@progbits
        .p2align 4,,15
        .globl  fct2()
        .type   fct2(), @function
fct2():
.LFB0:
        .file 1 "/tmp/gcc-explorer-compiler1161115-73-1xzfvi6/example.cpp"
        .loc 1 2 0
        .cfi_startproc
        .loc 1 2 0
        rep ret