1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12164190Sjkoshy *    documentation and/or other materials provided with the distribution.
13164190Sjkoshy *
14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164190Sjkoshy * SUCH DAMAGE.
25164190Sjkoshy *
26164190Sjkoshy * $FreeBSD$
27164190Sjkoshy */
28164190Sjkoshy
29164190Sjkoshy#include <sys/cdefs.h>
30164190Sjkoshy__FBSDID("$FreeBSD$");
31164190Sjkoshy
32164190Sjkoshy#include <assert.h>
33164190Sjkoshy#include <gelf.h>
34164190Sjkoshy#include <libelf.h>
35164190Sjkoshy#include <stdlib.h>
36164190Sjkoshy
37164190Sjkoshy#include "_libelf.h"
38164190Sjkoshy
39165535Sjkoshy/*
40165535Sjkoshy * Retrieve counts for sections, phdrs and the section string table index
41165535Sjkoshy * from section header #0 of the ELF object.
42165535Sjkoshy */
43165535Sjkoshystatic int
44165535Sjkoshy_libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum,
45165535Sjkoshy    uint16_t strndx)
46165535Sjkoshy{
47165535Sjkoshy	Elf_Scn *scn;
48165535Sjkoshy	size_t fsz;
49210338Skaiw	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
50165535Sjkoshy	uint32_t shtype;
51165535Sjkoshy
52165535Sjkoshy	assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
53165535Sjkoshy
54165535Sjkoshy	fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1);
55165535Sjkoshy	assert(fsz > 0);
56165535Sjkoshy
57165535Sjkoshy	if (e->e_rawsize < shoff + fsz) { /* raw file too small */
58165535Sjkoshy		LIBELF_SET_ERROR(HEADER, 0);
59165535Sjkoshy		return (0);
60165535Sjkoshy	}
61165535Sjkoshy
62165535Sjkoshy	if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL)
63165535Sjkoshy		return (0);
64165535Sjkoshy
65165535Sjkoshy	xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
66210338Skaiw	(*xlator)((char *) &scn->s_shdr, sizeof(scn->s_shdr),
67210338Skaiw	    e->e_rawfile + shoff, (size_t) 1,
68165535Sjkoshy	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
69165535Sjkoshy
70165535Sjkoshy#define	GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \
71165535Sjkoshy		scn->s_shdr.s_shdr64.M)
72165535Sjkoshy
73165535Sjkoshy	if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL) {
74165535Sjkoshy		LIBELF_SET_ERROR(SECTION, 0);
75165535Sjkoshy		return (0);
76165535Sjkoshy	}
77165535Sjkoshy
78165535Sjkoshy	e->e_u.e_elf.e_nscn = GET_SHDR_MEMBER(sh_size);
79165535Sjkoshy	e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum :
80165535Sjkoshy	    GET_SHDR_MEMBER(sh_info);
81165535Sjkoshy	e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx :
82165535Sjkoshy	    GET_SHDR_MEMBER(sh_link);
83165535Sjkoshy#undef	GET_SHDR_MEMBER
84165535Sjkoshy
85165535Sjkoshy	return (1);
86165535Sjkoshy}
87165535Sjkoshy
88164190Sjkoshy#define	EHDR_INIT(E,SZ)	 do {						\
89164190Sjkoshy		Elf##SZ##_Ehdr *eh = (E);				\
90164190Sjkoshy		eh->e_ident[EI_MAG0] = ELFMAG0;				\
91164190Sjkoshy		eh->e_ident[EI_MAG1] = ELFMAG1;				\
92164190Sjkoshy		eh->e_ident[EI_MAG2] = ELFMAG2;				\
93164190Sjkoshy		eh->e_ident[EI_MAG3] = ELFMAG3;				\
94164190Sjkoshy		eh->e_ident[EI_CLASS] = ELFCLASS##SZ;			\
95164190Sjkoshy		eh->e_ident[EI_DATA]  = ELFDATANONE;			\
96164190Sjkoshy		eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version);	\
97164190Sjkoshy		eh->e_machine = EM_NONE;				\
98164190Sjkoshy		eh->e_type    = ELF_K_NONE;				\
99164190Sjkoshy		eh->e_version = LIBELF_PRIVATE(version);		\
100164190Sjkoshy	} while (0)
101164190Sjkoshy
102164190Sjkoshyvoid *
103164190Sjkoshy_libelf_ehdr(Elf *e, int ec, int allocate)
104164190Sjkoshy{
105165535Sjkoshy	void *ehdr;
106164190Sjkoshy	size_t fsz, msz;
107165535Sjkoshy	uint16_t phnum, shnum, strndx;
108165535Sjkoshy	uint64_t shoff;
109210338Skaiw	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
110164190Sjkoshy
111164190Sjkoshy	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
112164190Sjkoshy
113164190Sjkoshy	if (e == NULL || e->e_kind != ELF_K_ELF) {
114164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
115164190Sjkoshy		return (NULL);
116164190Sjkoshy	}
117164190Sjkoshy
118164190Sjkoshy	if (e->e_class != ELFCLASSNONE && e->e_class != ec) {
119164190Sjkoshy		LIBELF_SET_ERROR(CLASS, 0);
120164190Sjkoshy		return (NULL);
121164190Sjkoshy	}
122164190Sjkoshy
123164190Sjkoshy	if (e->e_version != EV_CURRENT) {
124164190Sjkoshy		LIBELF_SET_ERROR(VERSION, 0);
125164190Sjkoshy		return (NULL);
126164190Sjkoshy	}
127164190Sjkoshy
128164190Sjkoshy	if (e->e_class == ELFCLASSNONE)
129164190Sjkoshy		e->e_class = ec;
130164190Sjkoshy
131164190Sjkoshy	if (ec == ELFCLASS32)
132164190Sjkoshy		ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32;
133164190Sjkoshy	else
134164190Sjkoshy		ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64;
135164190Sjkoshy
136164190Sjkoshy	if (ehdr != NULL)	/* already have a translated ehdr */
137164190Sjkoshy		return (ehdr);
138164190Sjkoshy
139165535Sjkoshy	fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
140164190Sjkoshy	assert(fsz > 0);
141164190Sjkoshy
142164190Sjkoshy	if (e->e_cmd != ELF_C_WRITE && e->e_rawsize < fsz) {
143164190Sjkoshy		LIBELF_SET_ERROR(HEADER, 0);
144164190Sjkoshy		return (NULL);
145164190Sjkoshy	}
146164190Sjkoshy
147164190Sjkoshy	msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT);
148164190Sjkoshy
149164190Sjkoshy	assert(msz > 0);
150164190Sjkoshy
151164190Sjkoshy	if ((ehdr = calloc((size_t) 1, msz)) == NULL) {
152164190Sjkoshy		LIBELF_SET_ERROR(RESOURCE, 0);
153164190Sjkoshy		return (NULL);
154164190Sjkoshy	}
155164190Sjkoshy
156164190Sjkoshy	if (ec == ELFCLASS32) {
157164190Sjkoshy		e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr;
158164190Sjkoshy		EHDR_INIT(ehdr,32);
159164190Sjkoshy	} else {
160164190Sjkoshy		e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr;
161164190Sjkoshy		EHDR_INIT(ehdr,64);
162164190Sjkoshy	}
163164190Sjkoshy
164164190Sjkoshy	if (allocate)
165164190Sjkoshy		e->e_flags |= ELF_F_DIRTY;
166164190Sjkoshy
167164190Sjkoshy	if (e->e_cmd == ELF_C_WRITE)
168164190Sjkoshy		return (ehdr);
169164190Sjkoshy
170164190Sjkoshy	xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec);
171210338Skaiw	(*xlator)(ehdr, msz, e->e_rawfile, (size_t) 1,
172164190Sjkoshy	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
173164190Sjkoshy
174165535Sjkoshy	/*
175165535Sjkoshy	 * If extended numbering is being used, read the correct
176165535Sjkoshy	 * number of sections and program header entries.
177165535Sjkoshy	 */
178165535Sjkoshy	if (ec == ELFCLASS32) {
179165535Sjkoshy		phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
180165535Sjkoshy		shnum = ((Elf32_Ehdr *) ehdr)->e_shnum;
181165535Sjkoshy		shoff = ((Elf32_Ehdr *) ehdr)->e_shoff;
182165535Sjkoshy		strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx;
183165535Sjkoshy	} else {
184165535Sjkoshy		phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
185165535Sjkoshy		shnum = ((Elf64_Ehdr *) ehdr)->e_shnum;
186165535Sjkoshy		shoff = ((Elf64_Ehdr *) ehdr)->e_shoff;
187165535Sjkoshy		strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx;
188165535Sjkoshy	}
189165535Sjkoshy
190165535Sjkoshy	if (shnum >= SHN_LORESERVE ||
191165535Sjkoshy	    (shoff == 0LL && (shnum != 0 || phnum == PN_XNUM ||
192165535Sjkoshy		strndx == SHN_XINDEX))) {
193165535Sjkoshy		LIBELF_SET_ERROR(HEADER, 0);
194165535Sjkoshy		return (NULL);
195165535Sjkoshy	}
196165535Sjkoshy
197165535Sjkoshy	if (shnum != 0 || shoff == 0LL) { /* not using extended numbering */
198165535Sjkoshy		e->e_u.e_elf.e_nphdr = phnum;
199165535Sjkoshy		e->e_u.e_elf.e_nscn = shnum;
200165535Sjkoshy		e->e_u.e_elf.e_strndx = strndx;
201165535Sjkoshy	} else if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0)
202165535Sjkoshy		return (NULL);
203165535Sjkoshy
204164190Sjkoshy	return (ehdr);
205164190Sjkoshy}
206