xref: /openbsd-src/usr.sbin/config/exec_elf.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: exec_elf.c,v 1.9 2003/09/26 17:00:27 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.9 2003/09/26 17:00:27 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 #include "config.h"
44 
45 caddr_t		ptr, rest, pre;
46 Elf_Ehdr	elf_ex;
47 Elf_Phdr	*elf_phdr;
48 Elf_Shdr	*elf_shdr;
49 char		*elf_total;
50 char		*elf_shstrtab;
51 off_t		elf_size;
52 
53 caddr_t		elf_adjust(caddr_t);
54 caddr_t		elf_readjust(caddr_t);
55 int		elf_check(char *);
56 void		elf_loadkernel(char *);
57 void		elf_savekernel(char *);
58 
59 caddr_t
60 elf_adjust(caddr_t x)
61 {
62 	int i;
63 	Elf_Shdr *s;
64 	unsigned long y = 0;
65 
66 	s = elf_shdr;
67 
68 	for (i = 0; i < elf_ex.e_shnum; i++) {
69 		if (s[i].sh_addr == 0)
70 			continue;
71 		if (((unsigned long)x >= s[i].sh_addr) &&
72 		    ((unsigned long)x < (s[i].sh_addr+s[i].sh_size))) {
73 			y = (unsigned long)&elf_total[(unsigned long)x -
74 			    s[i].sh_addr + s[i].sh_offset];
75 			break;
76 		}
77 	}
78 
79 	return((caddr_t)y);
80 }
81 
82 caddr_t
83 elf_readjust(caddr_t x)
84 {
85 	int i;
86 	Elf_Shdr *s;
87 	unsigned long y = 0;
88 
89 	s = elf_shdr;
90 
91 	for (i = 0; i < elf_ex.e_shnum; i++) {
92 		if (s[i].sh_addr == 0)
93 			continue;
94 		if (((x - elf_total) >= s[i].sh_offset) &&
95 		    ((x - elf_total) <= (s[i].sh_offset + s[i].sh_size)))
96 			y = (unsigned long)x - (unsigned long)elf_total +
97 			    (unsigned long)s[i].sh_addr - s[i].sh_offset;
98 	}
99 
100 	return((caddr_t)y);
101 }
102 
103 int
104 elf_check(char *file)
105 {
106 	int fd, ret = 1;
107 
108 	if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0)
109 		return (0);
110 
111 	if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex))
112 		ret = 0;
113 
114 	if (ret) {
115 		if (!IS_ELF(elf_ex))
116 			ret = 0;
117 	}
118 
119 	close(fd);
120 	return (ret);
121 }
122 
123 void
124 elf_loadkernel(char *file)
125 {
126 	int fd;
127 	Elf_Phdr *p;
128 	Elf_Shdr *s;
129 
130 	if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0)
131 		err(1, "%s", file);
132 
133 	if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex))
134 		errx(1, "can't read elf header");
135 
136 	if (!IS_ELF(elf_ex))
137 		errx(1, "bad elf magic");
138 
139 	elf_size = lseek(fd, 0L, SEEK_END);
140 	(void)lseek(fd, 0L, SEEK_SET);
141 	elf_total = emalloc(elf_size);
142 
143 	if (read(fd, elf_total, elf_size) != elf_size)
144 		errx(1, "can't read elf kernel");
145 
146 	elf_phdr = (Elf_Phdr *)&elf_total[elf_ex.e_phoff];
147 	elf_shdr = (Elf_Shdr *)&elf_total[elf_ex.e_shoff];
148 
149 	p = elf_phdr;
150 	s = elf_shdr;
151 
152 	elf_shstrtab = &elf_total[elf_shdr[elf_ex.e_shstrndx].sh_offset];
153 
154 	close(fd);
155 }
156 
157 void
158 elf_savekernel(char *outfile)
159 {
160 	int fd;
161 
162 	if ((fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0)
163 		err(1, "%s", outfile);
164 
165 	if (write(fd, elf_total, elf_size) != elf_size)
166 		errx(1, "can't write file %s", outfile);
167 
168 	close(fd);
169 }
170