1 1.6 jkoshy /* $NetBSD: libelf_phdr.c,v 1.7 2026/05/17 21:40:51 jkoshy Exp $ */ 2 1.2 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2006,2008 Joseph Koshy 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 christos * SUCH DAMAGE. 27 1.1 christos */ 28 1.1 christos 29 1.2 christos #if HAVE_NBTOOL_CONFIG_H 30 1.2 christos # include "nbtool_config.h" 31 1.2 christos #endif 32 1.2 christos 33 1.1 christos #include <sys/cdefs.h> 34 1.1 christos 35 1.1 christos #include <assert.h> 36 1.1 christos #include <gelf.h> 37 1.1 christos #include <libelf.h> 38 1.1 christos #include <stdlib.h> 39 1.1 christos 40 1.1 christos #include "_libelf.h" 41 1.1 christos 42 1.6 jkoshy ELFTC_VCSID("Id: libelf_phdr.c 4074 2025-01-07 15:34:21Z jkoshy"); 43 1.6 jkoshy 44 1.6 jkoshy __RCSID("$NetBSD: libelf_phdr.c,v 1.7 2026/05/17 21:40:51 jkoshy Exp $"); 45 1.1 christos 46 1.1 christos void * 47 1.6 jkoshy _libelf_getphdr(Elf *e, unsigned int ec) 48 1.1 christos { 49 1.1 christos size_t phnum; 50 1.1 christos size_t fsz, msz; 51 1.1 christos uint64_t phoff; 52 1.1 christos Elf32_Ehdr *eh32; 53 1.1 christos Elf64_Ehdr *eh64; 54 1.1 christos void *ehdr, *phdr; 55 1.5 christos _libelf_translator_function *xlator; 56 1.1 christos 57 1.1 christos assert(ec == ELFCLASS32 || ec == ELFCLASS64); 58 1.1 christos 59 1.1 christos if (e == NULL) { 60 1.1 christos LIBELF_SET_ERROR(ARGUMENT, 0); 61 1.1 christos return (NULL); 62 1.1 christos } 63 1.1 christos 64 1.1 christos if ((phdr = (ec == ELFCLASS32 ? 65 1.1 christos (void *) e->e_u.e_elf.e_phdr.e_phdr32 : 66 1.1 christos (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL) 67 1.1 christos return (phdr); 68 1.1 christos 69 1.1 christos /* 70 1.1 christos * Check the PHDR related fields in the EHDR for sanity. 71 1.1 christos */ 72 1.1 christos 73 1.1 christos if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 74 1.1 christos return (NULL); 75 1.1 christos 76 1.1 christos phnum = e->e_u.e_elf.e_nphdr; 77 1.1 christos 78 1.1 christos if (ec == ELFCLASS32) { 79 1.1 christos eh32 = (Elf32_Ehdr *) ehdr; 80 1.1 christos phoff = (uint64_t) eh32->e_phoff; 81 1.1 christos } else { 82 1.1 christos eh64 = (Elf64_Ehdr *) ehdr; 83 1.1 christos phoff = (uint64_t) eh64->e_phoff; 84 1.1 christos } 85 1.1 christos 86 1.1 christos fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version); 87 1.1 christos 88 1.1 christos assert(fsz > 0); 89 1.1 christos 90 1.5 christos if (phoff + fsz < phoff) { /* Numeric overflow. */ 91 1.5 christos LIBELF_SET_ERROR(HEADER, 0); 92 1.5 christos return (NULL); 93 1.5 christos } 94 1.5 christos 95 1.1 christos if ((uint64_t) e->e_rawsize < (phoff + fsz)) { 96 1.1 christos LIBELF_SET_ERROR(HEADER, 0); 97 1.1 christos return (NULL); 98 1.1 christos } 99 1.1 christos 100 1.5 christos if ((msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT)) == 0) 101 1.5 christos return (NULL); 102 1.1 christos 103 1.1 christos if ((phdr = calloc(phnum, msz)) == NULL) { 104 1.1 christos LIBELF_SET_ERROR(RESOURCE, 0); 105 1.1 christos return (NULL); 106 1.1 christos } 107 1.1 christos 108 1.1 christos if (ec == ELFCLASS32) 109 1.1 christos e->e_u.e_elf.e_phdr.e_phdr32 = phdr; 110 1.1 christos else 111 1.1 christos e->e_u.e_elf.e_phdr.e_phdr64 = phdr; 112 1.1 christos 113 1.1 christos 114 1.5 christos xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec, 115 1.5 christos _libelf_elfmachine(e)); 116 1.1 christos (*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum, 117 1.5 christos e->e_byteorder != LIBELF_PRIVATE(byteorder)); 118 1.1 christos 119 1.1 christos return (phdr); 120 1.1 christos } 121 1.1 christos 122 1.1 christos void * 123 1.6 jkoshy _libelf_newphdr(Elf *e, unsigned int ec, size_t count) 124 1.1 christos { 125 1.1 christos void *ehdr, *newphdr, *oldphdr; 126 1.1 christos size_t msz; 127 1.1 christos 128 1.1 christos if (e == NULL) { 129 1.1 christos LIBELF_SET_ERROR(ARGUMENT, 0); 130 1.1 christos return (NULL); 131 1.1 christos } 132 1.1 christos 133 1.1 christos if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) { 134 1.1 christos LIBELF_SET_ERROR(SEQUENCE, 0); 135 1.1 christos return (NULL); 136 1.1 christos } 137 1.1 christos 138 1.1 christos assert(e->e_class == ec); 139 1.1 christos assert(ec == ELFCLASS32 || ec == ELFCLASS64); 140 1.1 christos assert(e->e_version == EV_CURRENT); 141 1.1 christos 142 1.5 christos if ((msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version)) == 0) 143 1.5 christos return (NULL); 144 1.1 christos 145 1.1 christos newphdr = NULL; 146 1.1 christos if (count > 0 && (newphdr = calloc(count, msz)) == NULL) { 147 1.1 christos LIBELF_SET_ERROR(RESOURCE, 0); 148 1.1 christos return (NULL); 149 1.1 christos } 150 1.1 christos 151 1.1 christos if (ec == ELFCLASS32) { 152 1.1 christos if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL) 153 1.1 christos free(oldphdr); 154 1.1 christos e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr; 155 1.1 christos } else { 156 1.1 christos if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL) 157 1.1 christos free(oldphdr); 158 1.1 christos e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr; 159 1.1 christos } 160 1.1 christos 161 1.1 christos e->e_u.e_elf.e_nphdr = count; 162 1.1 christos 163 1.1 christos elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY); 164 1.1 christos 165 1.1 christos return (newphdr); 166 1.1 christos } 167