xref: /openbsd-src/usr.sbin/config/exec_elf.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: exec_elf.c,v 1.8 2003/06/28 04:55:07 deraadt Exp $ */
2 
3 /*
4  * Copyright (c) 1999 Mats O Jansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef LINT
28 static char rcsid[] = "$OpenBSD: exec_elf.c,v 1.8 2003/06/28 04:55:07 deraadt Exp $";
29 #endif
30 
31 #include <err.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <nlist.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <sys/exec.h>
39 #include <sys/exec_elf.h>
40 #include <sys/types.h>
41 
42 #include "ukc.h"
43 
44 caddr_t		ptr, rest, pre;
45 Elf_Ehdr	elf_ex;
46 Elf_Phdr	*elf_phdr;
47 Elf_Shdr	*elf_shdr;
48 char		*elf_total;
49 char		*elf_shstrtab;
50 off_t		elf_size;
51 
52 caddr_t		elf_adjust(caddr_t);
53 caddr_t		elf_readjust(caddr_t);
54 int		elf_check(char *);
55 void		elf_loadkernel(char *);
56 void		elf_savekernel(char *);
57 
58 caddr_t
59 elf_adjust(caddr_t x)
60 {
61 	int i;
62 	Elf_Shdr *s;
63 	unsigned long y = 0;
64 
65 	s = elf_shdr;
66 
67 	for (i = 0; i < elf_ex.e_shnum; i++) {
68 		if (s[i].sh_addr == 0)
69 			continue;
70 		if (((unsigned long)x >= s[i].sh_addr) &&
71 		    ((unsigned long)x < (s[i].sh_addr+s[i].sh_size))) {
72 			y = (unsigned long)&elf_total[(unsigned long)x -
73 			    s[i].sh_addr + s[i].sh_offset];
74 			break;
75 		}
76 	}
77 
78 	return((caddr_t)y);
79 }
80 
81 caddr_t
82 elf_readjust(caddr_t x)
83 {
84 	int i;
85 	Elf_Shdr *s;
86 	unsigned long y = 0;
87 
88 	s = elf_shdr;
89 
90 	for (i = 0; i < elf_ex.e_shnum; i++) {
91 		if (s[i].sh_addr == 0)
92 			continue;
93 		if (((x - elf_total) >= s[i].sh_offset) &&
94 		    ((x - elf_total) <= (s[i].sh_offset + s[i].sh_size)))
95 			y = (unsigned long)x - (unsigned long)elf_total +
96 			    (unsigned long)s[i].sh_addr - s[i].sh_offset;
97 	}
98 
99 	return((caddr_t)y);
100 }
101 
102 int
103 elf_check(char *file)
104 {
105 	int fd, ret = 1;
106 
107 	if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0)
108 		return (0);
109 
110 	if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex))
111 		ret = 0;
112 
113 	if (ret) {
114 		if (!IS_ELF(elf_ex))
115 			ret = 0;
116 	}
117 
118 	close(fd);
119 	return (ret);
120 }
121 
122 void
123 elf_loadkernel(char *file)
124 {
125 	int fd;
126 	Elf_Phdr *p;
127 	Elf_Shdr *s;
128 
129 	if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0)
130 		err(1, "%s", file);
131 
132 	if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex))
133 		errx(1, "can't read elf header");
134 
135 	if (!IS_ELF(elf_ex))
136 		errx(1, "bad elf magic");
137 
138 	elf_size = lseek(fd, 0L, SEEK_END);
139 	(void)lseek(fd, 0L, SEEK_SET);
140 	elf_total = malloc(elf_size);
141 
142 	if (read(fd, elf_total, elf_size) != elf_size)
143 		errx(1, "can't read elf kernel");
144 
145 	elf_phdr = (Elf_Phdr *)&elf_total[elf_ex.e_phoff];
146 	elf_shdr = (Elf_Shdr *)&elf_total[elf_ex.e_shoff];
147 
148 	p = elf_phdr;
149 	s = elf_shdr;
150 
151 	elf_shstrtab = &elf_total[elf_shdr[elf_ex.e_shstrndx].sh_offset];
152 
153 	close(fd);
154 }
155 
156 void
157 elf_savekernel(char *outfile)
158 {
159 	int fd;
160 
161 	if ((fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0)
162 		err(1, "%s", outfile);
163 
164 	if (write(fd, elf_total, elf_size) != elf_size)
165 		errx(1, "can't write file %s", outfile);
166 
167 	close(fd);
168 }
169