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