3

To simulate a complete restart of an application (GNU C version 4.1.2 (arm-linux-gnueabi)) including static variable initialization I try to setjmp/longjmp to a gcc pre_init hook function on request. The longjmp in fact reaches the .preinit_array function but then on stepping out it crashes with SIGSEGV.

Any idea why this can go wrong? Should this generally possible? Are there alternative ways to roll an Linux application back to its initial state while being under the control of gdbserver?

Added Source:

static jmp_buf reset_simulation_jumpbuf __attribute__ ((section (".noinit")));
int preinit(int argc, char **argv, char **envp)
{
  // at this point no static variables shall be initialized
  setjmp(reset_simulation_jumpbuf);
}
// gcc standard linker scripts are calling this function before initialization
__attribute__((section(".preinit_array"))) typeof(preinit) *__preinit = preinit;

int main(void)
{
  // at this point all static variables are initialized
  do_something();
  if (reset)
    longjmp(reset_simulation_jumpbuf,1);
}
Community
  • 1
  • 1
Marcel
  • 137
  • 1
  • 8

1 Answers1

0

It's hard to be completely sure, but my best guess is that your jump point is before static variables have been initialized, but it's potentially after the C stack has been set up and probably segment registers and other ARM internals I'm less familiar with.

At this point, there are several potential issues that could be causing you problems. The pre-init code could be in a different segment than the previous code with different permissions, causing the SIGSEGV. The stack could be left in a state that is not compatible with the code being run (too much user data left on it, not enough room for some init structure), and allocating something crosses a segment boundary. Your processor init code may have protected some registers after this point in the code, but assumes it's still unprotected now, and your longjmp didn't unprotect it. And these are just a few off the top of my head.

In short, there are way too many ways to shoot yourself in the foot by trying to play games with the init code. It's not that your code isn't working, it's just doing something really unusual that the system can't cope with. I'd recommend coming up with a different way of forcing your software reset, perhaps by causing a software reset in your processor if it has that capability.

Erik Johnson
  • 1,136
  • 6
  • 17