1/*- 2 * Copyright (c) 2006 Joseph Koshy 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <assert.h> 28#include <limits.h> 29 30#include "gelf.h" 31#include "_libelf.h" 32 33GElf_Dyn * 34gelf_getdyn(Elf_Data *d, int ndx, GElf_Dyn *dst) 35{ 36 int ec; 37 Elf *e; 38 Elf_Scn *scn; 39 Elf32_Dyn *dyn32; 40 Elf64_Dyn *dyn64; 41 size_t msz; 42 uint32_t sh_type; 43 44 if (d == NULL || ndx < 0 || dst == NULL || 45 (scn = d->d_scn) == NULL || 46 (e = scn->s_elf) == NULL) { 47 LIBELF_SET_ERROR(ARGUMENT, 0); 48 return (NULL); 49 } 50 51 ec = e->e_class; 52 assert(ec == ELFCLASS32 || ec == ELFCLASS64); 53 54 if (ec == ELFCLASS32) 55 sh_type = scn->s_shdr.s_shdr32.sh_type; 56 else 57 sh_type = scn->s_shdr.s_shdr64.sh_type; 58 59 if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) { 60 LIBELF_SET_ERROR(ARGUMENT, 0); 61 return (NULL); 62 } 63 64 msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); 65 66 assert(msz > 0); 67 68 if (msz * ndx >= d->d_size) { 69 LIBELF_SET_ERROR(ARGUMENT, 0); 70 return (NULL); 71 } 72 73 if (ec == ELFCLASS32) { 74 dyn32 = (Elf32_Dyn *) d->d_buf + ndx; 75 76 dst->d_tag = dyn32->d_tag; 77 dst->d_un.d_val = (Elf64_Xword) dyn32->d_un.d_val; 78 79 } else { 80 81 dyn64 = (Elf64_Dyn *) d->d_buf + ndx; 82 83 *dst = *dyn64; 84 } 85 86 return (dst); 87} 88 89int 90gelf_update_dyn(Elf_Data *d, int ndx, GElf_Dyn *ds) 91{ 92 int ec; 93 Elf *e; 94 Elf_Scn *scn; 95 Elf32_Dyn *dyn32; 96 Elf64_Dyn *dyn64; 97 size_t msz; 98 uint32_t sh_type; 99 100 if (d == NULL || ndx < 0 || ds == NULL || 101 (scn = d->d_scn) == NULL || 102 (e = scn->s_elf) == NULL) { 103 LIBELF_SET_ERROR(ARGUMENT, 0); 104 return (0); 105 } 106 107 ec = e->e_class; 108 assert(ec == ELFCLASS32 || ec == ELFCLASS64); 109 110 if (ec == ELFCLASS32) 111 sh_type = scn->s_shdr.s_shdr32.sh_type; 112 else 113 sh_type = scn->s_shdr.s_shdr64.sh_type; 114 115 if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) { 116 LIBELF_SET_ERROR(ARGUMENT, 0); 117 return (0); 118 } 119 120 msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); 121 assert(msz > 0); 122 123 if (msz * ndx >= d->d_size) { 124 LIBELF_SET_ERROR(ARGUMENT, 0); 125 return (0); 126 } 127 128 if (ec == ELFCLASS32) { 129 dyn32 = (Elf32_Dyn *) d->d_buf + ndx; 130 131 LIBELF_COPY_S32(dyn32, ds, d_tag); 132 LIBELF_COPY_U32(dyn32, ds, d_un.d_val); 133 } else { 134 dyn64 = (Elf64_Dyn *) d->d_buf + ndx; 135 136 *dyn64 = *ds; 137 } 138 139 return (1); 140} 141