1 /* $NetBSD: gelf_phdr.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 #include <sys/cdefs.h> 30 31 #include <gelf.h> 32 #include <libelf.h> 33 #include <limits.h> 34 #include <stdint.h> 35 36 #include "_libelf.h" 37 38 __RCSID("$NetBSD: gelf_phdr.c,v 1.5 2024/03/03 17:37:34 christos Exp $"); 39 ELFTC_VCSID("Id: gelf_phdr.c 3977 2022-05-01 06:45:34Z jkoshy"); 40 41 Elf32_Phdr * 42 elf32_getphdr(Elf *e) 43 { 44 return (_libelf_getphdr(e, ELFCLASS32)); 45 } 46 47 Elf64_Phdr * 48 elf64_getphdr(Elf *e) 49 { 50 return (_libelf_getphdr(e, ELFCLASS64)); 51 } 52 53 GElf_Phdr * 54 gelf_getphdr(Elf *e, int index, GElf_Phdr *d) 55 { 56 int ec; 57 Elf32_Ehdr *eh32; 58 Elf64_Ehdr *eh64; 59 Elf32_Phdr *ep32; 60 Elf64_Phdr *ep64; 61 size_t phnum; 62 63 if (d == NULL || e == NULL || 64 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) || 65 (e->e_kind != ELF_K_ELF) || index < 0 || 66 elf_getphdrnum(e, &phnum) < 0) { 67 LIBELF_SET_ERROR(ARGUMENT, 0); 68 return (NULL); 69 } 70 71 if ((size_t)index >= phnum) { 72 LIBELF_SET_ERROR(ARGUMENT, 0); 73 return (NULL); 74 } 75 76 if (ec == ELFCLASS32) { 77 if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL || 78 ((ep32 = _libelf_getphdr(e, ELFCLASS32)) == NULL)) 79 return (NULL); 80 81 ep32 += index; 82 83 d->p_type = ep32->p_type; 84 d->p_offset = ep32->p_offset; 85 d->p_vaddr = (Elf64_Addr) ep32->p_vaddr; 86 d->p_paddr = (Elf64_Addr) ep32->p_paddr; 87 d->p_filesz = (Elf64_Xword) ep32->p_filesz; 88 d->p_memsz = (Elf64_Xword) ep32->p_memsz; 89 d->p_flags = ep32->p_flags; 90 d->p_align = (Elf64_Xword) ep32->p_align; 91 92 } else { 93 if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL || 94 (ep64 = _libelf_getphdr(e, ELFCLASS64)) == NULL) 95 return (NULL); 96 97 ep64 += index; 98 99 *d = *ep64; 100 } 101 102 return (d); 103 } 104 105 Elf32_Phdr * 106 elf32_newphdr(Elf *e, size_t count) 107 { 108 return (_libelf_newphdr(e, ELFCLASS32, count)); 109 } 110 111 Elf64_Phdr * 112 elf64_newphdr(Elf *e, size_t count) 113 { 114 return (_libelf_newphdr(e, ELFCLASS64, count)); 115 } 116 117 void * 118 gelf_newphdr(Elf *e, size_t count) 119 { 120 if (e == NULL) { 121 LIBELF_SET_ERROR(ARGUMENT, 0); 122 return (NULL); 123 } 124 return (_libelf_newphdr(e, e->e_class, count)); 125 } 126 127 int 128 gelf_update_phdr(Elf *e, int ndx, GElf_Phdr *s) 129 { 130 int ec; 131 size_t phnum; 132 void *ehdr; 133 Elf32_Phdr *ph32; 134 Elf64_Phdr *ph64; 135 136 if (s == NULL || e == NULL || e->e_kind != ELF_K_ELF || 137 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) || 138 elf_getphdrnum(e, &phnum) < 0) { 139 LIBELF_SET_ERROR(ARGUMENT, 0); 140 return (0); 141 } 142 143 if (e->e_cmd == ELF_C_READ) { 144 LIBELF_SET_ERROR(MODE, 0); 145 return (0); 146 } 147 148 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 149 return (0); 150 151 if (ndx < 0 || (size_t)ndx > phnum) { 152 LIBELF_SET_ERROR(ARGUMENT, 0); 153 return (0); 154 } 155 156 (void) elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY); 157 158 if (ec == ELFCLASS64) { 159 ph64 = e->e_u.e_elf.e_phdr.e_phdr64 + ndx; 160 *ph64 = *s; 161 return (1); 162 } 163 164 ph32 = e->e_u.e_elf.e_phdr.e_phdr32 + ndx; 165 166 ph32->p_type = s->p_type; 167 ph32->p_flags = s->p_flags; 168 LIBELF_COPY_U32(ph32, s, p_offset); 169 LIBELF_COPY_U32(ph32, s, p_vaddr); 170 LIBELF_COPY_U32(ph32, s, p_paddr); 171 LIBELF_COPY_U32(ph32, s, p_filesz); 172 LIBELF_COPY_U32(ph32, s, p_memsz); 173 LIBELF_COPY_U32(ph32, s, p_align); 174 175 return (1); 176 } 177