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