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