Re: Pb when calling a nested function in the inferior

Kevin Buettner
2001-07-30T15:49:00Z

On Jul 30, 11:24pm, Joel Brobecker wrote:

> > > I've personally never used (or even seen) this
> > > nested function syntax, so I don't know anything
> > > useful about it. -- Michael
>
> Yes, I hesitated a bit before posting this example, knowing that it was
> a gcc extension. However, I thought it would make an easier to undertand
> example rather than posting Ada95 code.

Have you verified that the mechanisms used by gcc and by Ada95 for
passing the static chain are the same?

> > It's a gcc extension. Apparently, on i386, a pointer to the static
> > chain is passed in ecx. If Joel wants to fix this problem, it'd be a
> > good idea to see if the relevant ABI addresses this issue and then
> > make the appropriate changes. (The trick, I think, is to figure out
> > the correct value to load into ecx.)
>
> OK, I'll have a look and see what can be done. But before doing
> anything, is there a way for gdb to detect that the function it is about
> to call is nested? I think that, as a first step, having gdb diagnose
> such cases and report a warning or an error would be an improvement.
> Right now, it gives either an incorrect value or even crashes the
> inferior.

A couple of ideas come to mind:

1) In the version of gcc that I'm using the symbol that's associated
with get_value() is get_value.0. You could look for such symbols
and refuse to allow them to be called as inferior functions.

2) You could scan the prologue and look for a sequence of instructions
which looks like a save of the static chain. E.g, in your example,
I see:

0x804842c <get_value.0>: push %ebp
0x804842d <get_value.0+1>: mov %esp,%ebp
0x804842f <get_value.0+3>: sub $0x4,%esp
0x8048432 <get_value.0+6>: mov %ecx,0xfffffffc(%ebp)
0x8048435 <get_value.0+9>: mov 0xfffffffc(%ebp),%ecx
0x8048438 <get_value.0+12>: mov %ecx,%ecx
0x804843a <get_value.0+14>: mov 0xfffffffc(%ecx),%eax
0x804843d <get_value.0+17>: mov %eax,%eax
0x804843f <get_value.0+19>: leave
0x8048440 <get_value.0+20>: ret

It appears to me that ``mov %ecx,0xfffffffc(%ebp)'' is
responsible for saving the static chain pointer. If you could
detect this, you could print your error or warning. (You'd
want to make sure that no other instruction with a destination
of %ecx appears before this instruction in the prologue though;
if it does, it means it's doing something else.)

BTW, GDB isn't particularly graceful in its handling of the ``get_value.0''
symbol. E.g, observe what happens when I do ``x/i get_value.0'':

(gdb) x/i get_value.0
No symbol "get_value" in current context.

Kevin