/* Remote target system call support.
|
/* Remote target system call support.
|
Copyright 1997, 1998, 2002, 2004, 2007, 2008 Free Software Foundation, Inc.
|
Copyright 1997, 1998, 2002, 2004, 2007, 2008 Free Software Foundation, Inc.
|
Contributed by Cygnus Solutions.
|
Contributed by Cygnus Solutions.
|
|
|
This file is part of GDB.
|
This file is part of GDB.
|
|
|
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
the Free Software Foundation; either version 3 of the License, or
|
the Free Software Foundation; either version 3 of the License, or
|
(at your option) any later version.
|
(at your option) any later version.
|
|
|
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU General Public License for more details.
|
GNU General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
/* This interface isn't intended to be specific to any particular kind
|
/* This interface isn't intended to be specific to any particular kind
|
of remote (hardware, simulator, whatever). As such, support for it
|
of remote (hardware, simulator, whatever). As such, support for it
|
(e.g. sim/common/callback.c) should *not* live in the simulator source
|
(e.g. sim/common/callback.c) should *not* live in the simulator source
|
tree, nor should it live in the gdb source tree. K&R C must be
|
tree, nor should it live in the gdb source tree. K&R C must be
|
supported. */
|
supported. */
|
|
|
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
#include "cconfig.h"
|
#include "cconfig.h"
|
#endif
|
#endif
|
#include "ansidecl.h"
|
#include "ansidecl.h"
|
#include "libiberty.h"
|
#include "libiberty.h"
|
#include <stdarg.h>
|
#include <stdarg.h>
|
#include <stdio.h>
|
#include <stdio.h>
|
#ifdef HAVE_STDLIB_H
|
#ifdef HAVE_STDLIB_H
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#endif
|
#endif
|
#ifdef HAVE_STRING_H
|
#ifdef HAVE_STRING_H
|
#include <string.h>
|
#include <string.h>
|
#elif defined (HAVE_STRINGS_H)
|
#elif defined (HAVE_STRINGS_H)
|
#include <strings.h>
|
#include <strings.h>
|
#endif
|
#endif
|
#ifdef HAVE_UNISTD_H
|
#ifdef HAVE_UNISTD_H
|
#include <unistd.h>
|
#include <unistd.h>
|
#endif
|
#endif
|
#include <errno.h>
|
#include <errno.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
#include <time.h>
|
#include <time.h>
|
#include <sys/types.h>
|
#include <sys/types.h>
|
#include <sys/stat.h>
|
#include <sys/stat.h>
|
#include "gdb/callback.h"
|
#include "gdb/callback.h"
|
#include "targ-vals.h"
|
#include "targ-vals.h"
|
|
|
#ifndef ENOSYS
|
#ifndef ENOSYS
|
#define ENOSYS EINVAL
|
#define ENOSYS EINVAL
|
#endif
|
#endif
|
#ifndef ENAMETOOLONG
|
#ifndef ENAMETOOLONG
|
#define ENAMETOOLONG EINVAL
|
#define ENAMETOOLONG EINVAL
|
#endif
|
#endif
|
|
|
/* Maximum length of a path name. */
|
/* Maximum length of a path name. */
|
#ifndef MAX_PATH_LEN
|
#ifndef MAX_PATH_LEN
|
#define MAX_PATH_LEN 1024
|
#define MAX_PATH_LEN 1024
|
#endif
|
#endif
|
|
|
/* When doing file read/writes, do this many bytes at a time. */
|
/* When doing file read/writes, do this many bytes at a time. */
|
#define FILE_XFR_SIZE 4096
|
#define FILE_XFR_SIZE 4096
|
|
|
/* FIXME: for now, need to consider target word size. */
|
/* FIXME: for now, need to consider target word size. */
|
#define TWORD long
|
#define TWORD long
|
#define TADDR unsigned long
|
#define TADDR unsigned long
|
|
|
/* Path to be prepended to syscalls with absolute paths, and to be
|
/* Path to be prepended to syscalls with absolute paths, and to be
|
chdir:ed at startup, if not empty. */
|
chdir:ed at startup, if not empty. */
|
char *simulator_sysroot = "";
|
char *simulator_sysroot = "";
|
|
|
/* Utility of cb_syscall to fetch a path name or other string from the target.
|
/* Utility of cb_syscall to fetch a path name or other string from the target.
|
The result is 0 for success or a host errno value. */
|
The result is 0 for success or a host errno value. */
|
|
|
static int
|
static int
|
get_string (cb, sc, buf, buflen, addr)
|
get_string (cb, sc, buf, buflen, addr)
|
host_callback *cb;
|
host_callback *cb;
|
CB_SYSCALL *sc;
|
CB_SYSCALL *sc;
|
char *buf;
|
char *buf;
|
int buflen;
|
int buflen;
|
TADDR addr;
|
TADDR addr;
|
{
|
{
|
char *p, *pend;
|
char *p, *pend;
|
|
|
for (p = buf, pend = buf + buflen; p < pend; ++p, ++addr)
|
for (p = buf, pend = buf + buflen; p < pend; ++p, ++addr)
|
{
|
{
|
/* No, it isn't expected that this would cause one transaction with
|
/* No, it isn't expected that this would cause one transaction with
|
the remote target for each byte. The target could send the
|
the remote target for each byte. The target could send the
|
path name along with the syscall request, and cache the file
|
path name along with the syscall request, and cache the file
|
name somewhere (or otherwise tweak this as desired). */
|
name somewhere (or otherwise tweak this as desired). */
|
unsigned int count = (*sc->read_mem) (cb, sc, addr, p, 1);
|
unsigned int count = (*sc->read_mem) (cb, sc, addr, p, 1);
|
|
|
if (count != 1)
|
if (count != 1)
|
return EINVAL;
|
return EINVAL;
|
if (*p == 0)
|
if (*p == 0)
|
break;
|
break;
|
}
|
}
|
if (p == pend)
|
if (p == pend)
|
return ENAMETOOLONG;
|
return ENAMETOOLONG;
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/* Utility of cb_syscall to fetch a path name.
|
/* Utility of cb_syscall to fetch a path name.
|
The buffer is malloc'd and the address is stored in BUFP.
|
The buffer is malloc'd and the address is stored in BUFP.
|
The result is that of get_string, but prepended with
|
The result is that of get_string, but prepended with
|
simulator_sysroot if the string starts with '/'.
|
simulator_sysroot if the string starts with '/'.
|
If an error occurs, no buffer is left malloc'd. */
|
If an error occurs, no buffer is left malloc'd. */
|
|
|
static int
|
static int
|
get_path (cb, sc, addr, bufp)
|
get_path (cb, sc, addr, bufp)
|
host_callback *cb;
|
host_callback *cb;
|
CB_SYSCALL *sc;
|
CB_SYSCALL *sc;
|
TADDR addr;
|
TADDR addr;
|
char **bufp;
|
char **bufp;
|
{
|
{
|
char *buf = xmalloc (MAX_PATH_LEN);
|
char *buf = xmalloc (MAX_PATH_LEN);
|
int result;
|
int result;
|
int sysroot_len = strlen (simulator_sysroot);
|
int sysroot_len = strlen (simulator_sysroot);
|
|
|
result = get_string (cb, sc, buf, MAX_PATH_LEN - sysroot_len, addr);
|
result = get_string (cb, sc, buf, MAX_PATH_LEN - sysroot_len, addr);
|
if (result == 0)
|
if (result == 0)
|
{
|
{
|
/* Prepend absolute paths with simulator_sysroot. Relative paths
|
/* Prepend absolute paths with simulator_sysroot. Relative paths
|
are supposed to be relative to a chdir within that path, but at
|
are supposed to be relative to a chdir within that path, but at
|
this point unknown where. */
|
this point unknown where. */
|
if (simulator_sysroot[0] != '\0' && *buf == '/')
|
if (simulator_sysroot[0] != '\0' && *buf == '/')
|
{
|
{
|
/* Considering expected rareness of syscalls with absolute
|
/* Considering expected rareness of syscalls with absolute
|
file paths (compared to relative file paths and insn
|
file paths (compared to relative file paths and insn
|
execution), it does not seem worthwhile to rearrange things
|
execution), it does not seem worthwhile to rearrange things
|
to get rid of the string moves here; we'd need at least an
|
to get rid of the string moves here; we'd need at least an
|
extra call to check the initial '/' in the path. */
|
extra call to check the initial '/' in the path. */
|
memmove (buf + sysroot_len, buf, sysroot_len);
|
memmove (buf + sysroot_len, buf, sysroot_len);
|
memcpy (buf, simulator_sysroot, sysroot_len);
|
memcpy (buf, simulator_sysroot, sysroot_len);
|
}
|
}
|
|
|
*bufp = buf;
|
*bufp = buf;
|
}
|
}
|
else
|
else
|
free (buf);
|
free (buf);
|
return result;
|
return result;
|
}
|
}
|
|
|
/* Perform a system call on behalf of the target. */
|
/* Perform a system call on behalf of the target. */
|
|
|
CB_RC
|
CB_RC
|
cb_syscall (cb, sc)
|
cb_syscall (cb, sc)
|
host_callback *cb;
|
host_callback *cb;
|
CB_SYSCALL *sc;
|
CB_SYSCALL *sc;
|
{
|
{
|
TWORD result = 0, errcode = 0;
|
TWORD result = 0, errcode = 0;
|
|
|
if (sc->magic != CB_SYSCALL_MAGIC)
|
if (sc->magic != CB_SYSCALL_MAGIC)
|
abort ();
|
abort ();
|
|
|
switch (cb_target_to_host_syscall (cb, sc->func))
|
switch (cb_target_to_host_syscall (cb, sc->func))
|
{
|
{
|
#if 0 /* FIXME: wip */
|
#if 0 /* FIXME: wip */
|
case CB_SYS_argvlen :
|
case CB_SYS_argvlen :
|
{
|
{
|
/* Compute how much space is required to store the argv,envp
|
/* Compute how much space is required to store the argv,envp
|
strings so that the program can allocate the space and then
|
strings so that the program can allocate the space and then
|
call SYS_argv to fetch the values. */
|
call SYS_argv to fetch the values. */
|
int addr_size = cb->addr_size;
|
int addr_size = cb->addr_size;
|
int argc,envc,arglen,envlen;
|
int argc,envc,arglen,envlen;
|
const char **argv = cb->init_argv;
|
const char **argv = cb->init_argv;
|
const char **envp = cb->init_envp;
|
const char **envp = cb->init_envp;
|
|
|
argc = arglen = 0;
|
argc = arglen = 0;
|
if (argv)
|
if (argv)
|
{
|
{
|
for ( ; argv[argc]; ++argc)
|
for ( ; argv[argc]; ++argc)
|
arglen += strlen (argv[argc]) + 1;
|
arglen += strlen (argv[argc]) + 1;
|
}
|
}
|
envc = envlen = 0;
|
envc = envlen = 0;
|
if (envp)
|
if (envp)
|
{
|
{
|
for ( ; envp[envc]; ++envc)
|
for ( ; envp[envc]; ++envc)
|
envlen += strlen (envp[envc]) + 1;
|
envlen += strlen (envp[envc]) + 1;
|
}
|
}
|
result = arglen + envlen;
|
result = arglen + envlen;
|
break;
|
break;
|
}
|
}
|
|
|
case CB_SYS_argv :
|
case CB_SYS_argv :
|
{
|
{
|
/* Pointer to target's buffer. */
|
/* Pointer to target's buffer. */
|
TADDR tbuf = sc->arg1;
|
TADDR tbuf = sc->arg1;
|
/* Buffer size. */
|
/* Buffer size. */
|
int bufsize = sc->arg2;
|
int bufsize = sc->arg2;
|
/* Q is the target address of where all the strings go. */
|
/* Q is the target address of where all the strings go. */
|
TADDR q;
|
TADDR q;
|
int word_size = cb->word_size;
|
int word_size = cb->word_size;
|
int i,argc,envc,len;
|
int i,argc,envc,len;
|
const char **argv = cb->init_argv;
|
const char **argv = cb->init_argv;
|
const char **envp = cb->init_envp;
|
const char **envp = cb->init_envp;
|
|
|
argc = 0;
|
argc = 0;
|
if (argv)
|
if (argv)
|
{
|
{
|
for ( ; argv[argc]; ++argc)
|
for ( ; argv[argc]; ++argc)
|
{
|
{
|
int len = strlen (argv[argc]);
|
int len = strlen (argv[argc]);
|
int written = (*sc->write_mem) (cb, sc, tbuf, argv[argc], len + 1);
|
int written = (*sc->write_mem) (cb, sc, tbuf, argv[argc], len + 1);
|
if (written != len)
|
if (written != len)
|
{
|
{
|
result = -1;
|
result = -1;
|
errcode = EINVAL;
|
errcode = EINVAL;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
tbuf = len + 1;
|
tbuf = len + 1;
|
}
|
}
|
}
|
}
|
if ((*sc->write_mem) (cb, sc, tbuf, "", 1) != 1)
|
if ((*sc->write_mem) (cb, sc, tbuf, "", 1) != 1)
|
{
|
{
|
result = -1;
|
result = -1;
|
errcode = EINVAL;
|
errcode = EINVAL;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
tbuf++;
|
tbuf++;
|
envc = 0;
|
envc = 0;
|
if (envp)
|
if (envp)
|
{
|
{
|
for ( ; envp[envc]; ++envc)
|
for ( ; envp[envc]; ++envc)
|
{
|
{
|
int len = strlen (envp[envc]);
|
int len = strlen (envp[envc]);
|
int written = (*sc->write_mem) (cb, sc, tbuf, envp[envc], len + 1);
|
int written = (*sc->write_mem) (cb, sc, tbuf, envp[envc], len + 1);
|
if (written != len)
|
if (written != len)
|
{
|
{
|
result = -1;
|
result = -1;
|
errcode = EINVAL;
|
errcode = EINVAL;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
tbuf = len + 1;
|
tbuf = len + 1;
|
}
|
}
|
}
|
}
|
if ((*sc->write_mem) (cb, sc, tbuf, "", 1) != 1)
|
if ((*sc->write_mem) (cb, sc, tbuf, "", 1) != 1)
|
{
|
{
|
result = -1;
|
result = -1;
|
errcode = EINVAL;
|
errcode = EINVAL;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
result = argc;
|
result = argc;
|
sc->result2 = envc;
|
sc->result2 = envc;
|
break;
|
break;
|
}
|
}
|
#endif /* wip */
|
#endif /* wip */
|
|
|
case CB_SYS_exit :
|
case CB_SYS_exit :
|
/* Caller must catch and handle. */
|
/* Caller must catch and handle. */
|
break;
|
break;
|
|
|
case CB_SYS_open :
|
case CB_SYS_open :
|
{
|
{
|
char *path;
|
char *path;
|
|
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
if (errcode != 0)
|
if (errcode != 0)
|
{
|
{
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
result = (*cb->open) (cb, path, sc->arg2 /*, sc->arg3*/);
|
result = (*cb->open) (cb, path, sc->arg2 /*, sc->arg3*/);
|
free (path);
|
free (path);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_close :
|
case CB_SYS_close :
|
result = (*cb->close) (cb, sc->arg1);
|
result = (*cb->close) (cb, sc->arg1);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
break;
|
break;
|
|
|
case CB_SYS_read :
|
case CB_SYS_read :
|
{
|
{
|
/* ??? Perfect handling of error conditions may require only one
|
/* ??? Perfect handling of error conditions may require only one
|
call to cb->read. One can't assume all the data is
|
call to cb->read. One can't assume all the data is
|
contiguously stored in host memory so that would require
|
contiguously stored in host memory so that would require
|
malloc'ing/free'ing the space. Maybe later. */
|
malloc'ing/free'ing the space. Maybe later. */
|
char buf[FILE_XFR_SIZE];
|
char buf[FILE_XFR_SIZE];
|
int fd = sc->arg1;
|
int fd = sc->arg1;
|
TADDR addr = sc->arg2;
|
TADDR addr = sc->arg2;
|
size_t count = sc->arg3;
|
size_t count = sc->arg3;
|
size_t bytes_read = 0;
|
size_t bytes_read = 0;
|
int bytes_written;
|
int bytes_written;
|
|
|
while (count > 0)
|
while (count > 0)
|
{
|
{
|
if (cb_is_stdin (cb, fd))
|
if (cb_is_stdin (cb, fd))
|
result = (int) (*cb->read_stdin) (cb, buf,
|
result = (int) (*cb->read_stdin) (cb, buf,
|
(count < FILE_XFR_SIZE
|
(count < FILE_XFR_SIZE
|
? count : FILE_XFR_SIZE));
|
? count : FILE_XFR_SIZE));
|
else
|
else
|
result = (int) (*cb->read) (cb, fd, buf,
|
result = (int) (*cb->read) (cb, fd, buf,
|
(count < FILE_XFR_SIZE
|
(count < FILE_XFR_SIZE
|
? count : FILE_XFR_SIZE));
|
? count : FILE_XFR_SIZE));
|
if (result == -1)
|
if (result == -1)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
if (result == 0) /* EOF */
|
if (result == 0) /* EOF */
|
break;
|
break;
|
bytes_written = (*sc->write_mem) (cb, sc, addr, buf, result);
|
bytes_written = (*sc->write_mem) (cb, sc, addr, buf, result);
|
if (bytes_written != result)
|
if (bytes_written != result)
|
{
|
{
|
result = -1;
|
result = -1;
|
errcode = EINVAL;
|
errcode = EINVAL;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
bytes_read += result;
|
bytes_read += result;
|
count -= result;
|
count -= result;
|
addr += result;
|
addr += result;
|
/* If this is a short read, don't go back for more */
|
/* If this is a short read, don't go back for more */
|
if (result != FILE_XFR_SIZE)
|
if (result != FILE_XFR_SIZE)
|
break;
|
break;
|
}
|
}
|
result = bytes_read;
|
result = bytes_read;
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_write :
|
case CB_SYS_write :
|
{
|
{
|
/* ??? Perfect handling of error conditions may require only one
|
/* ??? Perfect handling of error conditions may require only one
|
call to cb->write. One can't assume all the data is
|
call to cb->write. One can't assume all the data is
|
contiguously stored in host memory so that would require
|
contiguously stored in host memory so that would require
|
malloc'ing/free'ing the space. Maybe later. */
|
malloc'ing/free'ing the space. Maybe later. */
|
char buf[FILE_XFR_SIZE];
|
char buf[FILE_XFR_SIZE];
|
int fd = sc->arg1;
|
int fd = sc->arg1;
|
TADDR addr = sc->arg2;
|
TADDR addr = sc->arg2;
|
size_t count = sc->arg3;
|
size_t count = sc->arg3;
|
int bytes_read;
|
int bytes_read;
|
size_t bytes_written = 0;
|
size_t bytes_written = 0;
|
|
|
while (count > 0)
|
while (count > 0)
|
{
|
{
|
int bytes_to_read = count < FILE_XFR_SIZE ? count : FILE_XFR_SIZE;
|
int bytes_to_read = count < FILE_XFR_SIZE ? count : FILE_XFR_SIZE;
|
bytes_read = (*sc->read_mem) (cb, sc, addr, buf, bytes_to_read);
|
bytes_read = (*sc->read_mem) (cb, sc, addr, buf, bytes_to_read);
|
if (bytes_read != bytes_to_read)
|
if (bytes_read != bytes_to_read)
|
{
|
{
|
result = -1;
|
result = -1;
|
errcode = EINVAL;
|
errcode = EINVAL;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
if (cb_is_stdout(cb, fd))
|
if (cb_is_stdout(cb, fd))
|
{
|
{
|
result = (int) (*cb->write_stdout) (cb, buf, bytes_read);
|
result = (int) (*cb->write_stdout) (cb, buf, bytes_read);
|
(*cb->flush_stdout) (cb);
|
(*cb->flush_stdout) (cb);
|
}
|
}
|
else if (cb_is_stderr(cb, fd))
|
else if (cb_is_stderr(cb, fd))
|
{
|
{
|
result = (int) (*cb->write_stderr) (cb, buf, bytes_read);
|
result = (int) (*cb->write_stderr) (cb, buf, bytes_read);
|
(*cb->flush_stderr) (cb);
|
(*cb->flush_stderr) (cb);
|
}
|
}
|
else
|
else
|
result = (int) (*cb->write) (cb, fd, buf, bytes_read);
|
result = (int) (*cb->write) (cb, fd, buf, bytes_read);
|
if (result == -1)
|
if (result == -1)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
bytes_written += result;
|
bytes_written += result;
|
count -= result;
|
count -= result;
|
addr += result;
|
addr += result;
|
}
|
}
|
result = bytes_written;
|
result = bytes_written;
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_lseek :
|
case CB_SYS_lseek :
|
{
|
{
|
int fd = sc->arg1;
|
int fd = sc->arg1;
|
unsigned long offset = sc->arg2;
|
unsigned long offset = sc->arg2;
|
int whence = sc->arg3;
|
int whence = sc->arg3;
|
|
|
result = (*cb->lseek) (cb, fd, offset, whence);
|
result = (*cb->lseek) (cb, fd, offset, whence);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_unlink :
|
case CB_SYS_unlink :
|
{
|
{
|
char *path;
|
char *path;
|
|
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
if (errcode != 0)
|
if (errcode != 0)
|
{
|
{
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
result = (*cb->unlink) (cb, path);
|
result = (*cb->unlink) (cb, path);
|
free (path);
|
free (path);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_truncate :
|
case CB_SYS_truncate :
|
{
|
{
|
char *path;
|
char *path;
|
long len = sc->arg2;
|
long len = sc->arg2;
|
|
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
if (errcode != 0)
|
if (errcode != 0)
|
{
|
{
|
result = -1;
|
result = -1;
|
errcode = EFAULT;
|
errcode = EFAULT;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
result = (*cb->truncate) (cb, path, len);
|
result = (*cb->truncate) (cb, path, len);
|
free (path);
|
free (path);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_ftruncate :
|
case CB_SYS_ftruncate :
|
{
|
{
|
int fd = sc->arg1;
|
int fd = sc->arg1;
|
long len = sc->arg2;
|
long len = sc->arg2;
|
|
|
result = (*cb->ftruncate) (cb, fd, len);
|
result = (*cb->ftruncate) (cb, fd, len);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_rename :
|
case CB_SYS_rename :
|
{
|
{
|
char *path1, *path2;
|
char *path1, *path2;
|
|
|
errcode = get_path (cb, sc, sc->arg1, &path1);
|
errcode = get_path (cb, sc, sc->arg1, &path1);
|
if (errcode != 0)
|
if (errcode != 0)
|
{
|
{
|
result = -1;
|
result = -1;
|
errcode = EFAULT;
|
errcode = EFAULT;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
errcode = get_path (cb, sc, sc->arg2, &path2);
|
errcode = get_path (cb, sc, sc->arg2, &path2);
|
if (errcode != 0)
|
if (errcode != 0)
|
{
|
{
|
result = -1;
|
result = -1;
|
errcode = EFAULT;
|
errcode = EFAULT;
|
free (path1);
|
free (path1);
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
result = (*cb->rename) (cb, path1, path2);
|
result = (*cb->rename) (cb, path1, path2);
|
free (path1);
|
free (path1);
|
free (path2);
|
free (path2);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_stat :
|
case CB_SYS_stat :
|
{
|
{
|
char *path,*buf;
|
char *path,*buf;
|
int buflen;
|
int buflen;
|
struct stat statbuf;
|
struct stat statbuf;
|
TADDR addr = sc->arg2;
|
TADDR addr = sc->arg2;
|
|
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
if (errcode != 0)
|
if (errcode != 0)
|
{
|
{
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
result = (*cb->stat) (cb, path, &statbuf);
|
result = (*cb->stat) (cb, path, &statbuf);
|
free (path);
|
free (path);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
buflen = cb_host_to_target_stat (cb, NULL, NULL);
|
buflen = cb_host_to_target_stat (cb, NULL, NULL);
|
buf = xmalloc (buflen);
|
buf = xmalloc (buflen);
|
if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
|
if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
|
{
|
{
|
/* The translation failed. This is due to an internal
|
/* The translation failed. This is due to an internal
|
host program error, not the target's fault. */
|
host program error, not the target's fault. */
|
free (buf);
|
free (buf);
|
errcode = ENOSYS;
|
errcode = ENOSYS;
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
if ((*sc->write_mem) (cb, sc, addr, buf, buflen) != buflen)
|
if ((*sc->write_mem) (cb, sc, addr, buf, buflen) != buflen)
|
{
|
{
|
free (buf);
|
free (buf);
|
errcode = EINVAL;
|
errcode = EINVAL;
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
free (buf);
|
free (buf);
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_fstat :
|
case CB_SYS_fstat :
|
{
|
{
|
char *buf;
|
char *buf;
|
int buflen;
|
int buflen;
|
struct stat statbuf;
|
struct stat statbuf;
|
TADDR addr = sc->arg2;
|
TADDR addr = sc->arg2;
|
|
|
result = (*cb->fstat) (cb, sc->arg1, &statbuf);
|
result = (*cb->fstat) (cb, sc->arg1, &statbuf);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
buflen = cb_host_to_target_stat (cb, NULL, NULL);
|
buflen = cb_host_to_target_stat (cb, NULL, NULL);
|
buf = xmalloc (buflen);
|
buf = xmalloc (buflen);
|
if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
|
if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
|
{
|
{
|
/* The translation failed. This is due to an internal
|
/* The translation failed. This is due to an internal
|
host program error, not the target's fault. */
|
host program error, not the target's fault. */
|
free (buf);
|
free (buf);
|
errcode = ENOSYS;
|
errcode = ENOSYS;
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
if ((*sc->write_mem) (cb, sc, addr, buf, buflen) != buflen)
|
if ((*sc->write_mem) (cb, sc, addr, buf, buflen) != buflen)
|
{
|
{
|
free (buf);
|
free (buf);
|
errcode = EINVAL;
|
errcode = EINVAL;
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
free (buf);
|
free (buf);
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_lstat :
|
case CB_SYS_lstat :
|
{
|
{
|
char *path, *buf;
|
char *path, *buf;
|
int buflen;
|
int buflen;
|
struct stat statbuf;
|
struct stat statbuf;
|
TADDR addr = sc->arg2;
|
TADDR addr = sc->arg2;
|
|
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
errcode = get_path (cb, sc, sc->arg1, &path);
|
if (errcode != 0)
|
if (errcode != 0)
|
{
|
{
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
result = (*cb->lstat) (cb, path, &statbuf);
|
result = (*cb->lstat) (cb, path, &statbuf);
|
free (path);
|
free (path);
|
if (result < 0)
|
if (result < 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
|
|
buflen = cb_host_to_target_stat (cb, NULL, NULL);
|
buflen = cb_host_to_target_stat (cb, NULL, NULL);
|
buf = xmalloc (buflen);
|
buf = xmalloc (buflen);
|
if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
|
if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
|
{
|
{
|
/* The translation failed. This is due to an internal
|
/* The translation failed. This is due to an internal
|
host program error, not the target's fault.
|
host program error, not the target's fault.
|
Unfortunately, it's hard to test this case, so there's no
|
Unfortunately, it's hard to test this case, so there's no
|
test-case for this execution path. */
|
test-case for this execution path. */
|
free (buf);
|
free (buf);
|
errcode = ENOSYS;
|
errcode = ENOSYS;
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
|
|
if ((*sc->write_mem) (cb, sc, addr, buf, buflen) != buflen)
|
if ((*sc->write_mem) (cb, sc, addr, buf, buflen) != buflen)
|
{
|
{
|
free (buf);
|
free (buf);
|
errcode = EINVAL;
|
errcode = EINVAL;
|
result = -1;
|
result = -1;
|
goto FinishSyscall;
|
goto FinishSyscall;
|
}
|
}
|
|
|
free (buf);
|
free (buf);
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_pipe :
|
case CB_SYS_pipe :
|
{
|
{
|
int p[2];
|
int p[2];
|
char *target_p = xcalloc (1, cb->target_sizeof_int * 2);
|
char *target_p = xcalloc (1, cb->target_sizeof_int * 2);
|
|
|
result = (*cb->pipe) (cb, p);
|
result = (*cb->pipe) (cb, p);
|
if (result != 0)
|
if (result != 0)
|
goto ErrorFinish;
|
goto ErrorFinish;
|
|
|
cb_store_target_endian (cb, target_p, cb->target_sizeof_int, p[0]);
|
cb_store_target_endian (cb, target_p, cb->target_sizeof_int, p[0]);
|
cb_store_target_endian (cb, target_p + cb->target_sizeof_int,
|
cb_store_target_endian (cb, target_p + cb->target_sizeof_int,
|
cb->target_sizeof_int, p[1]);
|
cb->target_sizeof_int, p[1]);
|
if ((*sc->write_mem) (cb, sc, sc->arg1, target_p,
|
if ((*sc->write_mem) (cb, sc, sc->arg1, target_p,
|
cb->target_sizeof_int * 2)
|
cb->target_sizeof_int * 2)
|
!= cb->target_sizeof_int * 2)
|
!= cb->target_sizeof_int * 2)
|
{
|
{
|
/* Close the pipe fd:s. */
|
/* Close the pipe fd:s. */
|
(*cb->close) (cb, p[0]);
|
(*cb->close) (cb, p[0]);
|
(*cb->close) (cb, p[1]);
|
(*cb->close) (cb, p[1]);
|
errcode = EFAULT;
|
errcode = EFAULT;
|
result = -1;
|
result = -1;
|
}
|
}
|
|
|
free (target_p);
|
free (target_p);
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_time :
|
case CB_SYS_time :
|
{
|
{
|
/* FIXME: May wish to change CB_SYS_time to something else.
|
/* FIXME: May wish to change CB_SYS_time to something else.
|
We might also want gettimeofday or times, but if system calls
|
We might also want gettimeofday or times, but if system calls
|
can be built on others, we can keep the number we have to support
|
can be built on others, we can keep the number we have to support
|
here down. */
|
here down. */
|
time_t t = (*cb->time) (cb, (time_t *) 0);
|
time_t t = (*cb->time) (cb, (time_t *) 0);
|
result = t;
|
result = t;
|
/* It is up to target code to process the argument to time(). */
|
/* It is up to target code to process the argument to time(). */
|
}
|
}
|
break;
|
break;
|
|
|
case CB_SYS_chdir :
|
case CB_SYS_chdir :
|
case CB_SYS_chmod :
|
case CB_SYS_chmod :
|
case CB_SYS_utime :
|
case CB_SYS_utime :
|
/* fall through for now */
|
/* fall through for now */
|
|
|
default :
|
default :
|
result = -1;
|
result = -1;
|
errcode = ENOSYS;
|
errcode = ENOSYS;
|
break;
|
break;
|
}
|
}
|
|
|
FinishSyscall:
|
FinishSyscall:
|
sc->result = result;
|
sc->result = result;
|
if (errcode == 0)
|
if (errcode == 0)
|
sc->errcode = 0;
|
sc->errcode = 0;
|
else
|
else
|
sc->errcode = cb_host_to_target_errno (cb, errcode);
|
sc->errcode = cb_host_to_target_errno (cb, errcode);
|
return CB_RC_OK;
|
return CB_RC_OK;
|
|
|
ErrorFinish:
|
ErrorFinish:
|
sc->result = result;
|
sc->result = result;
|
sc->errcode = (*cb->get_errno) (cb);
|
sc->errcode = (*cb->get_errno) (cb);
|
return CB_RC_OK;
|
return CB_RC_OK;
|
}
|
}
|
|
|