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$"); 29164190Sjkoshy 30164190Sjkoshy#include <sys/limits.h> 31164190Sjkoshy 32164190Sjkoshy#include <assert.h> 33164190Sjkoshy#include <gelf.h> 34164190Sjkoshy#include <libelf.h> 35164190Sjkoshy 36164190Sjkoshy#include "_libelf.h" 37164190Sjkoshy 38164190SjkoshyElf32_Shdr * 39164190Sjkoshyelf32_getshdr(Elf_Scn *s) 40164190Sjkoshy{ 41164190Sjkoshy return (_libelf_getshdr(s, ELFCLASS32)); 42164190Sjkoshy} 43164190Sjkoshy 44164190SjkoshyElf64_Shdr * 45164190Sjkoshyelf64_getshdr(Elf_Scn *s) 46164190Sjkoshy{ 47164190Sjkoshy return (_libelf_getshdr(s, ELFCLASS64)); 48164190Sjkoshy} 49164190Sjkoshy 50164190SjkoshyGElf_Shdr * 51164190Sjkoshygelf_getshdr(Elf_Scn *s, GElf_Shdr *d) 52164190Sjkoshy{ 53164190Sjkoshy int ec; 54164190Sjkoshy void *sh; 55164190Sjkoshy Elf32_Shdr *sh32; 56164190Sjkoshy Elf64_Shdr *sh64; 57164190Sjkoshy 58164190Sjkoshy if (d == NULL) { 59164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 60164190Sjkoshy return (NULL); 61164190Sjkoshy } 62164190Sjkoshy 63164190Sjkoshy if ((sh = _libelf_getshdr(s, ELFCLASSNONE)) == NULL) 64164190Sjkoshy return (NULL); 65164190Sjkoshy 66164190Sjkoshy ec = s->s_elf->e_class; 67164190Sjkoshy assert(ec == ELFCLASS32 || ec == ELFCLASS64); 68164190Sjkoshy 69164190Sjkoshy if (ec == ELFCLASS32) { 70164190Sjkoshy sh32 = (Elf32_Shdr *) sh; 71164190Sjkoshy 72164190Sjkoshy d->sh_name = sh32->sh_name; 73164190Sjkoshy d->sh_type = sh32->sh_type; 74164190Sjkoshy d->sh_flags = (Elf64_Xword) sh32->sh_flags; 75164190Sjkoshy d->sh_addr = (Elf64_Addr) sh32->sh_addr; 76164190Sjkoshy d->sh_offset = (Elf64_Off) sh32->sh_offset; 77164190Sjkoshy d->sh_size = (Elf64_Xword) sh32->sh_size; 78164190Sjkoshy d->sh_link = sh32->sh_link; 79164190Sjkoshy d->sh_info = sh32->sh_info; 80164190Sjkoshy d->sh_addralign = (Elf64_Xword) sh32->sh_addralign; 81164190Sjkoshy d->sh_entsize = (Elf64_Xword) sh32->sh_entsize; 82164190Sjkoshy } else { 83164190Sjkoshy sh64 = (Elf64_Shdr *) sh; 84164190Sjkoshy *d = *sh64; 85164190Sjkoshy } 86164190Sjkoshy 87164190Sjkoshy return (d); 88164190Sjkoshy} 89164190Sjkoshy 90164190Sjkoshyint 91164190Sjkoshygelf_update_shdr(Elf_Scn *scn, GElf_Shdr *s) 92164190Sjkoshy{ 93164190Sjkoshy int ec; 94164190Sjkoshy Elf *e; 95164190Sjkoshy Elf32_Shdr *sh32; 96164190Sjkoshy 97164190Sjkoshy 98164190Sjkoshy if (s == NULL || scn == NULL || (e = scn->s_elf) == NULL || 99164190Sjkoshy e->e_kind != ELF_K_ELF || 100164190Sjkoshy ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 101164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 102164190Sjkoshy return (0); 103164190Sjkoshy } 104164190Sjkoshy 105164190Sjkoshy if (e->e_cmd == ELF_C_READ) { 106164190Sjkoshy LIBELF_SET_ERROR(MODE, 0); 107164190Sjkoshy return (0); 108164190Sjkoshy } 109164190Sjkoshy 110210325Skaiw (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY); 111210325Skaiw 112164190Sjkoshy if (ec == ELFCLASS64) { 113164190Sjkoshy scn->s_shdr.s_shdr64 = *s; 114164190Sjkoshy return (1); 115164190Sjkoshy } 116164190Sjkoshy 117164190Sjkoshy sh32 = &scn->s_shdr.s_shdr32; 118164190Sjkoshy 119164190Sjkoshy sh32->sh_name = s->sh_name; 120164190Sjkoshy sh32->sh_type = s->sh_type; 121164190Sjkoshy LIBELF_COPY_U32(sh32, s, sh_flags); 122164190Sjkoshy LIBELF_COPY_U32(sh32, s, sh_addr); 123164190Sjkoshy LIBELF_COPY_U32(sh32, s, sh_offset); 124164190Sjkoshy LIBELF_COPY_U32(sh32, s, sh_size); 125164190Sjkoshy sh32->sh_link = s->sh_link; 126164190Sjkoshy sh32->sh_info = s->sh_info; 127164190Sjkoshy LIBELF_COPY_U32(sh32, s, sh_addralign); 128164190Sjkoshy LIBELF_COPY_U32(sh32, s, sh_entsize); 129164190Sjkoshy 130164190Sjkoshy return (1); 131164190Sjkoshy} 132