Home | History | Annotate | Line # | Download | only in hpcboot
load_elf.cpp revision 1.1.2.2
      1 /*	$NetBSD: load_elf.cpp,v 1.1.2.2 2001/02/11 19:09:56 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <load.h>
     40 #include <load_elf.h>
     41 #include <console.h>
     42 #include <memory.h>
     43 #include <file.h>
     44 
     45 ElfLoader::ElfLoader(Console *&cons, MemoryManager *&mem)
     46 	: Loader(cons, mem)
     47 {
     48 	DPRINTF((TEXT("Loader: ELF\n")));
     49 }
     50 
     51 ElfLoader::~ElfLoader(void)
     52 {
     53 }
     54 
     55 BOOL
     56 ElfLoader::setFile(File *&file)
     57 {
     58 	Loader::setFile(file);
     59 
     60 	/* read ELF header and check it */
     61 	if (!read_header())
     62 		return FALSE;
     63 	/* read program header */
     64 	size_t sz = _eh.e_phnum * _eh.e_phentsize;
     65 
     66 	return _file->read(_ph, sz, _eh.e_phoff) == sz;
     67 }
     68 
     69 size_t
     70 ElfLoader::memorySize()
     71 {
     72 	int i;
     73 	Elf_Phdr *ph = _ph;
     74 	size_t sz = 0;
     75 
     76 	DPRINTF((TEXT("file size: ")));
     77 	for (i = 0; i < _eh.e_phnum; i++, ph++) {
     78 		if (ph->p_type == PT_LOAD) {
     79 			size_t filesz = ph->p_filesz;
     80 			DPRINTF((TEXT("+0x%x"), filesz));
     81 			sz += _mem->roundPage(filesz);
     82 		}
     83 	}
     84 	DPRINTF((TEXT(" = 0x%x byte\n"), sz));
     85 	return sz;
     86 }
     87 
     88 kaddr_t
     89 ElfLoader::jumpAddr()
     90 {
     91 	DPRINTF((TEXT("kernel entry address: 0x%08x\n"), _eh.e_entry));
     92 	return _eh.e_entry;
     93 }
     94 
     95 BOOL
     96 ElfLoader::load()
     97 {
     98 	Elf_Phdr *ph;
     99 	int i;
    100 
    101 	_load_segment_start();
    102 
    103 	for (i = 0, ph = _ph; i < _eh.e_phnum; i++, ph++) {
    104 		if (ph->p_type == PT_LOAD) {
    105 			size_t filesz = ph->p_filesz;
    106 			size_t memsz = ph->p_memsz;
    107 			vaddr_t kv = ph->p_vaddr;
    108 			off_t fileofs = ph->p_offset;
    109 			DPRINTF((TEXT("[%d] vaddr 0x%08x file size 0x%x mem size 0x%x\n"),
    110 				 i, kv, filesz, memsz));
    111 			_load_segment(kv, memsz, fileofs, filesz);
    112 		}
    113 	}
    114 	/* tag chain still opening */
    115 
    116 	return TRUE;
    117 }
    118 
    119 BOOL
    120 ElfLoader::read_header(void)
    121 {
    122 	// read ELF header
    123 	_file->read(&_eh, sizeof(Elf_Ehdr), 0);
    124 
    125 	// check ELF Magic.
    126 	if (!is_elf_file()) {
    127 		DPRINTF((TEXT("not a ELF file.\n")));
    128 		return FALSE;
    129 	}
    130 
    131 	// Windows CE is 32bit little-endian only.
    132 	if (_eh.e_ident[EI_DATA] != ELFDATA2LSB ||
    133 	    _eh.e_ident[EI_CLASS] != ELFCLASS32) {
    134 		DPRINTF((TEXT("invalid class/data(%d/%d)\n"),
    135 			 _eh.e_ident[EI_CLASS], _eh.e_ident[EI_DATA]));
    136 		return FALSE;
    137 	}
    138 
    139 	// Is native architecture?
    140 	switch(_eh.e_machine) {
    141 		ELF32_MACHDEP_ID_CASES;
    142 	default:
    143 		DPRINTF((TEXT("not a native architecture. machine = %d\n"),
    144 			 _eh.e_machine));
    145 		return FALSE;
    146 	}
    147 
    148 	/* Check object type */
    149 	if (_eh.e_type != ET_EXEC) {
    150 		DPRINTF((TEXT("not a executable file. type = %d\n"),
    151 			 _eh.e_type));
    152 		return FALSE;
    153 	}
    154 
    155 	if (_eh.e_phoff == 0 || _eh.e_phnum == 0 || _eh.e_phnum > 16 ||
    156 	    _eh.e_phentsize != sizeof(Elf_Phdr)) {
    157 		DPRINTF((TEXT("invalid program header infomation.\n")));
    158 		return FALSE;
    159 	}
    160 
    161 	return TRUE;
    162 }
    163