xref: /freebsd/contrib/elftoolchain/libelf/libelf_ehdr.c (revision d003e0d7fe0d3a9b4b2c5835bb3f0f6faf3ab538)
12de3b87aSKai Wang /*-
22de3b87aSKai Wang  * Copyright (c) 2006,2008 Joseph Koshy
32de3b87aSKai Wang  * All rights reserved.
42de3b87aSKai Wang  *
52de3b87aSKai Wang  * Redistribution and use in source and binary forms, with or without
62de3b87aSKai Wang  * modification, are permitted provided that the following conditions
72de3b87aSKai Wang  * are met:
82de3b87aSKai Wang  * 1. Redistributions of source code must retain the above copyright
92de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer.
102de3b87aSKai Wang  * 2. Redistributions in binary form must reproduce the above copyright
112de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer in the
122de3b87aSKai Wang  *    documentation and/or other materials provided with the distribution.
132de3b87aSKai Wang  *
142de3b87aSKai Wang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152de3b87aSKai Wang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162de3b87aSKai Wang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172de3b87aSKai Wang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182de3b87aSKai Wang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192de3b87aSKai Wang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202de3b87aSKai Wang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212de3b87aSKai Wang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222de3b87aSKai Wang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232de3b87aSKai Wang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242de3b87aSKai Wang  * SUCH DAMAGE.
252de3b87aSKai Wang  */
262de3b87aSKai Wang 
272de3b87aSKai Wang #include <assert.h>
282de3b87aSKai Wang #include <gelf.h>
292de3b87aSKai Wang #include <libelf.h>
302de3b87aSKai Wang #include <stdlib.h>
312de3b87aSKai Wang 
322de3b87aSKai Wang #include "_libelf.h"
332de3b87aSKai Wang 
34*d003e0d7SEd Maste ELFTC_VCSID("$Id: libelf_ehdr.c 3732 2019-04-22 11:08:38Z jkoshy $");
352de3b87aSKai Wang 
362de3b87aSKai Wang /*
372de3b87aSKai Wang  * Retrieve counts for sections, phdrs and the section string table index
382de3b87aSKai Wang  * from section header #0 of the ELF object.
392de3b87aSKai Wang  */
402de3b87aSKai Wang static int
_libelf_load_extended(Elf * e,int ec,uint64_t shoff,uint16_t phnum,uint16_t strndx)412de3b87aSKai Wang _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum,
422de3b87aSKai Wang     uint16_t strndx)
432de3b87aSKai Wang {
442de3b87aSKai Wang 	size_t fsz;
45ae500c1fSEd Maste 	Elf_Scn *scn;
462de3b87aSKai Wang 	uint32_t shtype;
47ae500c1fSEd Maste 	_libelf_translator_function *xlator;
482de3b87aSKai Wang 
498c953901SMark Johnston 	assert(RB_EMPTY(&e->e_u.e_elf.e_scn));
502de3b87aSKai Wang 
512de3b87aSKai Wang 	fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1);
522de3b87aSKai Wang 	assert(fsz > 0);
532de3b87aSKai Wang 
54*d003e0d7SEd Maste 	if (shoff + fsz < shoff) {	/* Numeric overflow. */
55*d003e0d7SEd Maste 		LIBELF_SET_ERROR(HEADER, 0);
56*d003e0d7SEd Maste 		return (0);
57*d003e0d7SEd Maste 	}
58*d003e0d7SEd Maste 
59*d003e0d7SEd Maste 	if ((uint64_t) e->e_rawsize < shoff + fsz) {
602de3b87aSKai Wang 		LIBELF_SET_ERROR(HEADER, 0);
612de3b87aSKai Wang 		return (0);
622de3b87aSKai Wang 	}
632de3b87aSKai Wang 
642de3b87aSKai Wang 	if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL)
652de3b87aSKai Wang 		return (0);
662de3b87aSKai Wang 
67ae500c1fSEd Maste 	xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec,
68ae500c1fSEd Maste 	    _libelf_elfmachine(e));
69cf781b2eSEd Maste 	(*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr),
70cf781b2eSEd Maste 	    (unsigned char *) e->e_rawfile + shoff, (size_t) 1,
712de3b87aSKai Wang 	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
722de3b87aSKai Wang 
732de3b87aSKai Wang #define	GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \
742de3b87aSKai Wang 		scn->s_shdr.s_shdr64.M)
752de3b87aSKai Wang 
762de3b87aSKai Wang 	if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL) {
772de3b87aSKai Wang 		LIBELF_SET_ERROR(SECTION, 0);
782de3b87aSKai Wang 		return (0);
792de3b87aSKai Wang 	}
802de3b87aSKai Wang 
81cf781b2eSEd Maste 	e->e_u.e_elf.e_nscn = (size_t) GET_SHDR_MEMBER(sh_size);
822de3b87aSKai Wang 	e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum :
832de3b87aSKai Wang 	    GET_SHDR_MEMBER(sh_info);
842de3b87aSKai Wang 	e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx :
852de3b87aSKai Wang 	    GET_SHDR_MEMBER(sh_link);
862de3b87aSKai Wang #undef	GET_SHDR_MEMBER
872de3b87aSKai Wang 
882de3b87aSKai Wang 	return (1);
892de3b87aSKai Wang }
902de3b87aSKai Wang 
912de3b87aSKai Wang #define	EHDR_INIT(E,SZ)	 do {						\
922de3b87aSKai Wang 		Elf##SZ##_Ehdr *eh = (E);				\
932de3b87aSKai Wang 		eh->e_ident[EI_MAG0] = ELFMAG0;				\
942de3b87aSKai Wang 		eh->e_ident[EI_MAG1] = ELFMAG1;				\
952de3b87aSKai Wang 		eh->e_ident[EI_MAG2] = ELFMAG2;				\
962de3b87aSKai Wang 		eh->e_ident[EI_MAG3] = ELFMAG3;				\
972de3b87aSKai Wang 		eh->e_ident[EI_CLASS] = ELFCLASS##SZ;			\
982de3b87aSKai Wang 		eh->e_ident[EI_DATA]  = ELFDATANONE;			\
99cf781b2eSEd Maste 		eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version) & 0xFFU; \
1002de3b87aSKai Wang 		eh->e_machine = EM_NONE;				\
1012de3b87aSKai Wang 		eh->e_type    = ELF_K_NONE;				\
1022de3b87aSKai Wang 		eh->e_version = LIBELF_PRIVATE(version);		\
1032de3b87aSKai Wang 	} while (0)
1042de3b87aSKai Wang 
1052de3b87aSKai Wang void *
_libelf_ehdr(Elf * e,int ec,int allocate)1062de3b87aSKai Wang _libelf_ehdr(Elf *e, int ec, int allocate)
1072de3b87aSKai Wang {
1082de3b87aSKai Wang 	void *ehdr;
1092de3b87aSKai Wang 	size_t fsz, msz;
1102de3b87aSKai Wang 	uint16_t phnum, shnum, strndx;
1112de3b87aSKai Wang 	uint64_t shoff;
112cf781b2eSEd Maste 	int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s,
113cf781b2eSEd Maste 	    size_t _c, int _swap);
1142de3b87aSKai Wang 
1152de3b87aSKai Wang 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
1162de3b87aSKai Wang 
1172de3b87aSKai Wang 	if (e == NULL || e->e_kind != ELF_K_ELF) {
1182de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
1192de3b87aSKai Wang 		return (NULL);
1202de3b87aSKai Wang 	}
1212de3b87aSKai Wang 
1222de3b87aSKai Wang 	if (e->e_class != ELFCLASSNONE && e->e_class != ec) {
1232de3b87aSKai Wang 		LIBELF_SET_ERROR(CLASS, 0);
1242de3b87aSKai Wang 		return (NULL);
1252de3b87aSKai Wang 	}
1262de3b87aSKai Wang 
1272de3b87aSKai Wang 	if (e->e_version != EV_CURRENT) {
1282de3b87aSKai Wang 		LIBELF_SET_ERROR(VERSION, 0);
1292de3b87aSKai Wang 		return (NULL);
1302de3b87aSKai Wang 	}
1312de3b87aSKai Wang 
1322de3b87aSKai Wang 	if (e->e_class == ELFCLASSNONE)
1332de3b87aSKai Wang 		e->e_class = ec;
1342de3b87aSKai Wang 
1352de3b87aSKai Wang 	if (ec == ELFCLASS32)
1362de3b87aSKai Wang 		ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32;
1372de3b87aSKai Wang 	else
1382de3b87aSKai Wang 		ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64;
1392de3b87aSKai Wang 
1402de3b87aSKai Wang 	if (ehdr != NULL)	/* already have a translated ehdr */
1412de3b87aSKai Wang 		return (ehdr);
1422de3b87aSKai Wang 
1432de3b87aSKai Wang 	fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
1442de3b87aSKai Wang 	assert(fsz > 0);
1452de3b87aSKai Wang 
146*d003e0d7SEd Maste 	if (e->e_cmd != ELF_C_WRITE && e->e_rawsize < (off_t) fsz) {
1472de3b87aSKai Wang 		LIBELF_SET_ERROR(HEADER, 0);
1482de3b87aSKai Wang 		return (NULL);
1492de3b87aSKai Wang 	}
1502de3b87aSKai Wang 
151*d003e0d7SEd Maste 	if ((msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT)) == 0)
152*d003e0d7SEd Maste 		return (NULL);
1532de3b87aSKai Wang 
1542de3b87aSKai Wang 	if ((ehdr = calloc((size_t) 1, msz)) == NULL) {
1552de3b87aSKai Wang 		LIBELF_SET_ERROR(RESOURCE, 0);
1562de3b87aSKai Wang 		return (NULL);
1572de3b87aSKai Wang 	}
1582de3b87aSKai Wang 
1592de3b87aSKai Wang 	if (ec == ELFCLASS32) {
1602de3b87aSKai Wang 		e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr;
1612de3b87aSKai Wang 		EHDR_INIT(ehdr,32);
1622de3b87aSKai Wang 	} else {
1632de3b87aSKai Wang 		e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr;
1642de3b87aSKai Wang 		EHDR_INIT(ehdr,64);
1652de3b87aSKai Wang 	}
1662de3b87aSKai Wang 
1672de3b87aSKai Wang 	if (allocate)
1682de3b87aSKai Wang 		e->e_flags |= ELF_F_DIRTY;
1692de3b87aSKai Wang 
1702de3b87aSKai Wang 	if (e->e_cmd == ELF_C_WRITE)
1712de3b87aSKai Wang 		return (ehdr);
1722de3b87aSKai Wang 
173ae500c1fSEd Maste 	xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec,
174ae500c1fSEd Maste 	    _libelf_elfmachine(e));
175cf781b2eSEd Maste 	(*xlator)((unsigned char*) ehdr, msz, e->e_rawfile, (size_t) 1,
1762de3b87aSKai Wang 	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
1772de3b87aSKai Wang 
1782de3b87aSKai Wang 	if (ec == ELFCLASS32) {
1792de3b87aSKai Wang 		phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
1802de3b87aSKai Wang 		shnum = ((Elf32_Ehdr *) ehdr)->e_shnum;
1812de3b87aSKai Wang 		shoff = ((Elf32_Ehdr *) ehdr)->e_shoff;
1822de3b87aSKai Wang 		strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx;
1832de3b87aSKai Wang 	} else {
1842de3b87aSKai Wang 		phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
1852de3b87aSKai Wang 		shnum = ((Elf64_Ehdr *) ehdr)->e_shnum;
1862de3b87aSKai Wang 		shoff = ((Elf64_Ehdr *) ehdr)->e_shoff;
1872de3b87aSKai Wang 		strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx;
1882de3b87aSKai Wang 	}
1892de3b87aSKai Wang 
1902de3b87aSKai Wang 	if (shnum >= SHN_LORESERVE ||
1912de3b87aSKai Wang 	    (shoff == 0LL && (shnum != 0 || phnum == PN_XNUM ||
1922de3b87aSKai Wang 		strndx == SHN_XINDEX))) {
1932de3b87aSKai Wang 		LIBELF_SET_ERROR(HEADER, 0);
1942de3b87aSKai Wang 		return (NULL);
1952de3b87aSKai Wang 	}
1962de3b87aSKai Wang 
1971d1bfbbbSConrad Meyer 	/*
1981d1bfbbbSConrad Meyer 	 * If extended numbering is being used, read the correct
1991d1bfbbbSConrad Meyer 	 * number of sections and program header entries.
2001d1bfbbbSConrad Meyer 	 */
2011d1bfbbbSConrad Meyer 	if ((shnum == 0 && shoff != 0) || phnum == PN_XNUM || strndx == SHN_XINDEX) {
2021d1bfbbbSConrad Meyer 		if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0)
2031d1bfbbbSConrad Meyer 			return (NULL);
2041d1bfbbbSConrad Meyer 	} else {
2051d1bfbbbSConrad Meyer 		/* not using extended numbering */
2062de3b87aSKai Wang 		e->e_u.e_elf.e_nphdr = phnum;
2072de3b87aSKai Wang 		e->e_u.e_elf.e_nscn = shnum;
2082de3b87aSKai Wang 		e->e_u.e_elf.e_strndx = strndx;
2091d1bfbbbSConrad Meyer 	}
2102de3b87aSKai Wang 
2112de3b87aSKai Wang 	return (ehdr);
2122de3b87aSKai Wang }
213