mailing list of musl libc
 help / color / mirror / code / Atom feed
* request for help with aux
@ 2016-06-27 20:07 Daniel Wilkerson
  2016-06-27 21:00 ` Bobby Bingham
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Daniel Wilkerson @ 2016-06-27 20:07 UTC (permalink / raw)
  To: musl; +Cc: Mark Winterrowd

The musl crt0 code seems to expect that when the process starts that
the stack pointer points to a data page having the following four data
structures immediately contiguous: argc, argv, env, aux.

I'm writing a loader for musl-riscv and I need to know how to
initialize this data.  The only one I wonder about is the aux array.
It seems that aux is a collection of name/value pairs which are used
as follows in musl-riscv/src/env/__libc_start_main.c:

    void __init_libc(char **envp, char *pn)
    {
            size_t i, *auxv, aux[AUX_CNT] = { 0 };
            __environ = envp;
            for (i=0; envp[i]; i++);
            libc.auxv = auxv = (void *)(envp+i+1);
            for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT) aux[auxv[i]] = auxv[i\
+1];
            __hwcap = aux[AT_HWCAP];
            __sysinfo = aux[AT_SYSINFO];
            libc.page_size = aux[AT_PAGESZ];

            if (pn) {
                    __progname = __progname_full = pn;
                    for (i=0; pn[i]; i++) if (pn[i]=='/') __progname = pn+i+1;
            }

            __init_tls(aux);
            __init_ssp((void *)aux[AT_RANDOM]);

            if (aux[AT_UID]==aux[AT_EUID] && aux[AT_GID]==aux[AT_EGID]
                    && !aux[AT_SECURE]) return;

            struct pollfd pfd[3] = { {.fd=0}, {.fd=1}, {.fd=2} };
    #ifdef SYS_poll
            __syscall(SYS_poll, pfd, 3, 0);
    #else
            __syscall(SYS_ppoll, pfd, 3, &(struct timespec){0}, 0, _NSIG/8);
    #endif
            for (i=0; i<3; i++) if (pfd[i].revents&POLLNVAL)
                    if (__sys_open("/dev/null", O_RDWR)<0)
                            a_crash();
            libc.secure = 1;
    }

This seems to initalize aux to be all zeros, so it seems that in
theory all of the aux values could be optional:

        size_t i, *auxv, aux[AUX_CNT] = { 0 };

What I'm wondering is where to find the semantics of all of the aux
names; I could hunt through all of the code, but any high-level
suggestions you could provide could help a lot.  As a bonus, which
ones might not have sensible defaults and are actually non-optional,
if any.

Daniel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: request for help with aux
  2016-06-27 20:07 request for help with aux Daniel Wilkerson
@ 2016-06-27 21:00 ` Bobby Bingham
  2016-06-27 21:33   ` Daniel Wilkerson
  2016-06-27 21:58 ` Rich Felker
  2016-06-28  0:22 ` Patrick Oppenlander
  2 siblings, 1 reply; 6+ messages in thread
From: Bobby Bingham @ 2016-06-27 21:00 UTC (permalink / raw)
  To: musl

On Mon, Jun 27, 2016 at 01:07:59PM -0700, Daniel Wilkerson wrote:
> This seems to initalize aux to be all zeros, so it seems that in
> theory all of the aux values could be optional:
>
>         size_t i, *auxv, aux[AUX_CNT] = { 0 };
>
> What I'm wondering is where to find the semantics of all of the aux
> names; I could hunt through all of the code, but any high-level
> suggestions you could provide could help a lot.  As a bonus, which
> ones might not have sensible defaults and are actually non-optional,
> if any.

The getauxval man page is a good starting point:
http://man7.org/linux/man-pages/man3/getauxval.3.html

--
Bobby


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: request for help with aux
  2016-06-27 21:00 ` Bobby Bingham
@ 2016-06-27 21:33   ` Daniel Wilkerson
  2016-06-27 21:49     ` Nathan McSween
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Wilkerson @ 2016-06-27 21:33 UTC (permalink / raw)
  To: musl; +Cc: Mark Winterrowd

Ah, thank you.  From that page:

       This function is a nonstandard glibc extension.

And:

       The primary consumer of the information in the auxiliary vector is
       the dynamic linker ld-linux.so(8).  The auxiliary vector is a
       convenient and efficient shortcut that allows the kernel to
       communicate a certain set of standard information that the dynamic
       linker usually or always needs.  In some cases, the same information
       could be obtained by system calls, but using the auxiliary vector is
       cheaper.

It seems therefore that if I am doing static linking that it is safe
to simply provide an empty aux vector?

Daniel

On Mon, Jun 27, 2016 at 2:00 PM, Bobby Bingham <koorogi@koorogi.info> wrote:
> On Mon, Jun 27, 2016 at 01:07:59PM -0700, Daniel Wilkerson wrote:
>> This seems to initalize aux to be all zeros, so it seems that in
>> theory all of the aux values could be optional:
>>
>>         size_t i, *auxv, aux[AUX_CNT] = { 0 };
>>
>> What I'm wondering is where to find the semantics of all of the aux
>> names; I could hunt through all of the code, but any high-level
>> suggestions you could provide could help a lot.  As a bonus, which
>> ones might not have sensible defaults and are actually non-optional,
>> if any.
>
> The getauxval man page is a good starting point:
> http://man7.org/linux/man-pages/man3/getauxval.3.html
>
> --
> Bobby


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: request for help with aux
  2016-06-27 21:33   ` Daniel Wilkerson
@ 2016-06-27 21:49     ` Nathan McSween
  0 siblings, 0 replies; 6+ messages in thread
From: Nathan McSween @ 2016-06-27 21:49 UTC (permalink / raw)
  To: musl; +Cc: Mark Winterrowd

[-- Attachment #1: Type: text/plain, Size: 1698 bytes --]

Aux vec is used in things like SSP, so disabling aux would use a guessable
value for SSP, iirc static linking still uses the aux vec for SSP

On Mon, Jun 27, 2016, 2:34 PM Daniel Wilkerson <daniel.wilkerson@gmail.com>
wrote:

> Ah, thank you.  From that page:
>
>        This function is a nonstandard glibc extension.
>
> And:
>
>        The primary consumer of the information in the auxiliary vector is
>        the dynamic linker ld-linux.so(8).  The auxiliary vector is a
>        convenient and efficient shortcut that allows the kernel to
>        communicate a certain set of standard information that the dynamic
>        linker usually or always needs.  In some cases, the same information
>        could be obtained by system calls, but using the auxiliary vector is
>        cheaper.
>
> It seems therefore that if I am doing static linking that it is safe
> to simply provide an empty aux vector?
>
> Daniel
>
> On Mon, Jun 27, 2016 at 2:00 PM, Bobby Bingham <koorogi@koorogi.info>
> wrote:
> > On Mon, Jun 27, 2016 at 01:07:59PM -0700, Daniel Wilkerson wrote:
> >> This seems to initalize aux to be all zeros, so it seems that in
> >> theory all of the aux values could be optional:
> >>
> >>         size_t i, *auxv, aux[AUX_CNT] = { 0 };
> >>
> >> What I'm wondering is where to find the semantics of all of the aux
> >> names; I could hunt through all of the code, but any high-level
> >> suggestions you could provide could help a lot.  As a bonus, which
> >> ones might not have sensible defaults and are actually non-optional,
> >> if any.
> >
> > The getauxval man page is a good starting point:
> > http://man7.org/linux/man-pages/man3/getauxval.3.html
> >
> > --
> > Bobby
>

[-- Attachment #2: Type: text/html, Size: 2348 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: request for help with aux
  2016-06-27 20:07 request for help with aux Daniel Wilkerson
  2016-06-27 21:00 ` Bobby Bingham
@ 2016-06-27 21:58 ` Rich Felker
  2016-06-28  0:22 ` Patrick Oppenlander
  2 siblings, 0 replies; 6+ messages in thread
From: Rich Felker @ 2016-06-27 21:58 UTC (permalink / raw)
  To: musl

On Mon, Jun 27, 2016 at 01:07:59PM -0700, Daniel Wilkerson wrote:
> The musl crt0 code seems to expect that when the process starts that
> the stack pointer points to a data page having the following four data
> structures immediately contiguous: argc, argv, env, aux.

It's not necessarily a page. It's just an array of sorts that's
aligned to the (pointer) wordsize.

> I'm writing a loader for musl-riscv and I need to know how to
> initialize this data.  The only one I wonder about is the aux array.
> It seems that aux is a collection of name/value pairs which are used
> as follows in musl-riscv/src/env/__libc_start_main.c:

There are various places it's used; try something like:

	grep -r aux.*AT_ src ldso

For static linking only, at least AT_PHDR/AT_PHENT/AT_PHNUM need to be
correct in order for the executable's TLS image to be located at
runtime. AT_RANDOM should be valid (and point to a buffer of at least
16 random bytes IIRC) in order for stack protector to work correctly.
Other fields that are used if present but probably don't matter for
bare-metal setups you're looking at are AT_HWCAP, AT_SYSINFO_EHDR, and
AT_SECURE and the related uid/gid fields.

Rich


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: request for help with aux
  2016-06-27 20:07 request for help with aux Daniel Wilkerson
  2016-06-27 21:00 ` Bobby Bingham
  2016-06-27 21:58 ` Rich Felker
@ 2016-06-28  0:22 ` Patrick Oppenlander
  2 siblings, 0 replies; 6+ messages in thread
From: Patrick Oppenlander @ 2016-06-28  0:22 UTC (permalink / raw)
  To: musl

On 28/06/16 06:07, Daniel Wilkerson wrote:

>
> What I'm wondering is where to find the semantics of all of the aux
> names; I could hunt through all of the code, but any high-level
> suggestions you could provide could help a lot.  As a bonus, which
> ones might not have sensible defaults and are actually non-optional,
> if any.
>

Another helpful reference is the create_elf_tables function in 
fs/binfmt_elf.c in the linux kernel sources.

		Patrick



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-06-28  0:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-27 20:07 request for help with aux Daniel Wilkerson
2016-06-27 21:00 ` Bobby Bingham
2016-06-27 21:33   ` Daniel Wilkerson
2016-06-27 21:49     ` Nathan McSween
2016-06-27 21:58 ` Rich Felker
2016-06-28  0:22 ` Patrick Oppenlander

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).