xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/libelf_ehdr.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: libelf_ehdr.c,v 1.3 2016/02/20 02:43:42 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006,2008 Joseph Koshy
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
31 #endif
32 
33 #include <sys/cdefs.h>
34 
35 #include <assert.h>
36 #include <gelf.h>
37 #include <libelf.h>
38 #include <stdlib.h>
39 
40 #include "_libelf.h"
41 
42 __RCSID("$NetBSD: libelf_ehdr.c,v 1.3 2016/02/20 02:43:42 christos Exp $");
43 ELFTC_VCSID("Id: libelf_ehdr.c 3174 2015-03-27 17:13:41Z emaste ");
44 
45 /*
46  * Retrieve counts for sections, phdrs and the section string table index
47  * from section header #0 of the ELF object.
48  */
49 static int
50 _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum,
51     uint16_t strndx)
52 {
53 	Elf_Scn *scn;
54 	size_t fsz;
55 	int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s,
56 	    size_t _c, int _swap);
57 	uint32_t shtype;
58 
59 	assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
60 
61 	fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1);
62 	assert(fsz > 0);
63 
64 	if (e->e_rawsize < shoff + fsz) { /* raw file too small */
65 		LIBELF_SET_ERROR(HEADER, 0);
66 		return (0);
67 	}
68 
69 	if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL)
70 		return (0);
71 
72 	if (shoff > SSIZE_MAX) {
73 		LIBELF_SET_ERROR(HEADER, 0);
74 		return (0);
75 	}
76 
77 	xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
78 	(*xlator)((void *) &scn->s_shdr, sizeof(scn->s_shdr),
79 	    (unsigned char *) e->e_rawfile + shoff, (size_t) 1,
80 	    e->e_byteorder != _libelf_host_byteorder());
81 
82 #define	GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \
83 		scn->s_shdr.s_shdr64.M)
84 
85 	if (GET_SHDR_MEMBER(sh_size) > UINT_MAX) {
86 		LIBELF_SET_ERROR(HEADER, 0);
87 		return (0);
88 	}
89 
90 	if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL) {
91 		LIBELF_SET_ERROR(SECTION, 0);
92 		return (0);
93 	}
94 
95 	e->e_u.e_elf.e_nscn = (size_t) GET_SHDR_MEMBER(sh_size);
96 	e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum :
97 	    GET_SHDR_MEMBER(sh_info);
98 	e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx :
99 	    GET_SHDR_MEMBER(sh_link);
100 #undef	GET_SHDR_MEMBER
101 
102 	return (1);
103 }
104 
105 #define	EHDR_INIT(E,SZ)	 do {						\
106 		Elf##SZ##_Ehdr *eh = (E);				\
107 		eh->e_ident[EI_MAG0] = ELFMAG0;				\
108 		eh->e_ident[EI_MAG1] = ELFMAG1;				\
109 		eh->e_ident[EI_MAG2] = ELFMAG2;				\
110 		eh->e_ident[EI_MAG3] = ELFMAG3;				\
111 		eh->e_ident[EI_CLASS] = ELFCLASS##SZ;			\
112 		eh->e_ident[EI_DATA]  = ELFDATANONE;			\
113 		eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version) & 0xFFU; \
114 		eh->e_machine = EM_NONE;				\
115 		eh->e_type    = ELF_K_NONE;				\
116 		eh->e_version = LIBELF_PRIVATE(version);		\
117 	} while (/*CONSTCOND*/0)
118 
119 void *
120 _libelf_ehdr(Elf *e, int ec, int allocate)
121 {
122 	void *ehdr;
123 	size_t fsz, msz;
124 	uint16_t phnum, shnum, strndx;
125 	uint64_t shoff;
126 	int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s,
127 	    size_t _c, int _swap);
128 
129 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
130 
131 	if (e == NULL || e->e_kind != ELF_K_ELF) {
132 		LIBELF_SET_ERROR(ARGUMENT, 0);
133 		return (NULL);
134 	}
135 
136 	if (e->e_class != ELFCLASSNONE && e->e_class != ec) {
137 		LIBELF_SET_ERROR(CLASS, 0);
138 		return (NULL);
139 	}
140 
141 	if (e->e_version != EV_CURRENT) {
142 		LIBELF_SET_ERROR(VERSION, 0);
143 		return (NULL);
144 	}
145 
146 	if (e->e_class == ELFCLASSNONE)
147 		e->e_class = ec;
148 
149 	if (ec == ELFCLASS32)
150 		ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32;
151 	else
152 		ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64;
153 
154 	if (ehdr != NULL)	/* already have a translated ehdr */
155 		return (ehdr);
156 
157 	fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
158 	assert(fsz > 0);
159 
160 	if (e->e_cmd != ELF_C_WRITE && e->e_rawsize < fsz) {
161 		LIBELF_SET_ERROR(HEADER, 0);
162 		return (NULL);
163 	}
164 
165 	msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT);
166 
167 	assert(msz > 0);
168 
169 	if ((ehdr = calloc((size_t) 1, msz)) == NULL) {
170 		LIBELF_SET_ERROR(RESOURCE, 0);
171 		return (NULL);
172 	}
173 
174 	if (ec == ELFCLASS32) {
175 		e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr;
176 		EHDR_INIT(ehdr,32);
177 	} else {
178 		e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr;
179 		EHDR_INIT(ehdr,64);
180 	}
181 
182 	if (allocate)
183 		e->e_flags |= ELF_F_DIRTY;
184 
185 	if (e->e_cmd == ELF_C_WRITE)
186 		return (ehdr);
187 
188 	xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec);
189 	(*xlator)((void *)ehdr, msz, e->e_rawfile, (size_t) 1,
190 	    e->e_byteorder != _libelf_host_byteorder());
191 
192 	/*
193 	 * If extended numbering is being used, read the correct
194 	 * number of sections and program header entries.
195 	 */
196 	if (ec == ELFCLASS32) {
197 		phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
198 		shnum = ((Elf32_Ehdr *) ehdr)->e_shnum;
199 		shoff = ((Elf32_Ehdr *) ehdr)->e_shoff;
200 		strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx;
201 	} else {
202 		phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
203 		shnum = ((Elf64_Ehdr *) ehdr)->e_shnum;
204 		shoff = ((Elf64_Ehdr *) ehdr)->e_shoff;
205 		strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx;
206 	}
207 
208 	if (shnum >= SHN_LORESERVE ||
209 	    (shoff == 0LL && (shnum != 0 || phnum == PN_XNUM ||
210 		strndx == SHN_XINDEX))) {
211 		LIBELF_SET_ERROR(HEADER, 0);
212 		return (NULL);
213 	}
214 
215 	if (shnum != 0 || shoff == 0LL) { /* not using extended numbering */
216 		e->e_u.e_elf.e_nphdr = phnum;
217 		e->e_u.e_elf.e_nscn = shnum;
218 		e->e_u.e_elf.e_strndx = strndx;
219 	} else if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0)
220 		return (NULL);
221 
222 	return (ehdr);
223 }
224