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