/*-
* Copyright (c) 1995, 1998, 2000, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
* Emmanuel Dreyfus.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* based on exec_aout.c, sunos_exec.c and svr4_exec.c
*/
#ifdef DEBUG_LINUX
#define DPRINTF(a) uprintf a
#else
#define DPRINTF(a) do {} while (0)
#endif
#ifdef LINUX_ATEXIT_SIGNATURE
/*
* On the PowerPC, statically linked Linux binaries are not recognized
* by linux_signature nor by linux_gcc_signature. Fortunately, thoses
* binaries features a __libc_atexit ELF section. We therefore assume we
* have a Linux binary if we find this section.
*/
int
ELFNAME2(linux,atexit_signature)(
struct lwp *l,
struct exec_package *epp,
Elf_Ehdr *eh)
{
Elf_Shdr *sh;
size_t shsize;
u_int shstrndx;
size_t i;
static const char signature[] = "__libc_atexit";
const size_t sigsz = sizeof(signature);
char tbuf[sizeof(signature)];
int error;
/* Now let's find the string table. If it does not exist, give up. */
shstrndx = eh->e_shstrndx;
if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
error = ENOEXEC;
goto out;
}
/* Check if any section has the name we're looking for. */
const off_t stroff = sh[shstrndx].sh_offset;
for (i = 0; i < eh->e_shnum; i++) {
Elf_Shdr *s = &sh[i];
if (s->sh_name + sigsz > sh[shstrndx].sh_size)
continue;
#ifdef LINUX_GCC_SIGNATURE
/*
* Take advantage of the fact that all the linux binaries are compiled
* with gcc, and gcc sticks in the comment field a signature. Note that
* on SVR4 binaries, the gcc signature will follow the OS name signature,
* that will not be a problem. We don't bother to read in the string table,
* but we check all the progbits headers.
*
* XXX This only works in the i386. On the alpha (at least)
* XXX we have the same gcc signature which incorrectly identifies
* XXX NetBSD binaries as Linux.
*/
int
ELFNAME2(linux,gcc_signature)(
struct lwp *l,
struct exec_package *epp,
Elf_Ehdr *eh)
{
size_t shsize;
size_t i;
static const char signature[] = "\0GCC: (GNU) ";
char tbuf[sizeof(signature) - 1];
Elf_Shdr *sh;
int error;
for (i = 0; i < eh->e_shnum; i++) {
Elf_Shdr *s = &sh[i];
/*
* Identify candidates for the comment header;
* Header cannot have a load address, or flags and
* it must be large enough.
*/
if (s->sh_type != SHT_PROGBITS ||
s->sh_addr != 0 ||
s->sh_flags != 0 ||
s->sh_size < sizeof(signature) - 1)
continue;
/*
* error is 0, if the signatures match we are done.
*/
DPRINTF(("linux_gcc_sig: sig=%s\n", tbuf));
if (!memcmp(tbuf, signature, sizeof(signature) - 1)) {
error = 0;
goto out;
}
}
error = ENOEXEC;
out:
free(sh, M_TEMP);
return (error);
}
#endif
#ifdef LINUX_DEBUGLINK_SIGNATURE
/*
* Look for a .gnu_debuglink, specific to x86_64 interpreter
*/
int
ELFNAME2(linux,debuglink_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
{
Elf_Shdr *sh;
size_t shsize;
u_int shstrndx;
size_t i;
static const char signature[] = ".gnu_debuglink";
const size_t sigsz = sizeof(signature);
char tbuf[sizeof(signature)];
int error;
/* Now let's find the string table. If it does not exist, give up. */
shstrndx = eh->e_shstrndx;
if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
error = ENOEXEC;
goto out;
}
/* Check if any section has the name we're looking for. */
const off_t stroff = sh[shstrndx].sh_offset;
for (i = 0; i < eh->e_shnum; i++) {
Elf_Shdr *s = &sh[i];
if (s->sh_name + sigsz > sh[shstrndx].sh_size)
continue;
#ifdef LINUX_GO_RT0_SIGNATURE
/*
* Look for a .gopclntab, specific to go binaries
* in it look for a symbol called _rt0_<cpu>_linux
*/
int
ELFNAME2(linux,go_rt0_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
{
Elf_Shdr *sh;
size_t shsize;
u_int shstrndx;
size_t i;
static const char signature[] = ".gopclntab";
const size_t sigsz = sizeof(signature);
char tbuf[sizeof(signature)], *tmp = NULL;
char mbuf[64];
const char *m;
int mlen;
int error;
/* Load the section header table. */
shsize = eh->e_shnum * sizeof(Elf_Shdr);
sh = malloc(shsize, M_TEMP, M_WAITOK);
error = exec_read(l, epp->ep_vp, eh->e_shoff, sh, shsize,
IO_NODELOCKED);
if (error)
goto out;
/* Now let's find the string table. If it does not exist, give up. */
shstrndx = eh->e_shstrndx;
if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
error = ENOEXEC;
goto out;
}
/* Check if any section has the name we're looking for. */
const off_t stroff = sh[shstrndx].sh_offset;
for (i = 0; i < eh->e_shnum; i++) {
Elf_Shdr *s = &sh[i];
if (s->sh_name + sigsz > sh[shstrndx].sh_size)
continue;
#ifndef LINUX_MACHDEP_ELF_COPYARGS
/*
* Copy arguments onto the stack in the normal way, but add some
* extra information in case of dynamic binding.
*/
int
ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
struct ps_strings *arginfo, char **stackp, void *argp)
{
size_t len;
AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
struct elf_args *ap;
int error;
struct vattr *vap;
uint32_t randbytes[4];