1 /* $NetBSD: libelf_phdr.c,v 1.3 2016/02/20 02:43:42 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_phdr.c,v 1.3 2016/02/20 02:43:42 christos Exp $"); 43 ELFTC_VCSID("Id: libelf_phdr.c 3174 2015-03-27 17:13:41Z emaste "); 44 45 void * 46 _libelf_getphdr(Elf *e, int ec) 47 { 48 size_t phnum; 49 size_t fsz, msz; 50 uint64_t phoff; 51 Elf32_Ehdr *eh32; 52 Elf64_Ehdr *eh64; 53 void *ehdr, *phdr; 54 int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s, 55 size_t _c, int _swap); 56 57 assert(ec == ELFCLASS32 || ec == ELFCLASS64); 58 59 if (e == NULL) { 60 LIBELF_SET_ERROR(ARGUMENT, 0); 61 return (NULL); 62 } 63 64 if ((phdr = (ec == ELFCLASS32 ? 65 (void *) e->e_u.e_elf.e_phdr.e_phdr32 : 66 (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL) 67 return (phdr); 68 69 /* 70 * Check the PHDR related fields in the EHDR for sanity. 71 */ 72 73 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 74 return (NULL); 75 76 phnum = e->e_u.e_elf.e_nphdr; 77 78 if (ec == ELFCLASS32) { 79 eh32 = (Elf32_Ehdr *) ehdr; 80 phoff = (uint64_t) eh32->e_phoff; 81 } else { 82 eh64 = (Elf64_Ehdr *) ehdr; 83 phoff = (uint64_t) eh64->e_phoff; 84 } 85 86 fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version); 87 88 assert(fsz > 0); 89 90 if ((uint64_t) e->e_rawsize < (phoff + fsz)) { 91 LIBELF_SET_ERROR(HEADER, 0); 92 return (NULL); 93 } 94 95 msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT); 96 97 assert(msz > 0); 98 99 if ((phdr = calloc(phnum, msz)) == NULL) { 100 LIBELF_SET_ERROR(RESOURCE, 0); 101 return (NULL); 102 } 103 104 if (ec == ELFCLASS32) 105 e->e_u.e_elf.e_phdr.e_phdr32 = phdr; 106 else 107 e->e_u.e_elf.e_phdr.e_phdr64 = phdr; 108 109 110 xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec); 111 (*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum, 112 e->e_byteorder != _libelf_host_byteorder()); 113 114 return (phdr); 115 } 116 117 void * 118 _libelf_newphdr(Elf *e, int ec, size_t count) 119 { 120 void *ehdr, *newphdr, *oldphdr; 121 size_t msz; 122 123 if (e == NULL) { 124 LIBELF_SET_ERROR(ARGUMENT, 0); 125 return (NULL); 126 } 127 128 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) { 129 LIBELF_SET_ERROR(SEQUENCE, 0); 130 return (NULL); 131 } 132 133 assert(e->e_class == ec); 134 assert(ec == ELFCLASS32 || ec == ELFCLASS64); 135 assert(e->e_version == EV_CURRENT); 136 137 msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version); 138 139 assert(msz > 0); 140 141 newphdr = NULL; 142 if (count > 0 && (newphdr = calloc(count, msz)) == NULL) { 143 LIBELF_SET_ERROR(RESOURCE, 0); 144 return (NULL); 145 } 146 147 if (ec == ELFCLASS32) { 148 if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL) 149 free(oldphdr); 150 e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr; 151 } else { 152 if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL) 153 free(oldphdr); 154 e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr; 155 } 156 157 e->e_u.e_elf.e_nphdr = count; 158 159 elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY); 160 161 return (newphdr); 162 } 163