Re: [patch -v2] flat: fix data sections alignment

From: Oskar Schirmer
Date: Thu Mar 05 2009 - 11:43:11 EST


On Wed, Mar 04, 2009 at 16:48:04 -0500, Mike Frysinger wrote:
> On Wed, Mar 4, 2009 at 08:51, Johannes Weiner wrote:
> > - Â Â Â sp = (unsigned long *) ((-(unsigned long)sizeof(char *))&(unsigned long) p);
> > -
> > - Â Â Â sp -= envc+1;
> > - Â Â Â envp = sp;
> > - Â Â Â sp -= argc+1;
> > - Â Â Â argv = sp;
> > + Â Â Â sp = (unsigned long *)p;
> > + Â Â Â sp -= (envc + argc + 2) + 1 + (flat_argvp_envp_on_stack() ? 2 : 0);
> > + Â Â Â sp = (unsigned long *) ((unsigned long)sp & -FLAT_DATA_ALIGN);
> > + Â Â Â argv = sp + 1 + (flat_argvp_envp_on_stack() ? 2 : 0);
> > + Â Â Â envp = argv + (argc + 1);
>
> can this be cleaned up a bit so that the argv/envp assignment happens
> by using sp before aligning sp ? that would be defensive coding wrt
> preventing sp adjustment falling out of line with argv initialization,
> and cut down on duplicated code.

The stack grows down and needs to be aligned when done,
i.e. where it's user space's turn. Therefor, we need to
first calculate the amount of space we need for argv/envp,
then align the result, and finally push argv/envp backward
into the reserved space. Note, that all this was done
before too, with one difference: Alignment was requested
in the middle of the calculation, which is nonsense (as
the comment in the ARM flat.h prooved).

> > @@ -854,7 +861,7 @@ static int load_flat_binary(struct linux
> > Â Â Â Âstack_len = TOP_OF_ARGS - bprm->p; Â Â Â Â Â Â /* the strings */
> > Â Â Â Âstack_len += (bprm->argc + 1) * sizeof(char *); /* the argv array */
> > Â Â Â Âstack_len += (bprm->envc + 1) * sizeof(char *); /* the envp array */
> > -
> > + Â Â Â stack_len += FLAT_DATA_ALIGN;
>
> this seems weird. alignment is for aligning data, not padding it out
> some value ...

stack_len is the minimum amount of space to reserve
for the stack later on. As the stack pointer will be
aligned after pushing argv/envp (see above), we need
to reserve the additional space for maximum possible
alignment upon allocation.

Oskar
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/