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