2017-04-18 10:44:20 +03:00
|
|
|
/* $NetBSD: loadfile_elf32.c,v 1.40 2017/04/18 07:44:20 uwe Exp $ */
|
2001-10-31 02:51:03 +03:00
|
|
|
|
2016-12-03 12:20:55 +03:00
|
|
|
/*
|
2008-05-20 18:41:06 +04:00
|
|
|
* Copyright (c) 1997, 2008 The NetBSD Foundation, Inc.
|
2001-10-31 02:51:03 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
|
|
|
* NASA Ames Research Center and by Christos Zoulas.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-10-31 20:20:45 +03:00
|
|
|
/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
|
|
|
|
#ifndef ELFSIZE
|
|
|
|
#define ELFSIZE 32
|
|
|
|
#endif
|
|
|
|
|
2001-10-31 04:51:42 +03:00
|
|
|
#ifdef _STANDALONE
|
|
|
|
#include <lib/libsa/stand.h>
|
|
|
|
#include <lib/libkern/libkern.h>
|
2005-02-27 01:58:54 +03:00
|
|
|
#else
|
2001-10-31 04:51:42 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2005-02-27 01:58:54 +03:00
|
|
|
#include <unistd.h>
|
2001-10-31 04:51:42 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <err.h>
|
2005-02-27 01:58:54 +03:00
|
|
|
#endif
|
2001-10-31 04:51:42 +03:00
|
|
|
|
2005-02-27 01:58:54 +03:00
|
|
|
#include <sys/param.h>
|
2001-10-31 04:51:42 +03:00
|
|
|
#include <sys/exec.h>
|
|
|
|
|
|
|
|
#include "loadfile.h"
|
|
|
|
|
2001-10-31 20:20:45 +03:00
|
|
|
#if ((ELFSIZE == 32) && defined(BOOT_ELF32)) || \
|
|
|
|
((ELFSIZE == 64) && defined(BOOT_ELF64))
|
|
|
|
|
|
|
|
#define ELFROUND (ELFSIZE / 8)
|
2001-10-31 04:51:42 +03:00
|
|
|
|
2001-11-01 00:24:09 +03:00
|
|
|
#ifndef _STANDALONE
|
|
|
|
#include "byteorder.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Byte swapping may be necessary in the non-_STANDLONE case because
|
|
|
|
* we may be built with a host compiler.
|
|
|
|
*/
|
|
|
|
#define E16(f) \
|
|
|
|
f = (bo == ELFDATA2LSB) ? sa_htole16(f) : sa_htobe16(f)
|
|
|
|
#define E32(f) \
|
|
|
|
f = (bo == ELFDATA2LSB) ? sa_htole32(f) : sa_htobe32(f)
|
|
|
|
#define E64(f) \
|
|
|
|
f = (bo == ELFDATA2LSB) ? sa_htole64(f) : sa_htobe64(f)
|
|
|
|
|
|
|
|
#define I16(f) \
|
|
|
|
f = (bo == ELFDATA2LSB) ? sa_le16toh(f) : sa_be16toh(f)
|
|
|
|
#define I32(f) \
|
|
|
|
f = (bo == ELFDATA2LSB) ? sa_le32toh(f) : sa_be32toh(f)
|
|
|
|
#define I64(f) \
|
|
|
|
f = (bo == ELFDATA2LSB) ? sa_le64toh(f) : sa_be64toh(f)
|
|
|
|
|
|
|
|
static void
|
|
|
|
internalize_ehdr(Elf_Byte bo, Elf_Ehdr *ehdr)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if ELFSIZE == 32
|
|
|
|
I16(ehdr->e_type);
|
|
|
|
I16(ehdr->e_machine);
|
|
|
|
I32(ehdr->e_version);
|
|
|
|
I32(ehdr->e_entry);
|
|
|
|
I32(ehdr->e_phoff);
|
|
|
|
I32(ehdr->e_shoff);
|
|
|
|
I32(ehdr->e_flags);
|
|
|
|
I16(ehdr->e_ehsize);
|
|
|
|
I16(ehdr->e_phentsize);
|
|
|
|
I16(ehdr->e_phnum);
|
|
|
|
I16(ehdr->e_shentsize);
|
|
|
|
I16(ehdr->e_shnum);
|
|
|
|
I16(ehdr->e_shstrndx);
|
|
|
|
#elif ELFSIZE == 64
|
|
|
|
I16(ehdr->e_type);
|
|
|
|
I16(ehdr->e_machine);
|
|
|
|
I32(ehdr->e_version);
|
|
|
|
I64(ehdr->e_entry);
|
|
|
|
I64(ehdr->e_phoff);
|
|
|
|
I64(ehdr->e_shoff);
|
|
|
|
I32(ehdr->e_flags);
|
|
|
|
I16(ehdr->e_ehsize);
|
|
|
|
I16(ehdr->e_phentsize);
|
|
|
|
I16(ehdr->e_phnum);
|
|
|
|
I16(ehdr->e_shentsize);
|
|
|
|
I16(ehdr->e_shnum);
|
|
|
|
I16(ehdr->e_shstrndx);
|
|
|
|
#else
|
|
|
|
#error ELFSIZE is not 32 or 64
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
externalize_ehdr(Elf_Byte bo, Elf_Ehdr *ehdr)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if ELFSIZE == 32
|
|
|
|
E16(ehdr->e_type);
|
|
|
|
E16(ehdr->e_machine);
|
|
|
|
E32(ehdr->e_version);
|
|
|
|
E32(ehdr->e_entry);
|
|
|
|
E32(ehdr->e_phoff);
|
|
|
|
E32(ehdr->e_shoff);
|
|
|
|
E32(ehdr->e_flags);
|
|
|
|
E16(ehdr->e_ehsize);
|
|
|
|
E16(ehdr->e_phentsize);
|
|
|
|
E16(ehdr->e_phnum);
|
|
|
|
E16(ehdr->e_shentsize);
|
|
|
|
E16(ehdr->e_shnum);
|
|
|
|
E16(ehdr->e_shstrndx);
|
|
|
|
#elif ELFSIZE == 64
|
|
|
|
E16(ehdr->e_type);
|
|
|
|
E16(ehdr->e_machine);
|
|
|
|
E32(ehdr->e_version);
|
|
|
|
E64(ehdr->e_entry);
|
|
|
|
E64(ehdr->e_phoff);
|
|
|
|
E64(ehdr->e_shoff);
|
|
|
|
E32(ehdr->e_flags);
|
|
|
|
E16(ehdr->e_ehsize);
|
|
|
|
E16(ehdr->e_phentsize);
|
|
|
|
E16(ehdr->e_phnum);
|
|
|
|
E16(ehdr->e_shentsize);
|
|
|
|
E16(ehdr->e_shnum);
|
|
|
|
E16(ehdr->e_shstrndx);
|
|
|
|
#else
|
|
|
|
#error ELFSIZE is not 32 or 64
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
internalize_phdr(Elf_Byte bo, Elf_Phdr *phdr)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if ELFSIZE == 32
|
|
|
|
I32(phdr->p_type);
|
|
|
|
I32(phdr->p_offset);
|
|
|
|
I32(phdr->p_vaddr);
|
|
|
|
I32(phdr->p_paddr);
|
|
|
|
I32(phdr->p_filesz);
|
|
|
|
I32(phdr->p_memsz);
|
|
|
|
I32(phdr->p_flags);
|
|
|
|
I32(phdr->p_align);
|
|
|
|
#elif ELFSIZE == 64
|
|
|
|
I32(phdr->p_type);
|
|
|
|
I32(phdr->p_offset);
|
|
|
|
I64(phdr->p_vaddr);
|
|
|
|
I64(phdr->p_paddr);
|
|
|
|
I64(phdr->p_filesz);
|
|
|
|
I64(phdr->p_memsz);
|
|
|
|
I64(phdr->p_flags);
|
|
|
|
I64(phdr->p_align);
|
|
|
|
#else
|
|
|
|
#error ELFSIZE is not 32 or 64
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
internalize_shdr(Elf_Byte bo, Elf_Shdr *shdr)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if ELFSIZE == 32
|
|
|
|
I32(shdr->sh_name);
|
|
|
|
I32(shdr->sh_type);
|
|
|
|
I32(shdr->sh_flags);
|
|
|
|
I32(shdr->sh_addr);
|
|
|
|
I32(shdr->sh_offset);
|
|
|
|
I32(shdr->sh_size);
|
|
|
|
I32(shdr->sh_link);
|
|
|
|
I32(shdr->sh_info);
|
|
|
|
I32(shdr->sh_addralign);
|
|
|
|
I32(shdr->sh_entsize);
|
|
|
|
#elif ELFSIZE == 64
|
|
|
|
I32(shdr->sh_name);
|
|
|
|
I32(shdr->sh_type);
|
|
|
|
I64(shdr->sh_flags);
|
|
|
|
I64(shdr->sh_addr);
|
|
|
|
I64(shdr->sh_offset);
|
|
|
|
I64(shdr->sh_size);
|
|
|
|
I32(shdr->sh_link);
|
|
|
|
I32(shdr->sh_info);
|
|
|
|
I64(shdr->sh_addralign);
|
|
|
|
I64(shdr->sh_entsize);
|
|
|
|
#else
|
|
|
|
#error ELFSIZE is not 32 or 64
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
externalize_shdr(Elf_Byte bo, Elf_Shdr *shdr)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if ELFSIZE == 32
|
|
|
|
E32(shdr->sh_name);
|
|
|
|
E32(shdr->sh_type);
|
|
|
|
E32(shdr->sh_flags);
|
|
|
|
E32(shdr->sh_addr);
|
|
|
|
E32(shdr->sh_offset);
|
|
|
|
E32(shdr->sh_size);
|
|
|
|
E32(shdr->sh_link);
|
|
|
|
E32(shdr->sh_info);
|
|
|
|
E32(shdr->sh_addralign);
|
|
|
|
E32(shdr->sh_entsize);
|
|
|
|
#elif ELFSIZE == 64
|
|
|
|
E32(shdr->sh_name);
|
|
|
|
E32(shdr->sh_type);
|
|
|
|
E64(shdr->sh_flags);
|
|
|
|
E64(shdr->sh_addr);
|
|
|
|
E64(shdr->sh_offset);
|
|
|
|
E64(shdr->sh_size);
|
|
|
|
E32(shdr->sh_link);
|
|
|
|
E32(shdr->sh_info);
|
|
|
|
E64(shdr->sh_addralign);
|
|
|
|
E64(shdr->sh_entsize);
|
|
|
|
#else
|
|
|
|
#error ELFSIZE is not 32 or 64
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else /* _STANDALONE */
|
|
|
|
/*
|
|
|
|
* Byte swapping is never necessary in the _STANDALONE case because
|
|
|
|
* we are being built with the target compiler.
|
|
|
|
*/
|
|
|
|
#define internalize_ehdr(bo, ehdr) /* nothing */
|
|
|
|
#define externalize_ehdr(bo, ehdr) /* nothing */
|
|
|
|
|
|
|
|
#define internalize_phdr(bo, phdr) /* nothing */
|
|
|
|
|
|
|
|
#define internalize_shdr(bo, shdr) /* nothing */
|
|
|
|
#define externalize_shdr(bo, shdr) /* nothing */
|
|
|
|
#endif /* _STANDALONE */
|
|
|
|
|
2016-12-03 12:20:55 +03:00
|
|
|
#define IS_TEXT(p) (p.p_flags & PF_X)
|
|
|
|
#define IS_DATA(p) (p.p_flags & PF_W)
|
|
|
|
#define IS_BSS(p) (p.p_filesz < p.p_memsz)
|
|
|
|
|
2017-01-06 12:14:36 +03:00
|
|
|
/*
|
|
|
|
* Load the ELF binary into memory. Layout of the memory:
|
|
|
|
* +-----------------+------------+-----------------+-----------------+
|
|
|
|
* | KERNEL SEGMENTS | ELF HEADER | SECTION HEADERS | SYMBOL SECTIONS |
|
|
|
|
* +-----------------+------------+-----------------+-----------------+
|
|
|
|
* The KERNEL SEGMENTS start address is fixed by the segments themselves. We
|
|
|
|
* then map the rest by increasing maxp.
|
|
|
|
*
|
|
|
|
* The offsets of the SYMBOL SECTIONS are relative to the start address of the
|
|
|
|
* ELF HEADER. The shdr offset of ELF HEADER points to SECTION HEADERS.
|
|
|
|
*
|
|
|
|
* We just give the kernel a pointer to the ELF HEADER, which is enough for it
|
|
|
|
* to find the location and number of symbols by itself later.
|
|
|
|
*/
|
|
|
|
|
2001-10-31 04:51:42 +03:00
|
|
|
int
|
2007-12-29 20:54:41 +03:00
|
|
|
ELFNAMEEND(loadfile)(int fd, Elf_Ehdr *elf, u_long *marks, int flags)
|
2001-10-31 02:51:03 +03:00
|
|
|
{
|
|
|
|
Elf_Shdr *shp;
|
2001-11-09 21:31:08 +03:00
|
|
|
Elf_Phdr *phdr;
|
2001-10-31 02:51:03 +03:00
|
|
|
int i, j;
|
2003-09-01 02:40:13 +04:00
|
|
|
ssize_t sz;
|
2001-10-31 02:51:03 +03:00
|
|
|
int first;
|
2010-09-16 04:26:49 +04:00
|
|
|
Elf_Addr shpp;
|
|
|
|
Elf_Addr minp = ~0, maxp = 0, pos = 0, elfp = 0;
|
2011-02-18 00:15:31 +03:00
|
|
|
u_long offset = marks[MARK_START];
|
|
|
|
ssize_t nr;
|
|
|
|
struct __packed {
|
2016-12-03 12:20:55 +03:00
|
|
|
Elf_Nhdr nh;
|
|
|
|
uint8_t name[ELF_NOTE_NETBSD_NAMESZ + 1];
|
|
|
|
uint8_t desc[ELF_NOTE_NETBSD_DESCSZ];
|
2011-02-18 00:15:31 +03:00
|
|
|
} note;
|
|
|
|
char *shstr = NULL;
|
2013-11-27 21:33:03 +04:00
|
|
|
size_t shstrsz = 0;
|
2011-02-18 00:15:31 +03:00
|
|
|
int boot_load_ctf = 1;
|
|
|
|
|
|
|
|
/* some ports dont use the offset */
|
|
|
|
(void)&offset;
|
|
|
|
|
2016-08-31 19:22:37 +03:00
|
|
|
/* have not seen a data segment so far */
|
|
|
|
marks[MARK_DATA] = 0;
|
|
|
|
|
2011-02-18 00:15:31 +03:00
|
|
|
internalize_ehdr(elf->e_ident[EI_DATA], elf);
|
|
|
|
|
|
|
|
sz = elf->e_phnum * sizeof(Elf_Phdr);
|
|
|
|
phdr = ALLOC(sz);
|
|
|
|
|
|
|
|
if (lseek(fd, elf->e_phoff, SEEK_SET) == -1) {
|
|
|
|
WARN(("lseek phdr"));
|
|
|
|
goto freephdr;
|
|
|
|
}
|
|
|
|
nr = read(fd, phdr, sz);
|
|
|
|
if (nr == -1) {
|
|
|
|
WARN(("read program headers"));
|
|
|
|
goto freephdr;
|
|
|
|
}
|
|
|
|
if (nr != sz) {
|
|
|
|
errno = EIO;
|
|
|
|
WARN(("read program headers"));
|
|
|
|
goto freephdr;
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:44:48 +03:00
|
|
|
first = 1;
|
|
|
|
for (i = 0; i < elf->e_phnum; i++) {
|
2011-02-18 00:15:31 +03:00
|
|
|
internalize_phdr(elf->e_ident[EI_DATA], &phdr[i]);
|
2006-04-03 18:08:41 +04:00
|
|
|
|
|
|
|
#ifndef MD_LOADSEG /* Allow processor ABI specific segment loads */
|
|
|
|
#define MD_LOADSEG(a) /*CONSTCOND*/0
|
|
|
|
#endif
|
2011-02-18 00:15:31 +03:00
|
|
|
if (MD_LOADSEG(&phdr[i]))
|
|
|
|
goto loadseg;
|
2006-04-03 18:08:41 +04:00
|
|
|
|
2011-02-18 00:15:31 +03:00
|
|
|
if (phdr[i].p_type != PT_LOAD ||
|
|
|
|
(phdr[i].p_flags & (PF_W|PF_X)) == 0)
|
|
|
|
continue;
|
2001-10-31 02:51:03 +03:00
|
|
|
|
2011-02-18 00:15:31 +03:00
|
|
|
if ((IS_TEXT(phdr[i]) && (flags & LOAD_TEXT)) ||
|
|
|
|
(IS_DATA(phdr[i]) && (flags & LOAD_DATA))) {
|
|
|
|
loadseg:
|
2016-12-03 12:20:55 +03:00
|
|
|
/* XXX: Assume first address is lowest */
|
2011-02-18 00:15:31 +03:00
|
|
|
if (marks[MARK_DATA] == 0 && IS_DATA(phdr[i]))
|
|
|
|
marks[MARK_DATA] = LOADADDR(phdr[i].p_vaddr);
|
|
|
|
|
|
|
|
/* Read in segment. */
|
|
|
|
PROGRESS(("%s%lu", first ? "" : "+",
|
|
|
|
(u_long)phdr[i].p_filesz));
|
|
|
|
|
|
|
|
if (lseek(fd, phdr[i].p_offset, SEEK_SET) == -1) {
|
|
|
|
WARN(("lseek text"));
|
|
|
|
goto freephdr;
|
|
|
|
}
|
|
|
|
nr = READ(fd, phdr[i].p_vaddr, phdr[i].p_filesz);
|
|
|
|
if (nr == -1) {
|
|
|
|
WARN(("read text error"));
|
|
|
|
goto freephdr;
|
|
|
|
}
|
|
|
|
if (nr != (ssize_t)phdr[i].p_filesz) {
|
|
|
|
errno = EIO;
|
|
|
|
WARN(("read text"));
|
|
|
|
goto freephdr;
|
|
|
|
}
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
if ((IS_TEXT(phdr[i]) && (flags & (LOAD_TEXT|COUNT_TEXT))) ||
|
2016-12-03 12:20:55 +03:00
|
|
|
(IS_DATA(phdr[i]) && (flags & (LOAD_DATA|COUNT_DATA)))) {
|
2017-02-23 15:13:59 +03:00
|
|
|
/* XXX: Assume first address is lowest */
|
|
|
|
if (marks[MARK_DATA] == 0 && IS_DATA(phdr[i]))
|
|
|
|
marks[MARK_DATA] = LOADADDR(phdr[i].p_vaddr);
|
|
|
|
|
2011-02-18 00:15:31 +03:00
|
|
|
pos = phdr[i].p_vaddr;
|
|
|
|
if (minp > pos)
|
|
|
|
minp = pos;
|
|
|
|
pos += phdr[i].p_filesz;
|
|
|
|
if (maxp < pos)
|
|
|
|
maxp = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Zero out bss. */
|
|
|
|
if (IS_BSS(phdr[i]) && (flags & LOAD_BSS)) {
|
|
|
|
PROGRESS(("+%lu",
|
|
|
|
(u_long)(phdr[i].p_memsz - phdr[i].p_filesz)));
|
|
|
|
BZERO((phdr[i].p_vaddr + phdr[i].p_filesz),
|
|
|
|
phdr[i].p_memsz - phdr[i].p_filesz);
|
|
|
|
}
|
|
|
|
if (IS_BSS(phdr[i]) && (flags & (LOAD_BSS|COUNT_BSS))) {
|
|
|
|
pos += phdr[i].p_memsz - phdr[i].p_filesz;
|
|
|
|
if (maxp < pos)
|
|
|
|
maxp = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEALLOC(phdr, sz);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the ELF and section headers.
|
|
|
|
*/
|
|
|
|
maxp = roundup(maxp, ELFROUND);
|
|
|
|
if (flags & (LOAD_HDR|COUNT_HDR)) {
|
|
|
|
elfp = maxp;
|
|
|
|
maxp += sizeof(Elf_Ehdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & (LOAD_SYM|COUNT_SYM)) {
|
|
|
|
if (lseek(fd, elf->e_shoff, SEEK_SET) == -1) {
|
|
|
|
WARN(("lseek section headers"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
sz = elf->e_shnum * sizeof(Elf_Shdr);
|
|
|
|
|
|
|
|
shp = ALLOC(sz);
|
|
|
|
|
|
|
|
nr = read(fd, shp, sz);
|
|
|
|
if (nr == -1) {
|
|
|
|
WARN(("read section headers"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
|
|
|
if (nr != sz) {
|
|
|
|
errno = EIO;
|
|
|
|
WARN(("read section headers"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
|
|
|
|
|
|
|
shpp = maxp;
|
|
|
|
maxp += roundup(sz, ELFROUND);
|
2001-10-31 02:51:03 +03:00
|
|
|
|
2001-11-01 00:24:09 +03:00
|
|
|
#ifndef _STANDALONE
|
2011-02-18 00:15:31 +03:00
|
|
|
/* Internalize the section headers. */
|
|
|
|
for (i = 0; i < elf->e_shnum; i++)
|
|
|
|
internalize_shdr(elf->e_ident[EI_DATA], &shp[i]);
|
2016-12-03 12:20:55 +03:00
|
|
|
#endif
|
2001-11-01 00:24:09 +03:00
|
|
|
|
2011-02-18 00:15:31 +03:00
|
|
|
/*
|
|
|
|
* First load the section names section.
|
|
|
|
*/
|
2017-04-17 21:06:30 +03:00
|
|
|
if (boot_load_ctf && (elf->e_shstrndx != SHN_UNDEF)) {
|
2017-02-23 15:13:05 +03:00
|
|
|
Elf_Off shstroff = shp[elf->e_shstrndx].sh_offset;
|
|
|
|
shstrsz = shp[elf->e_shstrndx].sh_size;
|
2011-02-18 00:15:31 +03:00
|
|
|
if (flags & LOAD_SYM) {
|
2017-02-23 15:13:05 +03:00
|
|
|
if (lseek(fd, shstroff, SEEK_SET) == -1) {
|
2011-02-18 00:15:31 +03:00
|
|
|
WARN(("lseek symbols"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
2017-02-23 15:13:05 +03:00
|
|
|
nr = READ(fd, maxp, shstrsz);
|
2011-02-18 00:15:31 +03:00
|
|
|
if (nr == -1) {
|
|
|
|
WARN(("read symbols"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
2017-02-23 15:13:05 +03:00
|
|
|
if (nr != (ssize_t)shstrsz) {
|
2011-02-18 00:15:31 +03:00
|
|
|
errno = EIO;
|
|
|
|
WARN(("read symbols"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
2017-02-23 15:13:05 +03:00
|
|
|
}
|
2011-02-18 00:15:31 +03:00
|
|
|
|
2017-02-23 15:13:05 +03:00
|
|
|
shstr = ALLOC(shstrsz);
|
|
|
|
if (lseek(fd, shstroff, SEEK_SET) == -1) {
|
|
|
|
WARN(("lseek symbols"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
|
|
|
nr = read(fd, shstr, shstrsz);
|
|
|
|
if (nr == -1) {
|
|
|
|
WARN(("read symbols"));
|
|
|
|
goto freeshp;
|
2011-02-18 00:15:31 +03:00
|
|
|
}
|
|
|
|
shp[elf->e_shstrndx].sh_offset = maxp - elfp;
|
2017-02-23 15:13:05 +03:00
|
|
|
maxp += roundup(shstrsz, ELFROUND);
|
2011-02-18 00:15:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now load the symbol sections themselves. Make sure
|
|
|
|
* the sections are aligned. Don't bother with any
|
|
|
|
* string table that isn't referenced by a symbol
|
|
|
|
* table.
|
|
|
|
*/
|
2017-04-17 20:44:48 +03:00
|
|
|
first = 1;
|
2017-04-17 21:06:30 +03:00
|
|
|
for (i = 1; i < elf->e_shnum; i++) {
|
2015-07-25 10:06:11 +03:00
|
|
|
if (i == elf->e_shstrndx) {
|
|
|
|
/* already loaded this section */
|
|
|
|
continue;
|
2011-02-18 00:15:31 +03:00
|
|
|
}
|
|
|
|
switch (shp[i].sh_type) {
|
|
|
|
case SHT_PROGBITS:
|
2015-07-25 10:06:11 +03:00
|
|
|
if (boot_load_ctf && shstr) {
|
2011-02-18 00:15:31 +03:00
|
|
|
/* got a CTF section? */
|
2017-04-18 10:44:20 +03:00
|
|
|
if (strncmp(&shstr[shp[i].sh_name],
|
|
|
|
".SUNW_ctf", 10) == 0) {
|
2015-07-25 10:06:11 +03:00
|
|
|
goto havesym;
|
2011-02-18 00:15:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not loading this, so zero out the offset. */
|
|
|
|
shp[i].sh_offset = 0;
|
2015-07-25 10:06:11 +03:00
|
|
|
break;
|
2011-02-18 00:15:31 +03:00
|
|
|
case SHT_STRTAB:
|
2017-04-17 21:06:30 +03:00
|
|
|
for (j = 1; j < elf->e_shnum; j++)
|
2011-02-18 00:15:31 +03:00
|
|
|
if (shp[j].sh_type == SHT_SYMTAB &&
|
|
|
|
shp[j].sh_link == (unsigned int)i)
|
|
|
|
goto havesym;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
/* Not loading this, so zero out the offset. */
|
|
|
|
shp[i].sh_offset = 0;
|
|
|
|
break;
|
|
|
|
havesym:
|
|
|
|
case SHT_SYMTAB:
|
|
|
|
if (flags & LOAD_SYM) {
|
|
|
|
PROGRESS(("%s%ld", first ? " [" : "+",
|
|
|
|
(u_long)shp[i].sh_size));
|
|
|
|
if (lseek(fd, shp[i].sh_offset,
|
|
|
|
SEEK_SET) == -1) {
|
|
|
|
WARN(("lseek symbols"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
|
|
|
nr = READ(fd, maxp, shp[i].sh_size);
|
|
|
|
if (nr == -1) {
|
|
|
|
WARN(("read symbols"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
|
|
|
if (nr != (ssize_t)shp[i].sh_size) {
|
|
|
|
errno = EIO;
|
|
|
|
WARN(("read symbols"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shp[i].sh_offset = maxp - elfp;
|
|
|
|
maxp += roundup(shp[i].sh_size, ELFROUND);
|
|
|
|
first = 0;
|
|
|
|
break;
|
|
|
|
case SHT_NOTE:
|
|
|
|
if ((flags & LOAD_NOTE) == 0)
|
|
|
|
break;
|
|
|
|
if (shp[i].sh_size < sizeof(note)) {
|
|
|
|
shp[i].sh_offset = 0;
|
2008-05-20 18:41:06 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-09-26 00:59:38 +04:00
|
|
|
if (lseek(fd, shp[i].sh_offset, SEEK_SET)
|
|
|
|
== -1) {
|
2008-05-20 18:41:06 +04:00
|
|
|
WARN(("lseek note"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
|
|
|
nr = read(fd, ¬e, sizeof(note));
|
|
|
|
if (nr == -1) {
|
|
|
|
WARN(("read note"));
|
|
|
|
goto freeshp;
|
|
|
|
}
|
2008-09-26 00:59:38 +04:00
|
|
|
if (note.nh.n_namesz ==
|
|
|
|
ELF_NOTE_NETBSD_NAMESZ &&
|
|
|
|
note.nh.n_descsz ==
|
|
|
|
ELF_NOTE_NETBSD_DESCSZ &&
|
|
|
|
note.nh.n_type ==
|
|
|
|
ELF_NOTE_TYPE_NETBSD_TAG &&
|
2008-05-20 18:41:06 +04:00
|
|
|
memcmp(note.name, ELF_NOTE_NETBSD_NAME,
|
2008-09-26 00:59:38 +04:00
|
|
|
sizeof(note.name)) == 0) {
|
2015-07-25 10:06:11 +03:00
|
|
|
memcpy(&netbsd_version, ¬e.desc,
|
|
|
|
sizeof(netbsd_version));
|
2008-05-20 18:41:06 +04:00
|
|
|
}
|
|
|
|
shp[i].sh_offset = 0;
|
|
|
|
break;
|
2001-10-31 02:51:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags & LOAD_SYM) {
|
2001-11-01 00:24:09 +03:00
|
|
|
#ifndef _STANDALONE
|
|
|
|
/* Externalize the section headers. */
|
|
|
|
for (i = 0; i < elf->e_shnum; i++)
|
|
|
|
externalize_shdr(elf->e_ident[EI_DATA],
|
|
|
|
&shp[i]);
|
2016-12-03 12:20:55 +03:00
|
|
|
#endif
|
2001-10-31 02:51:03 +03:00
|
|
|
BCOPY(shp, shpp, sz);
|
|
|
|
|
|
|
|
if (first == 0)
|
|
|
|
PROGRESS(("]"));
|
|
|
|
}
|
2006-01-25 21:27:23 +03:00
|
|
|
DEALLOC(shp, sz);
|
2017-04-18 09:39:54 +03:00
|
|
|
if (shstr)
|
|
|
|
DEALLOC(shstr, shstrsz);
|
2010-03-13 00:43:10 +03:00
|
|
|
}
|
2001-10-31 02:51:03 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Frob the copied ELF header to give information relative
|
|
|
|
* to elfp.
|
|
|
|
*/
|
|
|
|
if (flags & LOAD_HDR) {
|
|
|
|
elf->e_phoff = 0;
|
|
|
|
elf->e_shoff = sizeof(Elf_Ehdr);
|
|
|
|
elf->e_phentsize = 0;
|
|
|
|
elf->e_phnum = 0;
|
2001-11-01 00:24:09 +03:00
|
|
|
externalize_ehdr(elf->e_ident[EI_DATA], elf);
|
2001-10-31 02:51:03 +03:00
|
|
|
BCOPY(elf, elfp, sizeof(*elf));
|
2001-11-01 00:24:09 +03:00
|
|
|
internalize_ehdr(elf->e_ident[EI_DATA], elf);
|
2001-10-31 02:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
marks[MARK_START] = LOADADDR(minp);
|
|
|
|
marks[MARK_ENTRY] = LOADADDR(elf->e_entry);
|
|
|
|
/*
|
|
|
|
* Since there can be more than one symbol section in the code
|
|
|
|
* and we need to find strtab too in order to do anything
|
|
|
|
* useful with the symbols, we just pass the whole elf
|
|
|
|
* header back and we let the kernel debugger find the
|
|
|
|
* location and number of symbols by itself.
|
|
|
|
*/
|
|
|
|
marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */
|
|
|
|
marks[MARK_SYM] = LOADADDR(elfp);
|
|
|
|
marks[MARK_END] = LOADADDR(maxp);
|
|
|
|
return 0;
|
2016-12-03 12:20:55 +03:00
|
|
|
|
2005-07-14 06:36:49 +04:00
|
|
|
freephdr:
|
2006-01-25 21:27:23 +03:00
|
|
|
DEALLOC(phdr, sz);
|
2005-07-14 06:36:49 +04:00
|
|
|
return 1;
|
2016-12-03 12:20:55 +03:00
|
|
|
|
2005-07-14 06:36:49 +04:00
|
|
|
freeshp:
|
2006-01-25 21:27:23 +03:00
|
|
|
DEALLOC(shp, sz);
|
2005-07-14 06:36:49 +04:00
|
|
|
return 1;
|
2001-10-31 02:51:03 +03:00
|
|
|
}
|
2001-10-31 04:51:42 +03:00
|
|
|
|
2008-09-26 00:59:38 +04:00
|
|
|
#ifdef TEST
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
u_int32_t netbsd_version;
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
u_long marks[MARK_MAX];
|
|
|
|
Elf_Ehdr elf;
|
|
|
|
if (argc != 2) {
|
|
|
|
(void)fprintf(stderr, "Usage: %s <file>\n", getprogname());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ((fd = open(argv[1], O_RDONLY)) == -1)
|
|
|
|
err(1, "Can't open `%s'", argv[1]);
|
|
|
|
if (read(fd, &elf, sizeof(elf)) != sizeof(elf))
|
|
|
|
err(1, "Can't read `%s'", argv[1]);
|
|
|
|
memset(marks, 0, sizeof(marks));
|
|
|
|
marks[MARK_START] = (u_long)malloc(2LL * 1024 * 2024 * 1024);
|
|
|
|
ELFNAMEEND(loadfile)(fd, &elf, marks, LOAD_ALL);
|
|
|
|
printf("%d\n", netbsd_version);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-31 20:20:45 +03:00
|
|
|
#endif /* (ELFSIZE == 32 && BOOT_ELF32) || (ELFSIZE == 64 && BOOT_ELF64) */
|