Bug 37506 - attribute section is not working with constant strings
Summary: attribute section is not working with constant strings
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.3.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-09-13 09:04 UTC by Márton Németh
Modified: 2008-09-14 07:14 UTC (History)
2 users (show)

See Also:
Host:
Target: i486-linux-gnu
Build: 4.3.1
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Márton Németh 2008-09-13 09:04:30 UTC
With the __attribute__ __section__ it is possible to allocate variables to different section than the default ( http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html ). In case of the following source code I would achieve to allocate both "foo bar" and "Hello World!" strings into the .xxx_section section:

char x[] __attribute__ ((__section__(".xxx_section"))) = "foo bar";
char* y __attribute__ ((__section__(".xxx_section"))) = "Hello World!";

int main() {
        return 0;
}

However, with gcc version 4.3.1 (Debian 4.3.1-9) only the "foo bar" string is stored in .xxx_section:

$ objdump -s a.out

a.out:     file format elf32-i386
[...]
Contents of section .rodata:
 8048448 03000000 01000200 48656c6c 6f20576f  ........Hello Wo
 8048458 726c6421 00                          rld!.           
[...]
Contents of section .xxx_section:
 8049568 666f6f20 62617200 50840408           foo bar.P...
Comment 1 pinskia@gmail.com 2008-09-13 09:49:44 UTC
Subject: Re:   New: attribute section is not working with constant strings



Sent from my iPhone

On Sep 13, 2008, at 2:04 AM, "nm127 at freemail dot hu" <gcc-bugzilla@gcc.gnu.org 
 > wrote:

> With the __attribute__ __section__ it is possible to allocate  
> variables to
> different section than the default (
> http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html ). In  
> case of the
> following source code I would achieve to allocate both "foo bar" and  
> "Hello
> World!" strings into the .xxx_section section:
>
> char x[] __attribute__ ((__section__(".xxx_section"))) = "foo bar";
> char* y __attribute__ ((__section__(".xxx_section"))) = "Hello  
> World!";
>
> int main() {
>        return 0;
> }
>
> However, with gcc version 4.3.1 (Debian 4.3.1-9) only the "foo bar"  
> string is
> stored in .xxx_section:
>
> $ objdump -s a.out
>
> a.out:     file format elf32-i386
> [...]
> Contents of section .rodata:
> 8048448 03000000 01000200 48656c6c 6f20576f  ........Hello Wo
> 8048458 726c6421 00                          rld!.
> [...]
> Contents of section .xxx_section:
> 8049568 666f6f20 62617200 50840408           foo bar.P...
>
>
> -- 
>           Summary: attribute section is not working with constant  
> strings
>           Product: gcc
>           Version: unknown
>            Status: UNCONFIRMED
>          Severity: normal
>          Priority: P3
>         Component: c
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: nm127 at freemail dot hu
> GCC build triplet: 4.3.1
> GCC target triplet: i486-linux-gnu
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37506
>

Comment 2 Márton Németh 2008-09-13 13:59:06 UTC
This problem maybe related to bug #192.
Comment 3 Andrew Pinski 2008-09-14 04:45:04 UTC
char* y __attribute__ ((__section__(".xxx_section"))) = "Hello World!";

That only puts the pointer variable y into that section and not the string.  The correct way is do like what you do for x.
Comment 4 Márton Németh 2008-09-14 05:32:14 UTC
(In reply to comment #3)
> char* y __attribute__ ((__section__(".xxx_section"))) = "Hello World!";
> 
> That only puts the pointer variable y into that section and not the string. 

Exatly that is my problem. So the pointer variable y goes to section .xxx_section, but the actual string not. And I do not know any possibility to specify that the "Hello World!" string should also go to .xxx_section.

For example I expect that in case of the following two lines the "Hello World!" string itself should be stored in section .xxx_section:

char* z = __attribute__ ((__section__(".xxx_section"))) "Hello World!";
char* w = "Hello World!" __attribute__ ((__section__(".xxx_section")));

What I get for both lines is an error message:

$ gcc -Wall test.c  
test.c:5: error: expected expression before '__attribute__'
test.c:6: error: expected ',' or ';' before '__attribute__'

Also, the problem gets complicated when a complete structure is initialized and it has some string fields:

typedef struct {
        int x;
        int y;
        char* str;
} s;

s a __attribute__ ((__section__(".xxx_section"))) = {
        0x11223344,
        0x55667788,
        "string of the initialized struct"
};

int main() {
        return 0;
}

Now we get the following sections:

$ objdump -s a.out

a.out:     file format elf32-i386

[...]
Contents of section .rodata:
 8048448 03000000 01000200 73747269 6e67206f  ........string o
 8048458 66207468 6520696e 69746961 6c697a65  f the initialize
 8048468 64207374 72756374 00                 d struct.       
[...]
Contents of section .xxx_section:
 804957c 44332211 88776655 50840408           D3"..wfUP...    

My expectation is that if I specify that the structure should be stored in .xxx_section than the string itself should also be stored in .xxx_section.
Comment 5 Andrew Pinski 2008-09-14 05:36:13 UTC
This is all expected, if you want a string constant to be in a different section, you need to put there your self by using a variable.
Comment 6 Márton Németh 2008-09-14 07:14:04 UTC
(In reply to comment #5)
> This is all expected, if you want a string constant to be in a different
> section, you need to put there your self by using a variable.

So the __attribute__ __section__ modifier is not recursively applied to all child element?

If the __attribute__ __section__ is not recursive shouldn't it be possible to specify explicitly this modifier for the child elements something like "z" and "w"  in comment #4?
Comment 7 brian 2008-09-14 07:54:45 UTC
Subject: Re:  attribute section is not working with constant strings


If you want a struct containing a pointer to a string in a specified
section, then:

char str[] __attribute__ ((__section__(".xxx_section"))) = "foo";

struct foo {
  char *p
} var = { str };