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 <libelf.h> 31164190Sjkoshy 32164190Sjkoshy#include "_libelf.h" 33164190Sjkoshy 34164190Sjkoshyunsigned int 35164190Sjkoshyelf_flagdata(Elf_Data *d, Elf_Cmd c, unsigned int flags) 36164190Sjkoshy{ 37164190Sjkoshy Elf *e; 38164190Sjkoshy Elf_Scn *scn; 39164190Sjkoshy unsigned int r; 40164190Sjkoshy 41164190Sjkoshy if (d == NULL) 42164190Sjkoshy return (0); 43164190Sjkoshy 44164190Sjkoshy if ((c != ELF_C_SET && c != ELF_C_CLR) || (scn = d->d_scn) == NULL || 45164190Sjkoshy (e = scn->s_elf) == NULL || e->e_kind != ELF_K_ELF || 46164190Sjkoshy (flags & ~ELF_F_DIRTY) != 0) { 47164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 48164190Sjkoshy return (0); 49164190Sjkoshy } 50164190Sjkoshy 51164190Sjkoshy if (c == ELF_C_SET) 52164190Sjkoshy r = scn->s_flags |= flags; 53164190Sjkoshy else 54164190Sjkoshy r = scn->s_flags &= ~flags; 55164190Sjkoshy 56164190Sjkoshy return (r); 57164190Sjkoshy} 58164190Sjkoshy 59164190Sjkoshyunsigned int 60164190Sjkoshyelf_flagehdr(Elf *e, Elf_Cmd c, unsigned int flags) 61164190Sjkoshy{ 62164190Sjkoshy int ec; 63164190Sjkoshy void *ehdr; 64164190Sjkoshy 65164190Sjkoshy if (e == NULL) 66164190Sjkoshy return (0); 67164190Sjkoshy 68164190Sjkoshy if ((c != ELF_C_SET && c != ELF_C_CLR) || 69164190Sjkoshy (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 || 70164190Sjkoshy ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 71164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 72164190Sjkoshy return (0); 73164190Sjkoshy } 74164190Sjkoshy 75164190Sjkoshy if (ec == ELFCLASS32) 76164190Sjkoshy ehdr = e->e_u.e_elf.e_ehdr.e_ehdr32; 77164190Sjkoshy else 78164190Sjkoshy ehdr = e->e_u.e_elf.e_ehdr.e_ehdr64; 79164190Sjkoshy 80164190Sjkoshy if (ehdr == NULL) { 81164190Sjkoshy LIBELF_SET_ERROR(SEQUENCE, 0); 82164190Sjkoshy return (0); 83164190Sjkoshy } 84164190Sjkoshy 85164190Sjkoshy return (elf_flagelf(e, c, flags)); 86164190Sjkoshy} 87164190Sjkoshy 88164190Sjkoshyunsigned int 89164190Sjkoshyelf_flagelf(Elf *e, Elf_Cmd c, unsigned int flags) 90164190Sjkoshy{ 91164190Sjkoshy int r; 92164190Sjkoshy 93164190Sjkoshy if (e == NULL) 94164190Sjkoshy return (0); 95164190Sjkoshy 96164190Sjkoshy if ((c != ELF_C_SET && c != ELF_C_CLR) || 97164190Sjkoshy (e->e_kind != ELF_K_ELF) || 98164190Sjkoshy (flags & ~(ELF_F_DIRTY|ELF_F_LAYOUT)) != 0) { 99164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 100164190Sjkoshy return (0); 101164190Sjkoshy } 102164190Sjkoshy 103164190Sjkoshy if (c == ELF_C_SET) 104164190Sjkoshy r = e->e_flags |= flags; 105164190Sjkoshy else 106164190Sjkoshy r = e->e_flags &= ~flags; 107164190Sjkoshy return (r); 108164190Sjkoshy} 109164190Sjkoshy 110164190Sjkoshyunsigned int 111164190Sjkoshyelf_flagphdr(Elf *e, Elf_Cmd c, unsigned int flags) 112164190Sjkoshy{ 113164190Sjkoshy int ec; 114164190Sjkoshy void *phdr; 115164190Sjkoshy 116164190Sjkoshy if (e == NULL) 117164190Sjkoshy return (0); 118164190Sjkoshy 119164190Sjkoshy if ((c != ELF_C_SET && c != ELF_C_CLR) || 120164190Sjkoshy (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 || 121164190Sjkoshy ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 122164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 123164190Sjkoshy return (0); 124164190Sjkoshy } 125164190Sjkoshy 126164190Sjkoshy if (ec == ELFCLASS32) 127164190Sjkoshy phdr = e->e_u.e_elf.e_phdr.e_phdr32; 128164190Sjkoshy else 129164190Sjkoshy phdr = e->e_u.e_elf.e_phdr.e_phdr64; 130164190Sjkoshy 131164190Sjkoshy if (phdr == NULL) { 132164190Sjkoshy LIBELF_SET_ERROR(SEQUENCE, 0); 133164190Sjkoshy return (0); 134164190Sjkoshy } 135164190Sjkoshy 136164190Sjkoshy return (elf_flagelf(e, c, flags)); 137164190Sjkoshy} 138164190Sjkoshy 139164190Sjkoshyunsigned int 140164190Sjkoshyelf_flagscn(Elf_Scn *s, Elf_Cmd c, unsigned int flags) 141164190Sjkoshy{ 142164190Sjkoshy int r; 143164190Sjkoshy 144164190Sjkoshy if (s == NULL) 145164190Sjkoshy return (0); 146164190Sjkoshy 147164190Sjkoshy if ((c != ELF_C_SET && c != ELF_C_CLR) || 148164190Sjkoshy (flags & ~ELF_F_DIRTY) != 0) { 149164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 150164190Sjkoshy return (0); 151164190Sjkoshy } 152164190Sjkoshy 153164190Sjkoshy if (c == ELF_C_SET) 154164190Sjkoshy r = s->s_flags |= flags; 155164190Sjkoshy else 156164190Sjkoshy r = s->s_flags &= ~flags; 157164190Sjkoshy return (r); 158164190Sjkoshy} 159164190Sjkoshy 160164190Sjkoshyunsigned int 161164190Sjkoshyelf_flagshdr(Elf_Scn *s, Elf_Cmd c, unsigned int flags) 162164190Sjkoshy{ 163164190Sjkoshy return (elf_flagscn(s, c, flags)); 164164190Sjkoshy} 165