elf_data.c revision 217833
1164190Sjkoshy/*- 2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy 3164190Sjkoshy * All rights reserved. 4164190Sjkoshy * 5164190Sjkoshy * Redistribution and use in source and binary forms, with or without 6164190Sjkoshy * modification, are permitted provided that the following conditions 7164190Sjkoshy * are met: 8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright 9164190Sjkoshy * notice, this list of conditions and the following disclaimer. 10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 11164190Sjkoshy * notice, this list of conditions and the following disclaimer in the 12164190Sjkoshy * documentation and/or other materials provided with the distribution. 13164190Sjkoshy * 14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17164190Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24164190Sjkoshy * SUCH DAMAGE. 25164190Sjkoshy */ 26164190Sjkoshy 27164190Sjkoshy#include <sys/cdefs.h> 28164190Sjkoshy__FBSDID("$FreeBSD: head/lib/libelf/elf_data.c 217833 2011-01-25 19:17:50Z kan $"); 29164190Sjkoshy 30164190Sjkoshy#include <assert.h> 31164190Sjkoshy#include <errno.h> 32164190Sjkoshy#include <libelf.h> 33164190Sjkoshy#include <stdlib.h> 34164190Sjkoshy 35164190Sjkoshy#include "_libelf.h" 36164190Sjkoshy 37164190Sjkoshy 38164190SjkoshyElf_Data * 39164190Sjkoshyelf_getdata(Elf_Scn *s, Elf_Data *d) 40164190Sjkoshy{ 41164190Sjkoshy Elf *e; 42164190Sjkoshy size_t fsz, msz, count; 43164190Sjkoshy int elfclass, elftype; 44164190Sjkoshy unsigned int sh_type; 45164190Sjkoshy uint64_t sh_align, sh_offset, sh_size; 46210338Skaiw int (*xlate)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); 47164190Sjkoshy 48164190Sjkoshy if (s == NULL || (e = s->s_elf) == NULL || e->e_kind != ELF_K_ELF || 49164190Sjkoshy (d != NULL && s != d->d_scn)) { 50164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 51164190Sjkoshy return (NULL); 52164190Sjkoshy } 53164190Sjkoshy 54164190Sjkoshy if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL) 55164190Sjkoshy return (d); 56164190Sjkoshy 57164190Sjkoshy if (d != NULL) 58164190Sjkoshy return (STAILQ_NEXT(d, d_next)); 59164190Sjkoshy 60164190Sjkoshy if (e->e_rawfile == NULL) { 61164190Sjkoshy LIBELF_SET_ERROR(SEQUENCE, 0); 62164190Sjkoshy return (NULL); 63164190Sjkoshy } 64164190Sjkoshy 65164190Sjkoshy elfclass = e->e_class; 66164190Sjkoshy 67164190Sjkoshy assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); 68164190Sjkoshy 69164190Sjkoshy if (elfclass == ELFCLASS32) { 70164190Sjkoshy sh_type = s->s_shdr.s_shdr32.sh_type; 71164190Sjkoshy sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset; 72164190Sjkoshy sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; 73164190Sjkoshy sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign; 74164190Sjkoshy } else { 75164190Sjkoshy sh_type = s->s_shdr.s_shdr64.sh_type; 76164190Sjkoshy sh_offset = s->s_shdr.s_shdr64.sh_offset; 77164190Sjkoshy sh_size = s->s_shdr.s_shdr64.sh_size; 78164190Sjkoshy sh_align = s->s_shdr.s_shdr64.sh_addralign; 79164190Sjkoshy } 80164190Sjkoshy 81210324Skaiw if (sh_type == SHT_NULL) 82210324Skaiw return (NULL); 83210324Skaiw 84164190Sjkoshy if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST || 85210324Skaiw elftype > ELF_T_LAST || (sh_type != SHT_NOBITS && 86210324Skaiw sh_offset + sh_size > (uint64_t) e->e_rawsize)) { 87164190Sjkoshy LIBELF_SET_ERROR(SECTION, 0); 88164190Sjkoshy return (NULL); 89164190Sjkoshy } 90164190Sjkoshy 91210324Skaiw if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize) 92210324Skaiw (elftype, (size_t) 1, e->e_version)) == 0) { 93164190Sjkoshy LIBELF_SET_ERROR(UNIMPL, 0); 94164190Sjkoshy return (NULL); 95164190Sjkoshy } 96164190Sjkoshy 97164190Sjkoshy if (sh_size % fsz) { 98164190Sjkoshy LIBELF_SET_ERROR(SECTION, 0); 99164190Sjkoshy return (NULL); 100164190Sjkoshy } 101164190Sjkoshy 102164190Sjkoshy count = sh_size / fsz; 103164190Sjkoshy 104164190Sjkoshy msz = _libelf_msize(elftype, elfclass, e->e_version); 105164190Sjkoshy 106164190Sjkoshy assert(msz > 0); 107164190Sjkoshy 108164190Sjkoshy if ((d = _libelf_allocate_data(s)) == NULL) 109164190Sjkoshy return (NULL); 110164190Sjkoshy 111210324Skaiw d->d_buf = NULL; 112164190Sjkoshy d->d_off = 0; 113164190Sjkoshy d->d_align = sh_align; 114164190Sjkoshy d->d_size = msz * count; 115164190Sjkoshy d->d_type = elftype; 116164190Sjkoshy d->d_version = e->e_version; 117164190Sjkoshy 118217833Skan if (sh_type == SHT_NOBITS || sh_size == 0) { 119217833Skan STAILQ_INSERT_TAIL(&s->s_data, d, d_next); 120210324Skaiw return (d); 121217833Skan } 122210324Skaiw 123210324Skaiw if ((d->d_buf = malloc(msz*count)) == NULL) { 124210324Skaiw (void) _libelf_release_data(d); 125210324Skaiw LIBELF_SET_ERROR(RESOURCE, 0); 126210324Skaiw return (NULL); 127210324Skaiw } 128210324Skaiw 129164190Sjkoshy d->d_flags |= LIBELF_F_MALLOCED; 130164190Sjkoshy 131164190Sjkoshy xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass); 132210338Skaiw if (!(*xlate)(d->d_buf, d->d_size, e->e_rawfile + sh_offset, count, 133210338Skaiw e->e_byteorder != LIBELF_PRIVATE(byteorder))) { 134210338Skaiw _libelf_release_data(d); 135210338Skaiw LIBELF_SET_ERROR(DATA, 0); 136210338Skaiw return (NULL); 137210338Skaiw } 138164190Sjkoshy 139210338Skaiw STAILQ_INSERT_TAIL(&s->s_data, d, d_next); 140210338Skaiw 141164190Sjkoshy return (d); 142164190Sjkoshy} 143164190Sjkoshy 144164190SjkoshyElf_Data * 145164190Sjkoshyelf_newdata(Elf_Scn *s) 146164190Sjkoshy{ 147164190Sjkoshy Elf *e; 148164190Sjkoshy Elf_Data *d; 149164190Sjkoshy 150164190Sjkoshy if (s == NULL || (e = s->s_elf) == NULL || 151164190Sjkoshy e->e_kind != ELF_K_ELF) { 152164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 153164190Sjkoshy return (NULL); 154164190Sjkoshy } 155164190Sjkoshy 156164190Sjkoshy /* 157164190Sjkoshy * elf_newdata() has to append a data descriptor, so 158164190Sjkoshy * bring in existing section data if not already present. 159164190Sjkoshy */ 160164190Sjkoshy if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data)) 161164190Sjkoshy if (elf_getdata(s, NULL) == NULL) 162164190Sjkoshy return (NULL); 163164190Sjkoshy 164210324Skaiw if ((d = _libelf_allocate_data(s)) == NULL) 165164190Sjkoshy return (NULL); 166164190Sjkoshy 167164190Sjkoshy STAILQ_INSERT_TAIL(&s->s_data, d, d_next); 168164190Sjkoshy 169164190Sjkoshy d->d_align = 1; 170164190Sjkoshy d->d_buf = NULL; 171164190Sjkoshy d->d_off = (uint64_t) ~0; 172164190Sjkoshy d->d_size = 0; 173164190Sjkoshy d->d_type = ELF_T_BYTE; 174164190Sjkoshy d->d_version = LIBELF_PRIVATE(version); 175164190Sjkoshy 176164190Sjkoshy (void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY); 177164190Sjkoshy 178164190Sjkoshy return (d); 179164190Sjkoshy} 180164190Sjkoshy 181164190Sjkoshy/* 182164190Sjkoshy * Retrieve a data descriptor for raw (untranslated) data for section 183164190Sjkoshy * `s'. 184164190Sjkoshy */ 185164190Sjkoshy 186164190SjkoshyElf_Data * 187164190Sjkoshyelf_rawdata(Elf_Scn *s, Elf_Data *d) 188164190Sjkoshy{ 189164190Sjkoshy Elf *e; 190164190Sjkoshy int elf_class; 191210324Skaiw uint32_t sh_type; 192164190Sjkoshy uint64_t sh_align, sh_offset, sh_size; 193164190Sjkoshy 194164190Sjkoshy if (s == NULL || (e = s->s_elf) == NULL || 195164190Sjkoshy e->e_kind != ELF_K_ELF || e->e_rawfile == NULL) { 196164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 197164190Sjkoshy return (NULL); 198164190Sjkoshy } 199164190Sjkoshy 200164190Sjkoshy if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL) 201164190Sjkoshy return (d); 202164190Sjkoshy 203164190Sjkoshy if (d != NULL) 204164190Sjkoshy return (STAILQ_NEXT(d, d_next)); 205164190Sjkoshy 206164190Sjkoshy elf_class = e->e_class; 207164190Sjkoshy 208164190Sjkoshy assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64); 209164190Sjkoshy 210164190Sjkoshy if (elf_class == ELFCLASS32) { 211210324Skaiw sh_type = s->s_shdr.s_shdr32.sh_type; 212164190Sjkoshy sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset; 213164190Sjkoshy sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; 214164190Sjkoshy sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign; 215164190Sjkoshy } else { 216210324Skaiw sh_type = s->s_shdr.s_shdr64.sh_type; 217167687Sjkoshy sh_offset = s->s_shdr.s_shdr64.sh_offset; 218167687Sjkoshy sh_size = s->s_shdr.s_shdr64.sh_size; 219167687Sjkoshy sh_align = s->s_shdr.s_shdr64.sh_addralign; 220164190Sjkoshy } 221164190Sjkoshy 222210324Skaiw if (sh_type == SHT_NULL) 223210324Skaiw return (NULL); 224210324Skaiw 225164190Sjkoshy if ((d = _libelf_allocate_data(s)) == NULL) 226164190Sjkoshy return (NULL); 227164190Sjkoshy 228210324Skaiw d->d_buf = sh_type == SHT_NOBITS ? NULL : e->e_rawfile + sh_offset; 229164190Sjkoshy d->d_off = 0; 230164190Sjkoshy d->d_align = sh_align; 231164190Sjkoshy d->d_size = sh_size; 232164190Sjkoshy d->d_type = ELF_T_BYTE; 233164190Sjkoshy d->d_version = e->e_version; 234164190Sjkoshy 235164190Sjkoshy STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next); 236164190Sjkoshy 237164190Sjkoshy return (d); 238164190Sjkoshy} 239