elf_flag.c revision 225736
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: stable/9/lib/libelf/elf_flag.c 164190 2006-11-11 17:16:35Z jkoshy $"); 29 30#include <libelf.h> 31 32#include "_libelf.h" 33 34unsigned int 35elf_flagdata(Elf_Data *d, Elf_Cmd c, unsigned int flags) 36{ 37 Elf *e; 38 Elf_Scn *scn; 39 unsigned int r; 40 41 if (d == NULL) 42 return (0); 43 44 if ((c != ELF_C_SET && c != ELF_C_CLR) || (scn = d->d_scn) == NULL || 45 (e = scn->s_elf) == NULL || e->e_kind != ELF_K_ELF || 46 (flags & ~ELF_F_DIRTY) != 0) { 47 LIBELF_SET_ERROR(ARGUMENT, 0); 48 return (0); 49 } 50 51 if (c == ELF_C_SET) 52 r = scn->s_flags |= flags; 53 else 54 r = scn->s_flags &= ~flags; 55 56 return (r); 57} 58 59unsigned int 60elf_flagehdr(Elf *e, Elf_Cmd c, unsigned int flags) 61{ 62 int ec; 63 void *ehdr; 64 65 if (e == NULL) 66 return (0); 67 68 if ((c != ELF_C_SET && c != ELF_C_CLR) || 69 (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 || 70 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 71 LIBELF_SET_ERROR(ARGUMENT, 0); 72 return (0); 73 } 74 75 if (ec == ELFCLASS32) 76 ehdr = e->e_u.e_elf.e_ehdr.e_ehdr32; 77 else 78 ehdr = e->e_u.e_elf.e_ehdr.e_ehdr64; 79 80 if (ehdr == NULL) { 81 LIBELF_SET_ERROR(SEQUENCE, 0); 82 return (0); 83 } 84 85 return (elf_flagelf(e, c, flags)); 86} 87 88unsigned int 89elf_flagelf(Elf *e, Elf_Cmd c, unsigned int flags) 90{ 91 int r; 92 93 if (e == NULL) 94 return (0); 95 96 if ((c != ELF_C_SET && c != ELF_C_CLR) || 97 (e->e_kind != ELF_K_ELF) || 98 (flags & ~(ELF_F_DIRTY|ELF_F_LAYOUT)) != 0) { 99 LIBELF_SET_ERROR(ARGUMENT, 0); 100 return (0); 101 } 102 103 if (c == ELF_C_SET) 104 r = e->e_flags |= flags; 105 else 106 r = e->e_flags &= ~flags; 107 return (r); 108} 109 110unsigned int 111elf_flagphdr(Elf *e, Elf_Cmd c, unsigned int flags) 112{ 113 int ec; 114 void *phdr; 115 116 if (e == NULL) 117 return (0); 118 119 if ((c != ELF_C_SET && c != ELF_C_CLR) || 120 (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 || 121 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 122 LIBELF_SET_ERROR(ARGUMENT, 0); 123 return (0); 124 } 125 126 if (ec == ELFCLASS32) 127 phdr = e->e_u.e_elf.e_phdr.e_phdr32; 128 else 129 phdr = e->e_u.e_elf.e_phdr.e_phdr64; 130 131 if (phdr == NULL) { 132 LIBELF_SET_ERROR(SEQUENCE, 0); 133 return (0); 134 } 135 136 return (elf_flagelf(e, c, flags)); 137} 138 139unsigned int 140elf_flagscn(Elf_Scn *s, Elf_Cmd c, unsigned int flags) 141{ 142 int r; 143 144 if (s == NULL) 145 return (0); 146 147 if ((c != ELF_C_SET && c != ELF_C_CLR) || 148 (flags & ~ELF_F_DIRTY) != 0) { 149 LIBELF_SET_ERROR(ARGUMENT, 0); 150 return (0); 151 } 152 153 if (c == ELF_C_SET) 154 r = s->s_flags |= flags; 155 else 156 r = s->s_flags &= ~flags; 157 return (r); 158} 159 160unsigned int 161elf_flagshdr(Elf_Scn *s, Elf_Cmd c, unsigned int flags) 162{ 163 return (elf_flagscn(s, c, flags)); 164} 165