Home | History | Annotate | Line # | Download | only in libtos
elf.c revision 1.4.4.2
      1  1.4.4.2  nathanw /*	$NetBSD: elf.c,v 1.4.4.2 2002/02/28 04:08:33 nathanw Exp $	*/
      2  1.4.4.2  nathanw 
      3  1.4.4.2  nathanw /*-
      4  1.4.4.2  nathanw  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.4.4.2  nathanw  * All rights reserved.
      6  1.4.4.2  nathanw  *
      7  1.4.4.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.4.4.2  nathanw  * by Leo Weppelman.
      9  1.4.4.2  nathanw  *
     10  1.4.4.2  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.4.4.2  nathanw  * modification, are permitted provided that the following conditions
     12  1.4.4.2  nathanw  * are met:
     13  1.4.4.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.4.4.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.4.4.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.4.4.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.4.4.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.4.4.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.4.4.2  nathanw  *    must display the following acknowledgement:
     20  1.4.4.2  nathanw  *        This product includes software developed by the NetBSD
     21  1.4.4.2  nathanw  *        Foundation, Inc. and its contributors.
     22  1.4.4.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.4.4.2  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.4.4.2  nathanw  *    from this software without specific prior written permission.
     25  1.4.4.2  nathanw  *
     26  1.4.4.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.4.4.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.4.4.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.4.4.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.4.4.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.4.4.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.4.4.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.4.4.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.4.4.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.4.4.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.4.4.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.4.4.2  nathanw  */
     38  1.4.4.2  nathanw 
     39  1.4.4.2  nathanw #ifdef TOSTOOLS
     40  1.4.4.2  nathanw #include <stdio.h>
     41  1.4.4.2  nathanw #include <stdlib.h>
     42  1.4.4.2  nathanw #include <unistd.h>
     43  1.4.4.2  nathanw #include <string.h>
     44  1.4.4.2  nathanw #include <sys/types.h>
     45  1.4.4.2  nathanw #include "exec_elf.h"
     46  1.4.4.2  nathanw 
     47  1.4.4.2  nathanw #define	MALLOC(x)	malloc(x)
     48  1.4.4.2  nathanw 
     49  1.4.4.2  nathanw #else
     50  1.4.4.2  nathanw 
     51  1.4.4.2  nathanw #include <stand.h>
     52  1.4.4.2  nathanw #include <atari_stand.h>
     53  1.4.4.2  nathanw #include <string.h>
     54  1.4.4.2  nathanw #include <libkern.h>
     55  1.4.4.2  nathanw #include <sys/exec_elf.h>
     56  1.4.4.2  nathanw 
     57  1.4.4.2  nathanw #define	MALLOC(x)	alloc(x)
     58  1.4.4.2  nathanw #endif
     59  1.4.4.2  nathanw 
     60  1.4.4.2  nathanw #include "libtos.h"
     61  1.4.4.2  nathanw #include "tosdefs.h"
     62  1.4.4.2  nathanw #include "kparamb.h"
     63  1.4.4.2  nathanw #include "cread.h"
     64  1.4.4.2  nathanw 
     65  1.4.4.2  nathanw /*
     66  1.4.4.2  nathanw  * Load an elf image.
     67  1.4.4.2  nathanw  * Exit codes:
     68  1.4.4.2  nathanw  *	-1      : Not an ELF file.
     69  1.4.4.2  nathanw  *	 0      : OK
     70  1.4.4.2  nathanw  *	 error# : Error during load (*errp might contain error string).
     71  1.4.4.2  nathanw  */
     72  1.4.4.2  nathanw #define ELFMAGIC	((ELFMAG0 << 24) | (ELFMAG1 << 16) | \
     73  1.4.4.2  nathanw 				(ELFMAG2 << 8) | ELFMAG3)
     74  1.4.4.2  nathanw 
     75  1.4.4.2  nathanw int
     76  1.4.4.2  nathanw elf_load(fd, od, errp, loadsyms)
     77  1.4.4.2  nathanw int	fd;
     78  1.4.4.2  nathanw osdsc_t	*od;
     79  1.4.4.2  nathanw char	**errp;
     80  1.4.4.2  nathanw int	loadsyms;
     81  1.4.4.2  nathanw {
     82  1.4.4.2  nathanw 	int		i,j;
     83  1.4.4.2  nathanw 	int		err;
     84  1.4.4.2  nathanw 	Elf32_Ehdr	ehdr;
     85  1.4.4.2  nathanw 	Elf32_Phdr	*phdrs;
     86  1.4.4.2  nathanw 	Elf32_Word	symsize, symstart;
     87  1.4.4.2  nathanw 	long		kernsize;
     88  1.4.4.2  nathanw 
     89  1.4.4.2  nathanw 	*errp = NULL;
     90  1.4.4.2  nathanw 	lseek(fd, (off_t)0, SEEK_SET);
     91  1.4.4.2  nathanw 	if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
     92  1.4.4.2  nathanw 		return -1;
     93  1.4.4.2  nathanw 
     94  1.4.4.2  nathanw 	if (*((u_int *)ehdr.e_ident) != ELFMAGIC)
     95  1.4.4.2  nathanw 		return -1;
     96  1.4.4.2  nathanw 
     97  1.4.4.2  nathanw 	/*
     98  1.4.4.2  nathanw 	 * calculate highest used address
     99  1.4.4.2  nathanw 	 */
    100  1.4.4.2  nathanw 	i = ehdr.e_phnum * sizeof(Elf32_Phdr);
    101  1.4.4.2  nathanw 	err = 1;
    102  1.4.4.2  nathanw 	if ((phdrs = (Elf32_Phdr *)MALLOC(i)) == NULL)
    103  1.4.4.2  nathanw 		goto error;
    104  1.4.4.2  nathanw 	err = 2;
    105  1.4.4.2  nathanw 	if (read(fd, phdrs, i) != i)
    106  1.4.4.2  nathanw 		goto error;
    107  1.4.4.2  nathanw 
    108  1.4.4.2  nathanw 	kernsize = 0;
    109  1.4.4.2  nathanw 	for (i = 0; i < ehdr.e_phnum; i++) {
    110  1.4.4.2  nathanw 		Elf32_Word sum;
    111  1.4.4.2  nathanw 
    112  1.4.4.2  nathanw 		sum = phdrs[i].p_vaddr + phdrs[i].p_memsz;
    113  1.4.4.2  nathanw 		if ((phdrs[i].p_flags & (PF_W|PF_X)) && (sum > kernsize))
    114  1.4.4.2  nathanw 			kernsize = sum;
    115  1.4.4.2  nathanw 	}
    116  1.4.4.2  nathanw 
    117  1.4.4.2  nathanw 	/*
    118  1.4.4.2  nathanw 	 * look for symbols and calculate the size
    119  1.4.4.2  nathanw 	 * XXX: This increases the load time by a factor 2 for gzipped
    120  1.4.4.2  nathanw 	 *      images!
    121  1.4.4.2  nathanw 	 */
    122  1.4.4.2  nathanw 	symsize  = 0;
    123  1.4.4.2  nathanw 	symstart = 0;
    124  1.4.4.2  nathanw 	if (loadsyms) {
    125  1.4.4.2  nathanw 	    i = ehdr.e_shnum + 1;
    126  1.4.4.2  nathanw 	    err = 3;
    127  1.4.4.2  nathanw 	    if (lseek(fd, (off_t)ehdr.e_shoff, SEEK_SET) != ehdr.e_shoff)
    128  1.4.4.2  nathanw 		goto error;
    129  1.4.4.2  nathanw 	    while (--i) {
    130  1.4.4.2  nathanw 	      Elf32_Shdr shdr;
    131  1.4.4.2  nathanw 
    132  1.4.4.2  nathanw 	      err = 4;
    133  1.4.4.2  nathanw 	      if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
    134  1.4.4.2  nathanw 		goto error;
    135  1.4.4.2  nathanw 	      if ((shdr.sh_type == SHT_SYMTAB) || (shdr.sh_type == SHT_STRTAB))
    136  1.4.4.2  nathanw 		symsize += shdr.sh_size;
    137  1.4.4.2  nathanw 	    }
    138  1.4.4.2  nathanw 	}
    139  1.4.4.2  nathanw 
    140  1.4.4.2  nathanw 	if (symsize) {
    141  1.4.4.2  nathanw 	  symstart = kernsize;
    142  1.4.4.2  nathanw 	  kernsize += symsize + sizeof(ehdr) + ehdr.e_shnum*sizeof(Elf32_Shdr);
    143  1.4.4.2  nathanw 	}
    144  1.4.4.2  nathanw 
    145  1.4.4.2  nathanw 	/*
    146  1.4.4.2  nathanw 	 * Extract various sizes from the kernel executable
    147  1.4.4.2  nathanw 	 */
    148  1.4.4.2  nathanw 	od->k_esym = symsize ? kernsize : 0;
    149  1.4.4.2  nathanw 	od->ksize  = kernsize;
    150  1.4.4.2  nathanw 	od->kentry = ehdr.e_entry;
    151  1.4.4.2  nathanw 
    152  1.4.4.2  nathanw 	err = 5;
    153  1.4.4.2  nathanw 	if ((od->kstart = (u_char *)MALLOC(od->ksize)) == NULL)
    154  1.4.4.2  nathanw 		goto error;
    155  1.4.4.2  nathanw 
    156  1.4.4.2  nathanw 	/*
    157  1.4.4.2  nathanw 	 * Read text & data, clear bss
    158  1.4.4.2  nathanw 	 */
    159  1.4.4.2  nathanw 	for (i = 0; i < ehdr.e_phnum; i++) {
    160  1.4.4.2  nathanw 	    u_char	*p;
    161  1.4.4.2  nathanw 	    Elf32_Phdr	*php = &phdrs[i];
    162  1.4.4.2  nathanw 
    163  1.4.4.2  nathanw 	    if (php->p_flags & (PF_W|PF_X)) {
    164  1.4.4.2  nathanw 		err = 6;
    165  1.4.4.2  nathanw 		if (lseek(fd, (off_t)php->p_offset, SEEK_SET) != php->p_offset)
    166  1.4.4.2  nathanw 		    goto error;
    167  1.4.4.2  nathanw 		p = (u_char *)(od->kstart) + php->p_vaddr;
    168  1.4.4.2  nathanw 		err = 7;
    169  1.4.4.2  nathanw 		if (read(fd, p, php->p_filesz) != php->p_filesz)
    170  1.4.4.2  nathanw 		    goto error;
    171  1.4.4.2  nathanw 		if (php->p_memsz > php->p_filesz)
    172  1.4.4.2  nathanw 		    bzero(p + php->p_filesz, php->p_memsz - php->p_filesz);
    173  1.4.4.2  nathanw 	    }
    174  1.4.4.2  nathanw 	}
    175  1.4.4.2  nathanw 
    176  1.4.4.2  nathanw 	/*
    177  1.4.4.2  nathanw 	 * Read symbols and strings
    178  1.4.4.2  nathanw 	 */
    179  1.4.4.2  nathanw 	if (symsize) {
    180  1.4.4.2  nathanw 	    u_char	*p, *symtab;
    181  1.4.4.2  nathanw 	    int		nhdrs;
    182  1.4.4.2  nathanw 	    Elf32_Shdr	*shp;
    183  1.4.4.2  nathanw 
    184  1.4.4.2  nathanw 	    symtab = od->kstart + symstart;
    185  1.4.4.2  nathanw 
    186  1.4.4.2  nathanw 	    p = symtab + sizeof(ehdr);
    187  1.4.4.2  nathanw 	    nhdrs = ehdr.e_shnum;
    188  1.4.4.2  nathanw 	    err = 8;
    189  1.4.4.2  nathanw 	    if (lseek(fd, (off_t)ehdr.e_shoff, SEEK_SET) != ehdr.e_shoff)
    190  1.4.4.2  nathanw 		goto error;
    191  1.4.4.2  nathanw 	    err = 9;
    192  1.4.4.2  nathanw 	    if (read(fd, p, nhdrs * sizeof(*shp)) != nhdrs * sizeof(*shp))
    193  1.4.4.2  nathanw 		goto error;
    194  1.4.4.2  nathanw 	    shp = (Elf32_Shdr*)p;
    195  1.4.4.2  nathanw 	    p  += nhdrs * sizeof(*shp);
    196  1.4.4.2  nathanw 	    for (i = 0; i < nhdrs; i++) {
    197  1.4.4.2  nathanw 		if (shp[i].sh_type == SHT_SYMTAB) {
    198  1.4.4.2  nathanw 		    if (shp[i].sh_offset == 0)
    199  1.4.4.2  nathanw 			continue;
    200  1.4.4.2  nathanw 		    /* Got the symbol table. */
    201  1.4.4.2  nathanw 		    err = 10;
    202  1.4.4.2  nathanw 		    if (lseek(fd, (off_t)shp[i].sh_offset, SEEK_SET) !=
    203  1.4.4.2  nathanw 							shp[i].sh_offset)
    204  1.4.4.2  nathanw 		    	goto error;
    205  1.4.4.2  nathanw 		    err = 11;
    206  1.4.4.2  nathanw 		    if (read(fd, p, shp[i].sh_size) != shp[i].sh_size)
    207  1.4.4.2  nathanw 			goto error;
    208  1.4.4.2  nathanw 		    shp[i].sh_offset = p - symtab;
    209  1.4.4.2  nathanw 		    /* Find the string table to go with it. */
    210  1.4.4.2  nathanw 		    j = shp[i].sh_link;
    211  1.4.4.2  nathanw 		    if (shp[j].sh_offset == 0)
    212  1.4.4.2  nathanw 			continue;
    213  1.4.4.2  nathanw 		    p += shp[i].sh_size;
    214  1.4.4.2  nathanw 		    err = 12;
    215  1.4.4.2  nathanw 		    if (lseek(fd, (off_t)shp[j].sh_offset, SEEK_SET) !=
    216  1.4.4.2  nathanw 				    			shp[j].sh_offset)
    217  1.4.4.2  nathanw 		    	goto error;
    218  1.4.4.2  nathanw 		    err = 13;
    219  1.4.4.2  nathanw 		    if (read(fd, p, shp[j].sh_size) != shp[j].sh_size)
    220  1.4.4.2  nathanw 			goto error;
    221  1.4.4.2  nathanw 		    shp[j].sh_offset = p - symtab;
    222  1.4.4.2  nathanw 		    /* There should only be one symbol table. */
    223  1.4.4.2  nathanw 		    break;
    224  1.4.4.2  nathanw 		}
    225  1.4.4.2  nathanw 	    }
    226  1.4.4.2  nathanw 	    ehdr.e_shoff = sizeof(ehdr);
    227  1.4.4.2  nathanw 	    bcopy(&ehdr, symtab, sizeof(ehdr));
    228  1.4.4.2  nathanw 	}
    229  1.4.4.2  nathanw 	return 0;
    230  1.4.4.2  nathanw 
    231  1.4.4.2  nathanw error:
    232  1.4.4.2  nathanw #ifdef TOSTOOLS
    233  1.4.4.2  nathanw 	{
    234  1.4.4.2  nathanw 		static char *errs[] = {
    235  1.4.4.2  nathanw 			/*  1 */ "Cannot malloc Elf phdr storage space",
    236  1.4.4.2  nathanw 			/*  2 */ "Cannot read Elf32_Phdrs",
    237  1.4.4.2  nathanw 			/*  3 */ "Cannot seek to e_shoff location",
    238  1.4.4.2  nathanw 			/*  4 */ "Cannot read Elf32_shdr",
    239  1.4.4.2  nathanw 			/*  5 */ "Cannot malloc kernel image space",
    240  1.4.4.2  nathanw 		    	/*  6 */ "Seek error while reading text segment\n",
    241  1.4.4.2  nathanw 		    	/*  7 */ "Read error in text segment\n",
    242  1.4.4.2  nathanw 			/*  8 */ "Error seeking to section headers",
    243  1.4.4.2  nathanw 			/*  9 */ "Error reading section headers",
    244  1.4.4.2  nathanw 		    	/* 10 */ "Error seeking to symbols",
    245  1.4.4.2  nathanw 			/* 11 */ "Error reading symbols",
    246  1.4.4.2  nathanw 		    	/* 12 */ "Error seeking to string table",
    247  1.4.4.2  nathanw 			/* 13 */ "Error reading strings"
    248  1.4.4.2  nathanw 		};
    249  1.4.4.2  nathanw 		*errp = errs[err];
    250  1.4.4.2  nathanw 	}
    251  1.4.4.2  nathanw #endif /* TOSTOOLS */
    252  1.4.4.2  nathanw 
    253  1.4.4.2  nathanw 	return err;
    254  1.4.4.2  nathanw }
    255