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