2000-05-22 23:15:33 +04:00
|
|
|
/* $NetBSD: elfXX_exec.c,v 1.6 2000/05/22 19:15:33 eeh Exp $ */
|
1998-08-17 03:30:00 +04:00
|
|
|
|
|
|
|
/*
|
2000-03-06 04:29:04 +03:00
|
|
|
* Copyright (c) 1998-2000 Eduardo Horvath. All rights reserved.
|
1998-08-17 03:30:00 +04:00
|
|
|
* Copyright (c) 1997 Jason R. Thorpe. All rights reserved.
|
|
|
|
* Copyright (C) 1995, 1996 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1995, 1996 TooLs GmbH.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* ELF support derived from NetBSD/alpha's boot loader, written
|
|
|
|
* by Christopher G. Demetriou.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First try for the boot code
|
|
|
|
*
|
|
|
|
* Input syntax is:
|
|
|
|
* [promdev[{:|,}partition]]/[filename] [flags]
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CONCAT(x,y) __CONCAT(x,y)
|
|
|
|
#define CAT3(s,m,e) CONCAT(s,CONCAT(m,e))
|
2000-03-06 04:29:04 +03:00
|
|
|
#define MEG (1024*1024)
|
1998-08-17 03:30:00 +04:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
int CAT3(elf,ELFSIZE,_exec) __P((int, CAT3(Elf,ELFSIZE,_Ehdr) *, u_int64_t *, void **, void **));
|
|
|
|
#endif
|
2000-05-22 23:15:33 +04:00
|
|
|
#if defined(ELFSIZE) && (ELFSIZE == 32)
|
|
|
|
#define ELF_ALIGN(x) (((x)+3)&(~3))
|
|
|
|
#elif defined(ELFSIZE) && (ELFSIZE == 64)
|
|
|
|
#define ELF_ALIGN(x) (((x)+7)&(~7))
|
|
|
|
#else
|
|
|
|
#error ELFSIZE must be either 32 or 64!
|
|
|
|
#endif
|
1998-08-17 03:30:00 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
CAT3(elf, ELFSIZE, _exec)(fd, elf, entryp, ssymp, esymp)
|
|
|
|
int fd;
|
|
|
|
CAT3(Elf,ELFSIZE,_Ehdr) *elf;
|
|
|
|
u_int64_t *entryp;
|
|
|
|
void **ssymp;
|
|
|
|
void **esymp;
|
|
|
|
{
|
|
|
|
CAT3(Elf,ELFSIZE,_Shdr) *shp;
|
|
|
|
CAT3(Elf,ELFSIZE,_Off) off;
|
|
|
|
void *addr;
|
|
|
|
size_t size;
|
2000-03-06 04:29:04 +03:00
|
|
|
u_int align;
|
1998-08-17 03:30:00 +04:00
|
|
|
int i, first = 1;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't display load address for ELF; it's encoded in
|
|
|
|
* each section.
|
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
1998-08-27 10:23:31 +04:00
|
|
|
printf("elf%d_exec: ", ELFSIZE);
|
1998-08-17 03:30:00 +04:00
|
|
|
#endif
|
|
|
|
printf("Booting %s\n", opened_name);
|
|
|
|
|
|
|
|
for (i = 0; i < elf->e_phnum; i++) {
|
|
|
|
CAT3(Elf,ELFSIZE,_Phdr) phdr;
|
1998-08-27 10:23:31 +04:00
|
|
|
size = lseek(fd, (size_t)(elf->e_phoff + sizeof(phdr) * i), SEEK_SET);
|
1998-08-17 03:30:00 +04:00
|
|
|
if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
|
|
|
|
printf("read phdr: %s\n", strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
1999-10-25 17:55:06 +04:00
|
|
|
if (phdr.p_type != PT_LOAD ||
|
|
|
|
(phdr.p_flags & (PF_W|PF_X)) == 0)
|
1998-08-17 03:30:00 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Read in segment. */
|
1998-08-23 06:48:28 +04:00
|
|
|
printf("%s%lu@0x%lx", first ? "" : "+", (u_long)phdr.p_filesz,
|
1998-08-17 03:30:00 +04:00
|
|
|
(u_long)phdr.p_vaddr);
|
1998-08-27 10:23:31 +04:00
|
|
|
(void)lseek(fd, (size_t)phdr.p_offset, SEEK_SET);
|
2000-03-06 04:29:04 +03:00
|
|
|
/*
|
|
|
|
* If the segment's VA is aligned on a 4MB boundary, align its
|
|
|
|
* request 4MB aligned physical memory. Otherwise use default
|
|
|
|
* alignment.
|
|
|
|
*/
|
|
|
|
align = phdr.p_align;
|
|
|
|
if ((phdr.p_vaddr & (4*MEG-1)) == 0)
|
|
|
|
align = 4*MEG;
|
|
|
|
if (OF_claim((void *)(long)phdr.p_vaddr, phdr.p_memsz, align) ==
|
1998-08-17 03:30:00 +04:00
|
|
|
(void *)-1)
|
|
|
|
panic("cannot claim memory");
|
1998-08-23 06:48:28 +04:00
|
|
|
if (read(fd, (void *)(long)phdr.p_vaddr, phdr.p_filesz) !=
|
1998-08-17 03:30:00 +04:00
|
|
|
phdr.p_filesz) {
|
|
|
|
printf("read segment: %s\n", strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
1998-08-23 06:48:28 +04:00
|
|
|
syncicache((void *)(long)phdr.p_vaddr, phdr.p_filesz);
|
1998-08-17 03:30:00 +04:00
|
|
|
|
|
|
|
/* Zero BSS. */
|
|
|
|
if (phdr.p_filesz < phdr.p_memsz) {
|
1998-08-23 06:48:28 +04:00
|
|
|
printf("+%lu@0x%lx", (u_long)phdr.p_memsz - phdr.p_filesz,
|
1998-08-17 03:30:00 +04:00
|
|
|
(u_long)(phdr.p_vaddr + phdr.p_filesz));
|
1998-08-23 06:48:28 +04:00
|
|
|
bzero((void*)(long)phdr.p_vaddr + phdr.p_filesz,
|
1998-08-27 10:23:31 +04:00
|
|
|
(size_t)phdr.p_memsz - phdr.p_filesz);
|
1998-08-17 03:30:00 +04:00
|
|
|
}
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" \n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the size of the symbol table.
|
|
|
|
*/
|
|
|
|
size = sizeof(CAT3(Elf,ELFSIZE,_Ehdr)) + (elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
|
|
|
|
shp = addr = alloc(elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
|
1998-08-27 10:23:31 +04:00
|
|
|
(void)lseek(fd, (off_t)elf->e_shoff, SEEK_SET);
|
|
|
|
if (read(fd, addr, (size_t)(elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)))) !=
|
1998-08-17 03:30:00 +04:00
|
|
|
elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr))) {
|
|
|
|
printf("read section headers: %s\n", strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
for (i = 0; i < elf->e_shnum; i++, shp++) {
|
1999-10-25 17:55:06 +04:00
|
|
|
if (shp->sh_type == SHT_NULL)
|
1998-08-17 03:30:00 +04:00
|
|
|
continue;
|
1999-10-25 17:55:06 +04:00
|
|
|
if (shp->sh_type != SHT_SYMTAB
|
|
|
|
&& shp->sh_type != SHT_STRTAB) {
|
1998-08-17 03:30:00 +04:00
|
|
|
shp->sh_offset = 0;
|
1999-10-25 17:55:06 +04:00
|
|
|
shp->sh_type = SHT_NOBITS;
|
1998-08-17 03:30:00 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size += shp->sh_size;
|
|
|
|
}
|
|
|
|
shp = addr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reserve memory for the symbols.
|
|
|
|
*/
|
|
|
|
if ((addr = OF_claim(0, size, NBPG)) == (void *)-1)
|
|
|
|
panic("no space for symbol table");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the headers.
|
|
|
|
*/
|
|
|
|
elf->e_phoff = 0;
|
|
|
|
elf->e_shoff = sizeof(CAT3(Elf,ELFSIZE,_Ehdr));
|
|
|
|
elf->e_phentsize = 0;
|
|
|
|
elf->e_phnum = 0;
|
|
|
|
bcopy(elf, addr, sizeof(CAT3(Elf,ELFSIZE,_Ehdr)));
|
1998-08-23 06:48:28 +04:00
|
|
|
bcopy(shp, addr + sizeof(CAT3(Elf,ELFSIZE,_Ehdr)),
|
|
|
|
elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
|
1998-08-17 03:30:00 +04:00
|
|
|
free(shp, elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
|
|
|
|
*ssymp = addr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now load the symbol sections themselves.
|
|
|
|
*/
|
|
|
|
shp = addr + sizeof(CAT3(Elf,ELFSIZE,_Ehdr));
|
2000-05-22 23:15:33 +04:00
|
|
|
size = sizeof(CAT3(Elf,ELFSIZE,_Ehdr)) +
|
|
|
|
(elf->e_shnum * sizeof(CAT3(Elf,ELFSIZE,_Shdr)));
|
|
|
|
size = ELF_ALIGN(size);
|
|
|
|
addr += size;
|
|
|
|
off = size;
|
1998-08-17 03:30:00 +04:00
|
|
|
for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) {
|
1999-10-25 17:55:06 +04:00
|
|
|
if (shp->sh_type == SHT_SYMTAB
|
|
|
|
|| shp->sh_type == SHT_STRTAB) {
|
1998-08-17 03:30:00 +04:00
|
|
|
if (first)
|
|
|
|
printf("symbols @ 0x%lx ", (u_long)addr);
|
1998-08-23 06:48:28 +04:00
|
|
|
printf("%s%d", first ? "" : "+", (int)shp->sh_size);
|
1998-08-17 03:30:00 +04:00
|
|
|
(void)lseek(fd, shp->sh_offset, SEEK_SET);
|
|
|
|
if (read(fd, addr, shp->sh_size) != shp->sh_size) {
|
|
|
|
printf("read symbols: %s\n", strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
2000-05-22 23:15:33 +04:00
|
|
|
addr += ELF_ALIGN(shp->sh_size);
|
1998-08-17 03:30:00 +04:00
|
|
|
shp->sh_offset = off;
|
2000-05-22 23:15:33 +04:00
|
|
|
off += ELF_ALIGN(shp->sh_size);
|
1998-08-17 03:30:00 +04:00
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*esymp = addr;
|
|
|
|
|
|
|
|
*entryp = elf->e_entry;
|
|
|
|
return (0);
|
|
|
|
}
|
2000-05-22 23:15:33 +04:00
|
|
|
|
|
|
|
#undef ELF_ALIGN
|