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 28#include <assert.h> 29#include <errno.h> 30#include "libelf.h" 31#include <stdlib.h> 32 33#include "_libelf.h" 34 35 36Elf_Data * 37elf_getdata(Elf_Scn *s, Elf_Data *d) 38{ 39 Elf *e; 40 char *dst; 41 size_t fsz, msz, count; 42 int elfclass, elftype; 43 unsigned int sh_type; 44 uint64_t sh_align, sh_offset, sh_size; 45 void (*xlate)(char *_d, char *_s, size_t _c, int _swap); 46 47 if (s == NULL || (e = s->s_elf) == NULL || e->e_kind != ELF_K_ELF || 48 (d != NULL && s != d->d_scn)) { 49 LIBELF_SET_ERROR(ARGUMENT, 0); 50 return (NULL); 51 } 52 53 if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL) 54 return (d); 55 56 if (d != NULL) 57 return (STAILQ_NEXT(d, d_next)); 58 59 if (e->e_rawfile == NULL) { 60 LIBELF_SET_ERROR(SEQUENCE, 0); 61 return (NULL); 62 } 63 64 elfclass = e->e_class; 65 66 assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); 67 68 if (elfclass == ELFCLASS32) { 69 sh_type = s->s_shdr.s_shdr32.sh_type; 70 sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset; 71 sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; 72 sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign; 73 } else { 74 sh_type = s->s_shdr.s_shdr64.sh_type; 75 sh_offset = s->s_shdr.s_shdr64.sh_offset; 76 sh_size = s->s_shdr.s_shdr64.sh_size; 77 sh_align = s->s_shdr.s_shdr64.sh_addralign; 78 } 79 80 if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST || 81 elftype > ELF_T_LAST || 82 sh_offset + sh_size > (uint64_t) e->e_rawsize) { 83 LIBELF_SET_ERROR(SECTION, 0); 84 return (NULL); 85 } 86 87 if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)(elftype, 88 (size_t) 1, e->e_version)) == 0) { 89 LIBELF_SET_ERROR(UNIMPL, 0); 90 return (NULL); 91 } 92 93 94 if (sh_size % fsz) { 95 LIBELF_SET_ERROR(SECTION, 0); 96 return (NULL); 97 } 98 99 count = sh_size / fsz; 100 101 msz = _libelf_msize(elftype, elfclass, e->e_version); 102 103 assert(msz > 0); 104 105 if ((dst = malloc(msz*count)) == NULL) { 106 LIBELF_SET_ERROR(RESOURCE, 0); 107 return (NULL); 108 } 109 110 if ((d = _libelf_allocate_data(s)) == NULL) 111 return (NULL); 112 113 d->d_buf = dst; 114 d->d_off = 0; 115 d->d_align = sh_align; 116 d->d_size = msz * count; 117 d->d_type = elftype; 118 d->d_version = e->e_version; 119 120 d->d_flags |= LIBELF_F_MALLOCED; 121 STAILQ_INSERT_TAIL(&s->s_data, d, d_next); 122 123 xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass); 124 (*xlate)(d->d_buf, e->e_rawfile + sh_offset, count, e->e_byteorder != 125 LIBELF_PRIVATE(byteorder)); 126 127 return (d); 128} 129 130Elf_Data * 131elf_newdata(Elf_Scn *s) 132{ 133 Elf *e; 134 Elf_Data *d; 135 136 if (s == NULL || (e = s->s_elf) == NULL || 137 e->e_kind != ELF_K_ELF) { 138 LIBELF_SET_ERROR(ARGUMENT, 0); 139 return (NULL); 140 } 141 142 /* 143 * elf_newdata() has to append a data descriptor, so 144 * bring in existing section data if not already present. 145 */ 146 if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data)) 147 if (elf_getdata(s, NULL) == NULL) 148 return (NULL); 149 150 if ((d = malloc(sizeof(Elf_Data))) == NULL) { 151 LIBELF_SET_ERROR(RESOURCE, errno); 152 return (NULL); 153 } 154 155 STAILQ_INSERT_TAIL(&s->s_data, d, d_next); 156 d->d_flags = 0; 157 d->d_scn = s; 158 159 d->d_align = 1; 160 d->d_buf = NULL; 161 d->d_off = (uint64_t) ~0; 162 d->d_size = 0; 163 d->d_type = ELF_T_BYTE; 164 d->d_version = LIBELF_PRIVATE(version); 165 166 (void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY); 167 168 return (d); 169} 170 171/* 172 * Retrieve a data descriptor for raw (untranslated) data for section 173 * `s'. 174 */ 175 176Elf_Data * 177elf_rawdata(Elf_Scn *s, Elf_Data *d) 178{ 179 Elf *e; 180 int elf_class; 181 uint64_t sh_align, sh_offset, sh_size; 182 183 if (s == NULL || (e = s->s_elf) == NULL || 184 e->e_kind != ELF_K_ELF || e->e_rawfile == NULL) { 185 LIBELF_SET_ERROR(ARGUMENT, 0); 186 return (NULL); 187 } 188 189 if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL) 190 return (d); 191 192 if (d != NULL) 193 return (STAILQ_NEXT(d, d_next)); 194 195 elf_class = e->e_class; 196 197 assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64); 198 199 if (elf_class == ELFCLASS32) { 200 sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset; 201 sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; 202 sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign; 203 } else { 204 sh_offset = s->s_shdr.s_shdr64.sh_offset; 205 sh_size = s->s_shdr.s_shdr64.sh_size; 206 sh_align = s->s_shdr.s_shdr64.sh_addralign; 207 } 208 209 if ((d = _libelf_allocate_data(s)) == NULL) 210 return (NULL); 211 212 d->d_buf = e->e_rawfile + sh_offset; 213 d->d_off = 0; 214 d->d_align = sh_align; 215 d->d_size = sh_size; 216 d->d_type = ELF_T_BYTE; 217 d->d_version = e->e_version; 218 219 STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next); 220 221 return (d); 222} 223