1 /* $NetBSD: gelf_ehdr.c,v 1.2 2014/03/09 16:58:04 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 <limits.h> 39 #include <string.h> 40 41 #include "_libelf.h" 42 43 __RCSID("$NetBSD: gelf_ehdr.c,v 1.2 2014/03/09 16:58:04 christos Exp $"); 44 ELFTC_VCSID("Id: gelf_ehdr.c 2268 2011-12-03 17:05:11Z jkoshy "); 45 46 Elf32_Ehdr * 47 elf32_getehdr(Elf *e) 48 { 49 return (_libelf_ehdr(e, ELFCLASS32, 0)); 50 } 51 52 Elf64_Ehdr * 53 elf64_getehdr(Elf *e) 54 { 55 return (_libelf_ehdr(e, ELFCLASS64, 0)); 56 } 57 58 GElf_Ehdr * 59 gelf_getehdr(Elf *e, GElf_Ehdr *d) 60 { 61 int ec; 62 Elf32_Ehdr *eh32; 63 Elf64_Ehdr *eh64; 64 65 if (d == NULL || e == NULL || 66 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 67 LIBELF_SET_ERROR(ARGUMENT, 0); 68 return (NULL); 69 } 70 71 if (ec == ELFCLASS32) { 72 if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL) 73 return (NULL); 74 75 (void) memcpy(d->e_ident, eh32->e_ident, 76 sizeof(eh32->e_ident)); 77 d->e_type = eh32->e_type; 78 d->e_machine = eh32->e_machine; 79 d->e_version = eh32->e_version; 80 d->e_entry = eh32->e_entry; 81 d->e_phoff = eh32->e_phoff; 82 d->e_shoff = eh32->e_shoff; 83 d->e_flags = eh32->e_flags; 84 d->e_ehsize = eh32->e_ehsize; 85 d->e_phentsize = eh32->e_phentsize; 86 d->e_phnum = eh32->e_phnum; 87 d->e_shentsize = eh32->e_shentsize; 88 d->e_shnum = eh32->e_shnum; 89 d->e_shstrndx = eh32->e_shstrndx; 90 91 return (d); 92 } 93 94 assert(ec == ELFCLASS64); 95 96 if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL) 97 return (NULL); 98 *d = *eh64; 99 100 return (d); 101 } 102 103 Elf32_Ehdr * 104 elf32_newehdr(Elf *e) 105 { 106 return (_libelf_ehdr(e, ELFCLASS32, 1)); 107 } 108 109 Elf64_Ehdr * 110 elf64_newehdr(Elf *e) 111 { 112 return (_libelf_ehdr(e, ELFCLASS64, 1)); 113 } 114 115 void * 116 gelf_newehdr(Elf *e, int ec) 117 { 118 if (e != NULL && 119 (ec == ELFCLASS32 || ec == ELFCLASS64)) 120 return (_libelf_ehdr(e, ec, 1)); 121 122 LIBELF_SET_ERROR(ARGUMENT, 0); 123 return (NULL); 124 } 125 126 int 127 gelf_update_ehdr(Elf *e, GElf_Ehdr *s) 128 { 129 int ec; 130 void *ehdr; 131 Elf32_Ehdr *eh32; 132 Elf64_Ehdr *eh64; 133 134 if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF || 135 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 136 LIBELF_SET_ERROR(ARGUMENT, 0); 137 return (0); 138 } 139 140 if (e->e_cmd == ELF_C_READ) { 141 LIBELF_SET_ERROR(MODE, 0); 142 return (0); 143 } 144 145 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 146 return (0); 147 148 (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); 149 150 if (ec == ELFCLASS64) { 151 eh64 = (Elf64_Ehdr *) ehdr; 152 *eh64 = *s; 153 return (1); 154 } 155 156 eh32 = (Elf32_Ehdr *) ehdr; 157 158 (void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident)); 159 160 eh32->e_type = s->e_type; 161 eh32->e_machine = s->e_machine; 162 eh32->e_version = s->e_version; 163 LIBELF_COPY_U32(eh32, s, e_entry); 164 LIBELF_COPY_U32(eh32, s, e_phoff); 165 LIBELF_COPY_U32(eh32, s, e_shoff); 166 eh32->e_flags = s->e_flags; 167 eh32->e_ehsize = s->e_ehsize; 168 eh32->e_phentsize = s->e_phentsize; 169 eh32->e_phnum = s->e_phnum; 170 eh32->e_shentsize = s->e_shentsize; 171 eh32->e_shnum = s->e_shnum; 172 eh32->e_shstrndx = s->e_shstrndx; 173 174 return (1); 175 } 176