Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.sbin/mopd Add support for converting Elf32 images on-the...
details: https://anonhg.NetBSD.org/src/rev/5ead083daa34
branches: trunk
changeset: 522433:5ead083daa34
user: thorpej <thorpej%NetBSD.org@localhost>
date: Mon Feb 18 22:00:36 2002 +0000
description:
Add support for converting Elf32 images on-the-fly into MOP images.
(This involved some infrastructure changes to the various mopd
support programs/libraries.)
diffstat:
usr.sbin/mopd/common/common.h | 29 ++-
usr.sbin/mopd/common/file.c | 482 ++++++++++++++++++++++++++++---------
usr.sbin/mopd/common/file.h | 36 +-
usr.sbin/mopd/mopa.out/mopa.out.c | 18 +-
usr.sbin/mopd/mopchk/mopchk.c | 36 +-
usr.sbin/mopd/mopd/process.c | 12 +-
6 files changed, 434 insertions(+), 179 deletions(-)
diffs (truncated from 853 to 300 lines):
diff -r 5bd546153d8c -r 5ead083daa34 usr.sbin/mopd/common/common.h
--- a/usr.sbin/mopd/common/common.h Mon Feb 18 20:55:55 2002 +0000
+++ b/usr.sbin/mopd/common/common.h Mon Feb 18 22:00:36 2002 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: common.h,v 1.4 1998/02/07 00:03:19 cgd Exp $ */
+/* $NetBSD: common.h,v 1.5 2002/02/18 22:00:36 thorpej Exp $ */
/*
* Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
@@ -28,7 +28,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
- * $NetBSD: common.h,v 1.4 1998/02/07 00:03:19 cgd Exp $
+ * $NetBSD: common.h,v 1.5 2002/02/18 22:00:36 thorpej Exp $
*
*/
@@ -71,6 +71,12 @@
#define DL_STATUS_SENT_MLD 2
#define DL_STATUS_SENT_PLT 3
+typedef enum {
+ IMAGE_TYPE_MOP, /* MOP image */
+ IMAGE_TYPE_AOUT, /* a.out image */
+ IMAGE_TYPE_ELF32 /* Elf32 image */
+} mopd_imagetype;
+
struct dllist {
u_char status; /* Status byte */
struct if_info *ii; /* interface pointer */
@@ -83,7 +89,24 @@
u_int32_t xferaddr; /* Transfer Address */
u_int32_t nloadaddr; /* Next Load Address */
off_t lseek; /* Seek before last read */
- int aout; /* Is it an a.out file */
+ mopd_imagetype image_type; /* what type of image is it? */
+
+ /* For Elf32 files */
+ int e_nsec; /* number of program sections */
+#define SEC_MAX 4
+ struct {
+ off_t s_foff; /* file offset of section */
+ u_int32_t s_vaddr; /* virtual address of section */
+ u_int32_t s_fsize; /* file size of section */
+ u_int32_t s_msize; /* memory size of section */
+ u_int32_t s_pad; /* padding until next section */
+ u_int32_t s_loff; /* logical offset into image */
+ } e_sections[SEC_MAX]; /* program sections */
+ u_int32_t e_curpos; /* current logical position */
+ int e_cursec; /* current section */
+
+ /* For a.out files */
+ int a_mid; /* Machine ID */
u_int32_t a_text; /* Size of text segment */
u_int32_t a_text_fill; /* Size of text segment fill */
u_int32_t a_data; /* Size of data segment */
diff -r 5bd546153d8c -r 5ead083daa34 usr.sbin/mopd/common/file.c
--- a/usr.sbin/mopd/common/file.c Mon Feb 18 20:55:55 2002 +0000
+++ b/usr.sbin/mopd/common/file.c Mon Feb 18 22:00:36 2002 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: file.c,v 1.7 2001/01/16 02:50:31 cgd Exp $ */
+/* $NetBSD: file.c,v 1.8 2002/02/18 22:00:36 thorpej Exp $ */
/*
* Copyright (c) 1995-96 Mats O Jansson. All rights reserved.
@@ -31,28 +31,37 @@
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: file.c,v 1.7 2001/01/16 02:50:31 cgd Exp $");
+__RCSID("$NetBSD: file.c,v 1.8 2002/02/18 22:00:36 thorpej Exp $");
#endif
#include "os.h"
#include "common.h"
#include "file.h"
#include "mopdef.h"
+#include <stddef.h>
#ifndef NOAOUT
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-#include <sys/exec_aout.h>
-#endif
-#if defined(__bsdi__)
-#define NOAOUT
-#endif
-#if defined(__FreeBSD__)
-#include <sys/imgact_aout.h>
-#endif
-#if !defined(MID_VAX)
-#define MID_VAX 140
-#endif
-#endif
+# if defined(__NetBSD__) || defined(__OpenBSD__)
+# include <sys/exec_aout.h>
+# endif
+# if defined(__bsdi__)
+# define NOAOUT
+# endif
+# if defined(__FreeBSD__)
+# include <sys/imgact_aout.h>
+# endif
+# if !defined(MID_VAX)
+# define MID_VAX 140
+# endif
+#endif /* NOAOUT */
+
+#ifndef NOELF
+# if defined(__NetBSD__)
+# include <sys/exec_elf.h>
+# else
+# define NOELF
+# endif
+#endif /* NOELF */
int getCLBYTES __P((int));
int getMID __P((int, int));
@@ -161,15 +170,14 @@
}
int
-GetMopFileInfo(fd, load, xfr)
- int fd;
- u_int32_t *load, *xfr;
+GetMopFileInfo(dl)
+ struct dllist *dl;
{
u_char header[512];
short image_type;
u_int32_t load_addr, xfr_addr, isd, iha, hbcnt, isize;
- if (read(fd, header, 512) != 512)
+ if (read(dl->ldfd, header, 512) != 512)
return(-1);
image_type = (u_short)(header[IHD_W_ALIAS+1]*256 +
@@ -266,13 +274,9 @@
return(-1);
}
- if (load != NULL) {
- *load = load_addr;
- }
-
- if (xfr != NULL) {
- *xfr = xfr_addr;
- }
+ dl->image_type = IMAGE_TYPE_MOP;
+ dl->loadaddr = load_addr;
+ dl->xferaddr = xfr_addr;
return(0);
}
@@ -397,6 +401,217 @@
#endif
int
+CheckElfFile(fd)
+ int fd;
+{
+#ifdef NOELF
+ return(-1);
+#else
+ Elf32_Ehdr ehdr;
+
+ (void)lseek(fd, (off_t) 0, SEEK_SET);
+
+ if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
+ return(-1);
+
+ if (ehdr.e_ident[0] != ELFMAG0 ||
+ ehdr.e_ident[1] != ELFMAG1 ||
+ ehdr.e_ident[2] != ELFMAG2 ||
+ ehdr.e_ident[3] != ELFMAG3)
+ return(-1);
+
+ /* Must be Elf32... */
+ if (ehdr.e_ident[EI_CLASS] != ELFCLASS32)
+ return(-1);
+
+ return(0);
+#endif /* NOELF */
+}
+
+int
+GetElfFileInfo(dl)
+ struct dllist *dl;
+{
+#ifdef NOELF
+ return(-1);
+#else
+ Elf32_Ehdr ehdr;
+ Elf32_Phdr phdr;
+ uint32_t e_machine, e_entry;
+ uint32_t e_phoff, e_phentsize, e_phnum;
+ int ei_data, i;
+
+ (void)lseek(dl->ldfd, (off_t) 0, SEEK_SET);
+
+ if (read(dl->ldfd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
+ return(-1);
+
+ if (ehdr.e_ident[0] != ELFMAG0 ||
+ ehdr.e_ident[1] != ELFMAG1 ||
+ ehdr.e_ident[2] != ELFMAG2 ||
+ ehdr.e_ident[3] != ELFMAG3)
+ return(-1);
+
+ /* Must be Elf32... */
+ if (ehdr.e_ident[EI_CLASS] != ELFCLASS32)
+ return(-1);
+
+ ei_data = ehdr.e_ident[EI_DATA];
+
+ switch (ei_data) {
+ case ELFDATA2LSB:
+ e_machine = mopFileGetLX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_machine),
+ sizeof(ehdr.e_machine));
+ e_entry = mopFileGetLX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_entry),
+ sizeof(ehdr.e_entry));
+
+ e_phoff = mopFileGetLX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_phoff),
+ sizeof(ehdr.e_phoff));
+ e_phentsize = mopFileGetLX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_phentsize),
+ sizeof(ehdr.e_phentsize));
+ e_phnum = mopFileGetLX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_phnum),
+ sizeof(ehdr.e_phnum));
+ break;
+
+ case ELFDATA2MSB:
+ e_machine = mopFileGetBX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_machine),
+ sizeof(ehdr.e_machine));
+ e_entry = mopFileGetBX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_entry),
+ sizeof(ehdr.e_entry));
+
+ e_phoff = mopFileGetBX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_phoff),
+ sizeof(ehdr.e_phoff));
+ e_phentsize = mopFileGetBX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_phentsize),
+ sizeof(ehdr.e_phentsize));
+ e_phnum = mopFileGetBX((u_char *) &ehdr,
+ offsetof(Elf32_Ehdr, e_phnum),
+ sizeof(ehdr.e_phnum));
+ break;
+
+ default:
+ return(-1);
+ }
+
+ dl->image_type = IMAGE_TYPE_ELF32;
+ dl->loadaddr = e_entry; /* We assume the standalone program */
+ dl->xferaddr = e_entry; /* will relocate itself if necessary */
+
+ if (e_phnum > SEC_MAX)
+ return(-1);
+ dl->e_nsec = e_phnum;
+ for (i = 0; i < dl->e_nsec; i++) {
+ if (lseek(dl->ldfd, (off_t) e_phoff + (i * e_phentsize),
+ SEEK_SET) == (off_t) -1)
+ return(-1);
+ if (read(dl->ldfd, (char *) &phdr, sizeof(phdr)) !=
+ sizeof(phdr))
+ return(-1);
+
+ switch (ei_data) {
+ case ELFDATA2LSB:
+ dl->e_sections[i].s_foff =
+ mopFileGetLX((u_char *) &phdr,
+ offsetof(Elf32_Phdr, p_offset),
+ sizeof(phdr.p_offset));
+ dl->e_sections[i].s_vaddr =
+ mopFileGetLX((u_char *) &phdr,
+ offsetof(Elf32_Phdr, p_vaddr),
+ sizeof(phdr.p_vaddr));
+ dl->e_sections[i].s_fsize =
+ mopFileGetLX((u_char *) &phdr,
+ offsetof(Elf32_Phdr, p_filesz),
+ sizeof(phdr.p_filesz));
+ dl->e_sections[i].s_msize =
+ mopFileGetLX((u_char *) &phdr,
+ offsetof(Elf32_Phdr, p_memsz),
+ sizeof(phdr.p_memsz));
+ break;
+
+ case ELFDATA2MSB:
+ dl->e_sections[i].s_foff =
+ mopFileGetBX((u_char *) &phdr,
+ offsetof(Elf32_Phdr, p_offset),
+ sizeof(phdr.p_offset));
+ dl->e_sections[i].s_vaddr =
+ mopFileGetBX((u_char *) &phdr,
Home |
Main Index |
Thread Index |
Old Index