Static variable getting overwritten by?

OP #1174108
I'm using two open source modules in a project, an MP3 decoder and a
TCP/IP stack.  Long story short, the TCP/IP stack contains a "static
struct" that holds the ARP table, and parts of it are being overwritten
when the MP3 decoder gets its chance to run.

I'm at a loss because I can't understand how the compiler would allow
this to happen.  There is a line in the .map file that lists the struct
that is getting overwritten:

.bss.arp_table
                0x20000ea0       0x60 ./uip-1.0/uip/uip_arp.o

Wouldn't this indicate that this section of RAM is set aside for
arp_table and won't be freed during execution?  The problem occurs
everytime, very reliably.  It's like variables in the MP3 decoder are
assigned to the same RAM address.

Any advice on tracking down this problem?  This is running on a
Cortex-M3, by the way.
OP
Additional information about this problem:

I have confirmed that when entering a function, the data passed into the
function, or variables local to the function are being allocated in this
section of RAM that has already been allocated to a static struct.  What
on earth could cause this problem??  I'm using CodeSourcery G++ Lite.
OP
Giovanni Di Sirio wrote:
> A stack overflow ?
>
> It would be a good idea to post the map file and the linker script.

This problem started occurring when I added a function that would make
heavy use of the stack, so that might be my problem.  As I was looking
through source files, I came across the stack allocation, and it's only
256 bytes; probably not enough.   I used a sample project to get started
so I don't fully understand how this sets the stack.

#ifndef STACK_SIZE
#define STACK_SIZE                              64
#endif
static unsigned long pulStack[STACK_SIZE];


It's obvious this makes the stack 64 32-bit words long, but the only
other instance of pulStack is where the vector table is set up.  How
exactly does the linker know to use this space for stack?  If I remember
correctly, the stack is populated from the top, down?  If this is the
case, this makes sense because the struct that is being corrupted is
very close to the bottom of the stack.  Is there a good way to determine
the required stack size, or is it not an exact science?
I cannot answer, it depends on how the startup file and the linker
scripts are done and the stack you are running on (process or
exception).

The linker does its work by executing a linker script, usually it has a
.ld extension but may change, you should have one in your project, it is
specified in the linker command line.

Anyway, just try to increase that value and see if something change.
OP
Giovanni Di Sirio wrote:
> I cannot answer, it depends on how the startup file and the linker
> scripts are done and the stack you are running on (process or
> exception).
>
> The linker does its work by executing a linker script, usually it has a
> .ld extension but may change, you should have one in your project, it is
> specified in the linker command line.
>
> Anyway, just try to increase that value and see if something change.

Thanks for your suggestion.  That fixed the problem.

Reply

A question of your own? This thread is over 18 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.