1*5ac3bc71Schristos /* $NetBSD: libelf_ehdr.c,v 1.5 2024/03/03 17:37:34 christos Exp $ */
2e81373b4Schristos
39dd9d0cfSchristos /*-
49dd9d0cfSchristos * Copyright (c) 2006,2008 Joseph Koshy
59dd9d0cfSchristos * All rights reserved.
69dd9d0cfSchristos *
79dd9d0cfSchristos * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos * modification, are permitted provided that the following conditions
99dd9d0cfSchristos * are met:
109dd9d0cfSchristos * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos * notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos * notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos * documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos *
169dd9d0cfSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos * SUCH DAMAGE.
279dd9d0cfSchristos */
289dd9d0cfSchristos
29e81373b4Schristos #if HAVE_NBTOOL_CONFIG_H
30e81373b4Schristos # include "nbtool_config.h"
31e81373b4Schristos #endif
32e81373b4Schristos
339dd9d0cfSchristos #include <sys/cdefs.h>
349dd9d0cfSchristos
359dd9d0cfSchristos #include <assert.h>
369dd9d0cfSchristos #include <gelf.h>
379dd9d0cfSchristos #include <libelf.h>
389dd9d0cfSchristos #include <stdlib.h>
399dd9d0cfSchristos
409dd9d0cfSchristos #include "_libelf.h"
419dd9d0cfSchristos
42*5ac3bc71Schristos __RCSID("$NetBSD: libelf_ehdr.c,v 1.5 2024/03/03 17:37:34 christos Exp $");
43*5ac3bc71Schristos ELFTC_VCSID("Id: libelf_ehdr.c 3977 2022-05-01 06:45:34Z jkoshy");
449dd9d0cfSchristos
459dd9d0cfSchristos /*
469dd9d0cfSchristos * Retrieve counts for sections, phdrs and the section string table index
479dd9d0cfSchristos * from section header #0 of the ELF object.
489dd9d0cfSchristos */
499dd9d0cfSchristos static int
_libelf_load_extended(Elf * e,int ec,uint64_t shoff,uint16_t phnum,uint16_t strndx)509dd9d0cfSchristos _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum,
519dd9d0cfSchristos uint16_t strndx)
529dd9d0cfSchristos {
539dd9d0cfSchristos size_t fsz;
54*5ac3bc71Schristos Elf_Scn *scn;
559dd9d0cfSchristos uint32_t shtype;
56*5ac3bc71Schristos _libelf_translator_function *xlator;
579dd9d0cfSchristos
589dd9d0cfSchristos assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
599dd9d0cfSchristos
609dd9d0cfSchristos fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1);
619dd9d0cfSchristos assert(fsz > 0);
629dd9d0cfSchristos
63*5ac3bc71Schristos if (shoff + fsz < shoff) { /* Numeric overflow. */
64*5ac3bc71Schristos LIBELF_SET_ERROR(HEADER, 0);
65*5ac3bc71Schristos return (0);
66*5ac3bc71Schristos }
67*5ac3bc71Schristos
68*5ac3bc71Schristos if ((uint64_t) e->e_rawsize < shoff + fsz) {
699dd9d0cfSchristos LIBELF_SET_ERROR(HEADER, 0);
709dd9d0cfSchristos return (0);
719dd9d0cfSchristos }
729dd9d0cfSchristos
739dd9d0cfSchristos if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL)
749dd9d0cfSchristos return (0);
759dd9d0cfSchristos
76e81373b4Schristos if (shoff > SSIZE_MAX) {
77e81373b4Schristos LIBELF_SET_ERROR(HEADER, 0);
78e81373b4Schristos return (0);
79e81373b4Schristos }
80e81373b4Schristos
81*5ac3bc71Schristos xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec,
82*5ac3bc71Schristos _libelf_elfmachine(e));
83*5ac3bc71Schristos (*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr),
8442bd3019Schristos (unsigned char *) e->e_rawfile + shoff, (size_t) 1,
85*5ac3bc71Schristos e->e_byteorder != LIBELF_PRIVATE(byteorder));
869dd9d0cfSchristos
879dd9d0cfSchristos #define GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \
889dd9d0cfSchristos scn->s_shdr.s_shdr64.M)
899dd9d0cfSchristos
90e81373b4Schristos if (GET_SHDR_MEMBER(sh_size) > UINT_MAX) {
91e81373b4Schristos LIBELF_SET_ERROR(HEADER, 0);
92e81373b4Schristos return (0);
93e81373b4Schristos }
94e81373b4Schristos
959dd9d0cfSchristos if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL) {
969dd9d0cfSchristos LIBELF_SET_ERROR(SECTION, 0);
979dd9d0cfSchristos return (0);
989dd9d0cfSchristos }
999dd9d0cfSchristos
10042bd3019Schristos e->e_u.e_elf.e_nscn = (size_t) GET_SHDR_MEMBER(sh_size);
1019dd9d0cfSchristos e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum :
1029dd9d0cfSchristos GET_SHDR_MEMBER(sh_info);
1039dd9d0cfSchristos e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx :
1049dd9d0cfSchristos GET_SHDR_MEMBER(sh_link);
1059dd9d0cfSchristos #undef GET_SHDR_MEMBER
1069dd9d0cfSchristos
1079dd9d0cfSchristos return (1);
1089dd9d0cfSchristos }
1099dd9d0cfSchristos
1109dd9d0cfSchristos #define EHDR_INIT(E,SZ) do { \
1119dd9d0cfSchristos Elf##SZ##_Ehdr *eh = (E); \
1129dd9d0cfSchristos eh->e_ident[EI_MAG0] = ELFMAG0; \
1139dd9d0cfSchristos eh->e_ident[EI_MAG1] = ELFMAG1; \
1149dd9d0cfSchristos eh->e_ident[EI_MAG2] = ELFMAG2; \
1159dd9d0cfSchristos eh->e_ident[EI_MAG3] = ELFMAG3; \
1169dd9d0cfSchristos eh->e_ident[EI_CLASS] = ELFCLASS##SZ; \
1179dd9d0cfSchristos eh->e_ident[EI_DATA] = ELFDATANONE; \
11842bd3019Schristos eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version) & 0xFFU; \
1199dd9d0cfSchristos eh->e_machine = EM_NONE; \
1209dd9d0cfSchristos eh->e_type = ELF_K_NONE; \
1219dd9d0cfSchristos eh->e_version = LIBELF_PRIVATE(version); \
122e81373b4Schristos } while (/* CONSTCOND */ 0)
1239dd9d0cfSchristos
1249dd9d0cfSchristos void *
_libelf_ehdr(Elf * e,int ec,int allocate)1259dd9d0cfSchristos _libelf_ehdr(Elf *e, int ec, int allocate)
1269dd9d0cfSchristos {
1279dd9d0cfSchristos void *ehdr;
1289dd9d0cfSchristos size_t fsz, msz;
1299dd9d0cfSchristos uint16_t phnum, shnum, strndx;
1309dd9d0cfSchristos uint64_t shoff;
13142bd3019Schristos int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s,
13242bd3019Schristos size_t _c, int _swap);
1339dd9d0cfSchristos
1349dd9d0cfSchristos assert(ec == ELFCLASS32 || ec == ELFCLASS64);
1359dd9d0cfSchristos
1369dd9d0cfSchristos if (e == NULL || e->e_kind != ELF_K_ELF) {
1379dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
1389dd9d0cfSchristos return (NULL);
1399dd9d0cfSchristos }
1409dd9d0cfSchristos
1419dd9d0cfSchristos if (e->e_class != ELFCLASSNONE && e->e_class != ec) {
1429dd9d0cfSchristos LIBELF_SET_ERROR(CLASS, 0);
1439dd9d0cfSchristos return (NULL);
1449dd9d0cfSchristos }
1459dd9d0cfSchristos
1469dd9d0cfSchristos if (e->e_version != EV_CURRENT) {
1479dd9d0cfSchristos LIBELF_SET_ERROR(VERSION, 0);
1489dd9d0cfSchristos return (NULL);
1499dd9d0cfSchristos }
1509dd9d0cfSchristos
1519dd9d0cfSchristos if (e->e_class == ELFCLASSNONE)
1529dd9d0cfSchristos e->e_class = ec;
1539dd9d0cfSchristos
1549dd9d0cfSchristos if (ec == ELFCLASS32)
1559dd9d0cfSchristos ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32;
1569dd9d0cfSchristos else
1579dd9d0cfSchristos ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64;
1589dd9d0cfSchristos
1599dd9d0cfSchristos if (ehdr != NULL) /* already have a translated ehdr */
1609dd9d0cfSchristos return (ehdr);
1619dd9d0cfSchristos
1629dd9d0cfSchristos fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
1639dd9d0cfSchristos assert(fsz > 0);
1649dd9d0cfSchristos
165*5ac3bc71Schristos if (e->e_cmd != ELF_C_WRITE && e->e_rawsize < (off_t) fsz) {
1669dd9d0cfSchristos LIBELF_SET_ERROR(HEADER, 0);
1679dd9d0cfSchristos return (NULL);
1689dd9d0cfSchristos }
1699dd9d0cfSchristos
170*5ac3bc71Schristos if ((msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT)) == 0)
171*5ac3bc71Schristos return (NULL);
1729dd9d0cfSchristos
1739dd9d0cfSchristos if ((ehdr = calloc((size_t) 1, msz)) == NULL) {
1749dd9d0cfSchristos LIBELF_SET_ERROR(RESOURCE, 0);
1759dd9d0cfSchristos return (NULL);
1769dd9d0cfSchristos }
1779dd9d0cfSchristos
1789dd9d0cfSchristos if (ec == ELFCLASS32) {
1799dd9d0cfSchristos e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr;
1809dd9d0cfSchristos EHDR_INIT(ehdr,32);
1819dd9d0cfSchristos } else {
1829dd9d0cfSchristos e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr;
1839dd9d0cfSchristos EHDR_INIT(ehdr,64);
1849dd9d0cfSchristos }
1859dd9d0cfSchristos
1869dd9d0cfSchristos if (allocate)
1879dd9d0cfSchristos e->e_flags |= ELF_F_DIRTY;
1889dd9d0cfSchristos
1899dd9d0cfSchristos if (e->e_cmd == ELF_C_WRITE)
1909dd9d0cfSchristos return (ehdr);
1919dd9d0cfSchristos
192*5ac3bc71Schristos xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec,
193*5ac3bc71Schristos _libelf_elfmachine(e));
194*5ac3bc71Schristos (*xlator)((unsigned char*) ehdr, msz, e->e_rawfile, (size_t) 1,
195*5ac3bc71Schristos e->e_byteorder != LIBELF_PRIVATE(byteorder));
1969dd9d0cfSchristos
1979dd9d0cfSchristos if (ec == ELFCLASS32) {
1989dd9d0cfSchristos phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
1999dd9d0cfSchristos shnum = ((Elf32_Ehdr *) ehdr)->e_shnum;
2009dd9d0cfSchristos shoff = ((Elf32_Ehdr *) ehdr)->e_shoff;
2019dd9d0cfSchristos strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx;
2029dd9d0cfSchristos } else {
2039dd9d0cfSchristos phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
2049dd9d0cfSchristos shnum = ((Elf64_Ehdr *) ehdr)->e_shnum;
2059dd9d0cfSchristos shoff = ((Elf64_Ehdr *) ehdr)->e_shoff;
2069dd9d0cfSchristos strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx;
2079dd9d0cfSchristos }
2089dd9d0cfSchristos
2099dd9d0cfSchristos if (shnum >= SHN_LORESERVE ||
2109dd9d0cfSchristos (shoff == 0LL && (shnum != 0 || phnum == PN_XNUM ||
2119dd9d0cfSchristos strndx == SHN_XINDEX))) {
2129dd9d0cfSchristos LIBELF_SET_ERROR(HEADER, 0);
2139dd9d0cfSchristos return (NULL);
2149dd9d0cfSchristos }
2159dd9d0cfSchristos
216*5ac3bc71Schristos /*
217*5ac3bc71Schristos * If extended numbering is being used, read the correct
218*5ac3bc71Schristos * number of sections and program header entries.
219*5ac3bc71Schristos */
220*5ac3bc71Schristos if ((shnum == 0 && shoff != 0) || phnum == PN_XNUM || strndx == SHN_XINDEX) {
221*5ac3bc71Schristos if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0)
222*5ac3bc71Schristos return (NULL);
223*5ac3bc71Schristos } else {
224*5ac3bc71Schristos /* not using extended numbering */
2259dd9d0cfSchristos e->e_u.e_elf.e_nphdr = phnum;
2269dd9d0cfSchristos e->e_u.e_elf.e_nscn = shnum;
2279dd9d0cfSchristos e->e_u.e_elf.e_strndx = strndx;
228*5ac3bc71Schristos }
2299dd9d0cfSchristos
2309dd9d0cfSchristos return (ehdr);
2319dd9d0cfSchristos }
232