Bug 50807 - [avr] Constructor writing to RAM for variable in Flash
Summary: [avr] Constructor writing to RAM for variable in Flash
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.7.0
: P5 minor
Target Milestone: 4.9.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2011-10-20 10:12 UTC by Georg-Johann Lay
Modified: 2014-09-21 14:36 UTC (History)
2 users (show)

See Also:
Host:
Target: avr
Build:
Known to work: 4.9.2
Known to fail: 4.6.2, 4.7.0
Last reconfirmed: 2011-10-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Georg-Johann Lay 2011-10-20 10:12:21 UTC
The following line of code

const char __attribute__((progmem)) var = "Hallo"[0];

compileds with

avr-g++ progmem.c -S -Os

compiles code that tries to initialize var at run time:

	.section	.text.startup,"ax",@progbits
	.type	_GLOBAL__sub_I_progmem.c, @function
_GLOBAL__sub_I_progmem.c:
	ldi r24,lo8(72)
	sts _ZL3var,r24
	ret
	.size	_GLOBAL__sub_I_progmem.c, .-_GLOBAL__sub_I_progmem.c

	.global __do_global_ctors
	.section .ctors,"a",@progbits
	.word	gs(_GLOBAL__sub_I_progmem.c)
	.local	_ZL3var
	.comm	_ZL3var,1,1

As var is located in flash and thus cannot be initialized at runtime, there should be an error message like
"progmem variable var cannot be initialized at load time".
Comment 1 Georg-Johann Lay 2011-10-20 10:16:37 UTC
Confirmed with 4.6.2

The generated code is a bit different.
var is put into the correct section .progmem but there is no error and the constructor will write to RAM:

	.section	.text.startup,"ax",@progbits
	.type	_GLOBAL__sub_I_progmem.c, @function
_GLOBAL__sub_I_progmem.c:
	ldi r24,lo8(72)
	sts _ZL3var,r24
	ret
	.size	_GLOBAL__sub_I_progmem.c, .-_GLOBAL__sub_I_progmem.c

	.global __do_global_ctors
	.section .ctors,"a",@progbits
	.word	gs(_GLOBAL__sub_I_progmem.c)
	.section	.progmem.data,"a",@progbits
	.type	_ZL3var, @object
	.size	_ZL3var, 1
_ZL3var:
	.skip 1,0
Comment 2 Jakub Jelinek 2011-10-26 17:13:59 UTC
GCC 4.6.2 is being released.
Comment 3 Jakub Jelinek 2012-03-01 14:39:18 UTC
GCC 4.6.3 is being released.
Comment 4 Jakub Jelinek 2013-04-12 16:16:16 UTC
Is this reproduceable with 4.7.3, 4.8.0 or the trunk?
Comment 5 Georg-Johann Lay 2013-06-11 11:43:32 UTC
(In reply to Jakub Jelinek from comment #4)
> Is this reproduceable with 4.7.3, 4.8.0 or the trunk?

With the test case from above and 4.8.0, no code is generated at all.

With an extended test case as follows, the constructor still writes to RAM in insn 6:

const char __attribute__((progmem)) var = "Hallo"[0];

const char *foo (void)
{
    return &var;
}

_GLOBAL__sub_I__Z3foov:
	ldi r24,lo8(72)	 ;  5	movqi_insn/2	[length = 1]
	sts _ZL3var,r24	 ;  6	movqi_insn/3	[length = 2]
	ret	 ;  13	return	[length = 1]

And there is a warning from the back end:

foo.c: In function 'const char* foo()':
foo.c:1:37: warning: uninitialized variable 'var' put into program memory area [-Wuninitialized]
 const char __attribute__((progmem)) var = "Hallo"[0];
Comment 6 Georg-Johann Lay 2014-09-21 14:36:25 UTC
Works in 4.9.