1 /*- 2 * Copyright (c) 2006,2008-2009,2011 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 <libelf.h> 28 29 #include "_libelf.h" 30 31 ELFTC_VCSID("$Id: elf_flag.c 3174 2015-03-27 17:13:41Z emaste $"); 32 33 unsigned int 34 elf_flagarhdr(Elf_Arhdr *a, Elf_Cmd c, unsigned int flags) 35 { 36 unsigned int r; 37 38 if (a == NULL) 39 return (0); 40 41 if ((c != ELF_C_SET && c != ELF_C_CLR) || 42 (flags & ~ELF_F_DIRTY) != 0) { 43 LIBELF_SET_ERROR(ARGUMENT, 0); 44 return (0); 45 } 46 47 if (c == ELF_C_SET) 48 r = a->ar_flags |= flags; 49 else 50 r = a->ar_flags &= ~flags; 51 52 return (r & LIBELF_F_API_MASK); 53 } 54 55 unsigned int 56 elf_flagdata(Elf_Data *d, Elf_Cmd c, unsigned int flags) 57 { 58 unsigned int r; 59 struct _Libelf_Data *ld; 60 61 if (d == NULL) 62 return (0); 63 64 if ((c != ELF_C_SET && c != ELF_C_CLR) || 65 (flags & ~ELF_F_DIRTY) != 0) { 66 LIBELF_SET_ERROR(ARGUMENT, 0); 67 return (0); 68 } 69 70 ld = (struct _Libelf_Data *) d; 71 72 if (c == ELF_C_SET) 73 r = ld->d_flags |= flags; 74 else 75 r = ld->d_flags &= ~flags; 76 77 return (r & LIBELF_F_API_MASK); 78 } 79 80 unsigned int 81 elf_flagehdr(Elf *e, Elf_Cmd c, unsigned int flags) 82 { 83 int ec; 84 void *ehdr; 85 86 if (e == NULL) 87 return (0); 88 89 if ((c != ELF_C_SET && c != ELF_C_CLR) || 90 (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 || 91 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 92 LIBELF_SET_ERROR(ARGUMENT, 0); 93 return (0); 94 } 95 96 if (ec == ELFCLASS32) 97 ehdr = e->e_u.e_elf.e_ehdr.e_ehdr32; 98 else 99 ehdr = e->e_u.e_elf.e_ehdr.e_ehdr64; 100 101 if (ehdr == NULL) { 102 LIBELF_SET_ERROR(SEQUENCE, 0); 103 return (0); 104 } 105 106 return (elf_flagelf(e, c, flags)); 107 } 108 109 unsigned int 110 elf_flagelf(Elf *e, Elf_Cmd c, unsigned int flags) 111 { 112 unsigned int r; 113 114 if (e == NULL) 115 return (0); 116 117 if ((c != ELF_C_SET && c != ELF_C_CLR) || 118 (e->e_kind != ELF_K_ELF) || 119 (flags & ~(ELF_F_ARCHIVE | ELF_F_ARCHIVE_SYSV | 120 ELF_F_DIRTY | ELF_F_LAYOUT)) != 0) { 121 LIBELF_SET_ERROR(ARGUMENT, 0); 122 return (0); 123 } 124 125 if ((flags & ELF_F_ARCHIVE_SYSV) && (flags & ELF_F_ARCHIVE) == 0) { 126 LIBELF_SET_ERROR(ARGUMENT, 0); 127 return (0); 128 } 129 130 if ((flags & ELF_F_ARCHIVE) && e->e_cmd != ELF_C_WRITE) { 131 LIBELF_SET_ERROR(MODE, 0); 132 return (0); 133 } 134 135 if (c == ELF_C_SET) 136 r = e->e_flags |= flags; 137 else 138 r = e->e_flags &= ~flags; 139 return (r & LIBELF_F_API_MASK); 140 } 141 142 unsigned int 143 elf_flagphdr(Elf *e, Elf_Cmd c, unsigned int flags) 144 { 145 int ec; 146 void *phdr; 147 148 if (e == NULL) 149 return (0); 150 151 if ((c != ELF_C_SET && c != ELF_C_CLR) || 152 (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 || 153 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 154 LIBELF_SET_ERROR(ARGUMENT, 0); 155 return (0); 156 } 157 158 if (ec == ELFCLASS32) 159 phdr = e->e_u.e_elf.e_phdr.e_phdr32; 160 else 161 phdr = e->e_u.e_elf.e_phdr.e_phdr64; 162 163 if (phdr == NULL) { 164 LIBELF_SET_ERROR(SEQUENCE, 0); 165 return (0); 166 } 167 168 return (elf_flagelf(e, c, flags)); 169 } 170 171 unsigned int 172 elf_flagscn(Elf_Scn *s, Elf_Cmd c, unsigned int flags) 173 { 174 unsigned int r; 175 176 if (s == NULL) 177 return (0); 178 179 if ((c != ELF_C_SET && c != ELF_C_CLR) || 180 (flags & ~ELF_F_DIRTY) != 0) { 181 LIBELF_SET_ERROR(ARGUMENT, 0); 182 return (0); 183 } 184 185 if (c == ELF_C_SET) 186 r = s->s_flags |= flags; 187 else 188 r = s->s_flags &= ~flags; 189 return (r & LIBELF_F_API_MASK); 190 } 191 192 unsigned int 193 elf_flagshdr(Elf_Scn *s, Elf_Cmd c, unsigned int flags) 194 { 195 return (elf_flagscn(s, c, flags)); 196 } 197