avr-size .bss bigger than RAM?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

avr-size .bss bigger than RAM?

Stuart Longland
Hi all,

Just a curiosity I noticed with some code I was toying with tonight,
compiled for an ATTiny861:

> $ avr-size bin/attiny861/synth
>    text   data    bss    dec    hex filename
>    3216      6    535   3757    ead bin/attiny861/synth

The code is at http://github.com/sjlongland/atinysynth/ -- build with
`make PORT=attiny861`.

The code runs fine, as can be seen demonstrated in the video on this page:

https://hackaday.io/project/24932/log/63089-building-synth-2

If I am reading the output from size correctly, then .bss is overflowing
SRAM by about 23 bytes.  How does that work?  Or am I misreading the
information?
--
Stuart Longland (aka Redhatter, VK4MSL)

I haven't lost my mind...
  ...it's backed up on a tape somewhere.


_______________________________________________
AVR-chat mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-chat

signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: avr-size .bss bigger than RAM?

Rolf Pfister-2
Am 25.07.2017 um 11:24 schrieb Stuart Longland:
> If I am reading the output from size correctly, then .bss is overflowing
> SRAM by about 23 bytes.  How does that work?  Or am I misreading the
> information?
>

I would say it uses more than 29 bytes too much. Because data also
needs SRAM. And subroutine calling and interrupts will also need some
bytes of SRAM for the stack.
Maybe you didn't see something going wrong yet because it didn't fill
all reserved data structures yet.

Rolf


_______________________________________________
AVR-chat mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-chat
Reply | Threaded
Open this post in threaded view
|

Re: avr-size .bss bigger than RAM?

Stuart Longland
On 25/07/17 22:36, Rolf Pfister wrote:
> I would say it uses more than 29 bytes too much. Because data also
> needs SRAM. And subroutine calling and interrupts will also need some
> bytes of SRAM for the stack.
> Maybe you didn't see something going wrong yet because it didn't fill
> all reserved data structures yet.

Right… I was a bit surprised that it would allow such a program to link.
 arm-ld complains when the data structures won't fit into the spaces
allocated by the linker script.

I did another check, and found one of my structures occupied the full
512 bytes, so I've cut it back, .bss is now 279 bytes.

Often the code will define a big integer array somewhere and call that
the stack, so you know exactly where it is and how big it is.  AVR
though, all that is abstracted.

Anyone know where I can find out how much RAM is allocated for stack on AVR?
--
Stuart Longland (aka Redhatter, VK4MSL)

I haven't lost my mind...
  ...it's backed up on a tape somewhere.


_______________________________________________
AVR-chat mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-chat

signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: avr-size .bss bigger than RAM?

Matt.VanDeWerken
I'm not sure that you can determine how big the stack is (nor can you constrain it). In the AVR, the stack grows unconstrained downwards from the top of RAM. See here: http://www.nongnu.org/avr-libc/user-manual/malloc.html for an explanation of the memory sections in the AVR.

If you're running an RTOS you can often allocate/specify where the stacks for each process reside, and how big they are, but you can't actually constrain them to always fit within that space so you have to be careful to allocate enough space for them.



-----Original Message-----
From: AVR-chat [mailto:avr-chat-bounces+matt.vandewerken=[hidden email]] On Behalf Of Stuart Longland
Sent: Wednesday, 26 July 2017 8:11 AM
To: [hidden email]
Subject: Re: [avr-chat] avr-size .bss bigger than RAM?

On 25/07/17 22:36, Rolf Pfister wrote:
> I would say it uses more than 29 bytes too much. Because data also
> needs SRAM. And subroutine calling and interrupts will also need some
> bytes of SRAM for the stack.
> Maybe you didn't see something going wrong yet because it didn't fill
> all reserved data structures yet.

Right… I was a bit surprised that it would allow such a program to link.
 arm-ld complains when the data structures won't fit into the spaces allocated by the linker script.

I did another check, and found one of my structures occupied the full
512 bytes, so I've cut it back, .bss is now 279 bytes.

Often the code will define a big integer array somewhere and call that the stack, so you know exactly where it is and how big it is.  AVR though, all that is abstracted.

Anyone know where I can find out how much RAM is allocated for stack on AVR?
--
Stuart Longland (aka Redhatter, VK4MSL)

I haven't lost my mind...
  ...it's backed up on a tape somewhere.

_______________________________________________
AVR-chat mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-chat
Reply | Threaded
Open this post in threaded view
|

Re: avr-size .bss bigger than RAM?

Stuart Longland
On 26/07/17 08:43, [hidden email] wrote:
> I'm not sure that you can determine how big the stack is (nor can you constrain it). In the AVR, the stack grows unconstrained downwards from the top of RAM. See here: http://www.nongnu.org/avr-libc/user-manual/malloc.html for an explanation of the memory sections in the AVR.
>
> If you're running an RTOS you can often allocate/specify where the stacks for each process reside, and how big they are, but you can't actually constrain them to always fit within that space so you have to be careful to allocate enough space for them.

Agreed, you can't constrain it directly, but you can estimate how big
it'll get by the depth of subroutine calls and ISRs, then reserve that
portion of RAM so that stack and application state do not meet.

In my case, I'm not running an RTOS due to the severe memory
restrictions present in the ATTiny861.
--
Stuart Longland (aka Redhatter, VK4MSL)

I haven't lost my mind...
  ...it's backed up on a tape somewhere.


_______________________________________________
AVR-chat mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-chat

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: avr-size .bss bigger than RAM?

Erik Christiansen-2
In reply to this post by Stuart Longland
On 25.07.17 19:24, Stuart Longland wrote:
> > $ avr-size bin/attiny861/synth
> >    text   data    bss    dec    hex filename
> >    3216      6    535   3757    ead bin/attiny861/synth

On 26.07.17 08:10, Stuart Longland wrote:

> On 25/07/17 22:36, Rolf Pfister wrote:
> > I would say it uses more than 29 bytes too much. Because data also
> > needs SRAM. And subroutine calling and interrupts will also need some
> > bytes of SRAM for the stack.
> > Maybe you didn't see something going wrong yet because it didn't fill
> > all reserved data structures yet.
>
> Right… I was a bit surprised that it would allow such a program to link.
>  arm-ld complains when the data structures won't fit into the spaces
> allocated by the linker script.

Yes, the gnu linker does warn on memory region overflow, so it is likely
that the linker script you're using is one of those which were made so
generic that this protection has been disabled. Your version may differ,
but here:

$ avr-gcc -mmcu=attiny861 -Wl,--verbose | more
...
MEMORY
{
  text   (rx)   : ORIGIN = 0, LENGTH = 8K
  data   (rw!x) : ORIGIN = 0x800060, LENGTH = 0xffa0
  eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 64K
  fuse      (rw!x) : ORIGIN = 0x820000, LENGTH = 1K
  lock      (rw!x) : ORIGIN = 0x830000, LENGTH = 1K
  signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K
}

Your data + bss has no chance of overflowing 0xffa0 (65440) bytes.
Admittedly, my version is perhaps a bit dated:

$ avr-ld --version
GNU ld (GNU Binutils) 2.20.1.20100303

There is nothing to stop you taking a copy of the linker script, and
correcting the size limit on the data region, to restore the protection.

> I did another check, and found one of my structures occupied the full
> 512 bytes, so I've cut it back, .bss is now 279 bytes.
>
> Often the code will define a big integer array somewhere and call that
> the stack, so you know exactly where it is and how big it is.  AVR
> though, all that is abstracted.
>
> Anyone know where I can find out how much RAM is allocated for stack on AVR?

If you have a look at the prologue to main, in a list file for your
program, created with an avr-gcc option along the lines of:
-Wa,-ahdl=xx.lst
then the top of stack may be found where it is loaded into SP. What is
available is the gap between that and what you have used.

Erik

_______________________________________________
AVR-chat mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-chat