Home | History | Annotate | Line # | Download | only in mdsetimage
      1 /*	$NetBSD: bin_nlist.c,v 1.1 2016/09/21 16:25:41 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 2002 Christopher G. Demetriou
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>>
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: bin_nlist.c,v 1.1 2016/09/21 16:25:41 christos Exp $");
     33 
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include <string.h>
     37 #include <nlist.h>
     38 #include <err.h>
     39 
     40 #include "bin.h"
     41 #include "extern.h"
     42 
     43 struct bininfo {
     44 	const char *fname;
     45 	int kfd;
     46 };
     47 
     48 #define X_MD_ROOT_IMAGE 0
     49 #define X_MD_ROOT_SIZE 1
     50 
     51 struct {
     52 	const char *name;
     53 	int	(*check)(const char *, size_t);
     54 	int	(*findoff)(const char *, size_t, u_long, size_t *, u_long);
     55 } exec_formats[] = {
     56 #ifdef NLIST_AOUT
     57 	{	"a.out",	check_aout,	findoff_aout,	},
     58 #endif
     59 #ifdef NLIST_ECOFF
     60 	{	"ECOFF",	check_ecoff,	findoff_ecoff,	},
     61 #endif
     62 #ifdef NLIST_ELF32
     63 	{	"ELF32",	check_elf32,	findoff_elf32,	},
     64 #endif
     65 #ifdef NLIST_ELF64
     66 	{	"ELF64",	check_elf64,	findoff_elf64,	},
     67 #endif
     68 #ifdef NLIST_COFF
     69 	{	"COFF",		check_coff,	findoff_coff,	},
     70 #endif
     71 };
     72 
     73 void *
     74 bin_open(int kfd, const char *kfile, const char *bfdname)
     75 {
     76 	struct bininfo *bin;
     77 
     78 	if ((bin = malloc(sizeof(*bin))) == NULL) {
     79 		warn("%s", kfile);
     80 		return NULL;
     81 	}
     82 	bin->kfd = kfd;
     83 	bin->fname = kfile;
     84 
     85 	return bin;
     86 }
     87 
     88 
     89 int
     90 bin_find_md_root(void *binp, const char *mappedfile, off_t mappedsize,
     91     unsigned long text_start, const char *root_name, const char *size_name,
     92     size_t *md_root_image_offset, size_t *md_root_size_offset,
     93     uint32_t *md_root_size, int verbose)
     94 {
     95 	struct bininfo *bin = binp;
     96 	struct nlist nl[3];
     97 	size_t i, n = sizeof exec_formats / sizeof exec_formats[0];
     98 
     99 	for (i = 0; i < n; i++) {
    100 		if ((*exec_formats[i].check)(mappedfile, mappedsize) == 0)
    101 			break;
    102 	}
    103 	if (i == n) {
    104 		warnx("%s: unknown executable format", bin->fname);
    105 		return 1;
    106 	}
    107 
    108 	if (verbose) {
    109 		fprintf(stderr, "%s is an %s binary\n", bin->fname,
    110 		    exec_formats[i].name);
    111 #ifdef NLIST_AOUT
    112 		if (text_start != (unsigned long)~0)
    113 			fprintf(stderr, "kernel text loads at 0x%lx\n",
    114 			    text_start);
    115 #endif
    116 	}
    117 
    118 	(void)memset(nl, 0, sizeof(nl));
    119 	N_NAME(&nl[X_MD_ROOT_IMAGE]) = root_name;
    120 	N_NAME(&nl[X_MD_ROOT_SIZE]) = size_name;
    121 
    122 	if (__fdnlist(bin->kfd, nl) != 0)
    123 		return 1;
    124 
    125 	if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
    126 	    nl[1].n_value, md_root_size_offset, text_start) != 0) {
    127 		warnx("couldn't find offset for %s in %s",
    128 		    nl[X_MD_ROOT_SIZE].n_name, bin->fname);
    129 		return 1;
    130 	}
    131 	if (verbose)
    132 		fprintf(stderr, "%s is at offset %#zx in %s\n",
    133 		    nl[X_MD_ROOT_SIZE].n_name, *md_root_size_offset,
    134 		    bin->fname);
    135 	memcpy(md_root_size, &mappedfile[*md_root_size_offset],
    136 	    sizeof(*md_root_size));
    137 	if (verbose)
    138 		fprintf(stderr, "%s has value %#x\n",
    139 		    nl[X_MD_ROOT_SIZE].n_name, *md_root_size);
    140 
    141 	if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
    142 	    nl[0].n_value, md_root_image_offset, text_start) != 0) {
    143 		warnx("couldn't find offset for %s in %s",
    144 		    nl[X_MD_ROOT_IMAGE].n_name, bin->fname);
    145 		return 1;
    146 	}
    147 	if (verbose)
    148 		fprintf(stderr, "%s is at offset %#zx in %s\n",
    149 			nl[X_MD_ROOT_IMAGE].n_name,
    150 			*md_root_image_offset, bin->fname);
    151 
    152 	return 0;
    153 }
    154 
    155 void
    156 bin_put_32(void *bin, off_t size, char *buf)
    157 {
    158 	uint32_t s = (uint32_t)size;
    159 	memcpy(buf, &s, sizeof(s));
    160 }
    161 
    162 void
    163 bin_close(void *bin)
    164 {
    165 }
    166 
    167 const char **
    168 bin_supported_targets(void)
    169 {
    170 	static const char *fmts[] = {
    171 #ifdef NLIST_AOUT
    172 		"aout",
    173 #endif
    174 #ifdef NLIST_ECOFF
    175 		"ecoff",
    176 #endif
    177 #ifdef NLIST_ELF32
    178 		"elf32",
    179 #endif
    180 #ifdef NLIST_ELF64
    181 		"elf64",
    182 #endif
    183 #ifdef NLIST_COFF
    184 		"coff",
    185 #endif
    186 		NULL,
    187 	};
    188 
    189 	return fmts;
    190 }
    191