Home | History | Annotate | Line # | Download | only in libsa
loadfile_ecoff.c revision 1.11
      1  1.11  tsutsui /* $NetBSD: loadfile_ecoff.c,v 1.11 2007/12/29 17:54:42 tsutsui Exp $ */
      2   1.1  thorpej 
      3   1.1  thorpej /*-
      4   1.1  thorpej  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5   1.1  thorpej  * All rights reserved.
      6   1.1  thorpej  *
      7   1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1  thorpej  * NASA Ames Research Center and by Christos Zoulas.
     10   1.1  thorpej  *
     11   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.1  thorpej  * modification, are permitted provided that the following conditions
     13   1.1  thorpej  * are met:
     14   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20   1.1  thorpej  *    must display the following acknowledgement:
     21   1.1  thorpej  *	This product includes software developed by the NetBSD
     22   1.1  thorpej  *	Foundation, Inc. and its contributors.
     23   1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25   1.1  thorpej  *    from this software without specific prior written permission.
     26   1.1  thorpej  *
     27   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1  thorpej  */
     39   1.1  thorpej 
     40   1.2  thorpej #ifdef _STANDALONE
     41   1.2  thorpej #include <lib/libsa/stand.h>
     42   1.2  thorpej #include <lib/libkern/libkern.h>
     43   1.5    perry #else
     44   1.2  thorpej #include <stdio.h>
     45   1.2  thorpej #include <string.h>
     46   1.2  thorpej #include <errno.h>
     47   1.2  thorpej #include <stdlib.h>
     48   1.5    perry #include <unistd.h>
     49   1.2  thorpej #include <fcntl.h>
     50   1.2  thorpej #include <err.h>
     51   1.5    perry #endif
     52   1.2  thorpej 
     53   1.5    perry #include <sys/param.h>
     54   1.2  thorpej #include <sys/exec.h>
     55   1.2  thorpej 
     56   1.2  thorpej #include "loadfile.h"
     57   1.2  thorpej 
     58   1.2  thorpej #ifdef BOOT_ECOFF
     59   1.2  thorpej 
     60   1.2  thorpej int
     61  1.11  tsutsui loadfile_coff(int fd, struct ecoff_exechdr *coff, u_long *marks, int flags)
     62   1.1  thorpej {
     63   1.1  thorpej 	paddr_t offset = marks[MARK_START];
     64   1.1  thorpej 	paddr_t minp = ~0, maxp = 0, pos;
     65   1.9    isaki 	ssize_t nr;
     66   1.4  reinoud 
     67   1.4  reinoud 	/* some ports dont use the offset */
     68   1.4  reinoud 	offset = offset;
     69   1.1  thorpej 
     70   1.1  thorpej 	/* Read in text. */
     71   1.1  thorpej 	if (lseek(fd, ECOFF_TXTOFF(coff), SEEK_SET) == -1)  {
     72   1.1  thorpej 		WARN(("lseek text"));
     73   1.1  thorpej 		return 1;
     74   1.1  thorpej 	}
     75   1.1  thorpej 
     76   1.1  thorpej 	if (coff->a.tsize != 0) {
     77   1.1  thorpej 		if (flags & LOAD_TEXT) {
     78   1.1  thorpej 			PROGRESS(("%lu", coff->a.tsize));
     79   1.9    isaki 			nr = READ(fd, coff->a.text_start, coff->a.tsize);
     80   1.9    isaki 			if (nr == -1) {
     81   1.9    isaki 				return 1;
     82   1.9    isaki 			}
     83   1.9    isaki 			if (nr != coff->a.tsize) {
     84  1.10    isaki 				errno = EIO;
     85   1.1  thorpej 				return 1;
     86   1.1  thorpej 			}
     87   1.1  thorpej 		}
     88   1.1  thorpej 		else {
     89   1.1  thorpej 			if (lseek(fd, coff->a.tsize, SEEK_CUR) == -1) {
     90   1.1  thorpej 				WARN(("read text"));
     91   1.1  thorpej 				return 1;
     92   1.1  thorpej 			}
     93   1.1  thorpej 		}
     94   1.1  thorpej 		if (flags & (COUNT_TEXT|LOAD_TEXT)) {
     95   1.1  thorpej 			pos = coff->a.text_start;
     96   1.1  thorpej 			if (minp > pos)
     97   1.1  thorpej 				minp = pos;
     98   1.1  thorpej 			pos += coff->a.tsize;
     99   1.1  thorpej 			if (maxp < pos)
    100   1.1  thorpej 				maxp = pos;
    101   1.1  thorpej 		}
    102   1.1  thorpej 	}
    103   1.1  thorpej 
    104   1.1  thorpej 	/* Read in data. */
    105   1.1  thorpej 	if (coff->a.dsize != 0) {
    106   1.1  thorpej 		if (flags & LOAD_DATA) {
    107   1.1  thorpej 			PROGRESS(("+%lu", coff->a.dsize));
    108   1.9    isaki 			nr = READ(fd, coff->a.data_start, coff->a.dsize);
    109   1.9    isaki 			if (nr == -1) {
    110   1.9    isaki 				WARN(("read data"));
    111   1.9    isaki 				return 1;
    112   1.9    isaki 			}
    113   1.9    isaki 			if (nr != coff->a.dsize) {
    114  1.10    isaki 				errno = EIO;
    115   1.1  thorpej 				WARN(("read data"));
    116   1.1  thorpej 				return 1;
    117   1.1  thorpej 			}
    118   1.1  thorpej 		}
    119   1.1  thorpej 		if (flags & (COUNT_DATA|LOAD_DATA)) {
    120   1.1  thorpej 			pos = coff->a.data_start;
    121   1.1  thorpej 			if (minp > pos)
    122   1.1  thorpej 				minp = pos;
    123   1.1  thorpej 			pos += coff->a.dsize;
    124   1.1  thorpej 			if (maxp < pos)
    125   1.1  thorpej 				maxp = pos;
    126   1.1  thorpej 		}
    127   1.1  thorpej 	}
    128   1.1  thorpej 
    129   1.1  thorpej 	/* Zero out bss. */
    130   1.1  thorpej 	if (coff->a.bsize != 0) {
    131   1.1  thorpej 		if (flags & LOAD_BSS) {
    132   1.1  thorpej 			PROGRESS(("+%lu", coff->a.bsize));
    133   1.1  thorpej 			BZERO(coff->a.bss_start, coff->a.bsize);
    134   1.1  thorpej 		}
    135   1.1  thorpej 		if (flags & (COUNT_BSS|LOAD_BSS)) {
    136   1.1  thorpej 			pos = coff->a.bss_start;
    137   1.1  thorpej 			if (minp > pos)
    138   1.1  thorpej 				minp = pos;
    139   1.1  thorpej 			pos = coff->a.bsize;
    140   1.1  thorpej 			if (maxp < pos)
    141   1.1  thorpej 				maxp = pos;
    142   1.1  thorpej 		}
    143   1.1  thorpej 	}
    144   1.1  thorpej 
    145   1.1  thorpej 	marks[MARK_START] = LOADADDR(minp);
    146   1.1  thorpej 	marks[MARK_ENTRY] = LOADADDR(coff->a.entry);
    147   1.1  thorpej 	marks[MARK_NSYM] = 1;	/* XXX: Kernel needs >= 0 */
    148   1.1  thorpej 	marks[MARK_SYM] = LOADADDR(maxp);
    149   1.1  thorpej 	marks[MARK_END] = LOADADDR(maxp);
    150   1.8   martin 	marks[MARK_DATA] = LOADADDR(coff->a.data_start);
    151   1.1  thorpej 	return 0;
    152   1.1  thorpej }
    153   1.2  thorpej 
    154   1.2  thorpej #endif /* BOOT_ECOFF */
    155