1*0a6a1f1dSLionel Sambuc /* $NetBSD: elf_flag.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2006,2008-2009,2011 Joseph Koshy
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc * are met:
10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc *
16*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc */
28*0a6a1f1dSLionel Sambuc
29*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel Sambuc #include <libelf.h>
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc #include "_libelf.h"
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: elf_flag.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
36*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: elf_flag.c 2272 2011-12-03 17:07:31Z jkoshy ");
37*0a6a1f1dSLionel Sambuc
38*0a6a1f1dSLionel Sambuc unsigned int
elf_flagarhdr(Elf_Arhdr * a,Elf_Cmd c,unsigned int flags)39*0a6a1f1dSLionel Sambuc elf_flagarhdr(Elf_Arhdr *a, Elf_Cmd c, unsigned int flags)
40*0a6a1f1dSLionel Sambuc {
41*0a6a1f1dSLionel Sambuc unsigned int r;
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc if (a == NULL)
44*0a6a1f1dSLionel Sambuc return (0);
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc if ((c != ELF_C_SET && c != ELF_C_CLR) ||
47*0a6a1f1dSLionel Sambuc (flags & ~ELF_F_DIRTY) != 0) {
48*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
49*0a6a1f1dSLionel Sambuc return (0);
50*0a6a1f1dSLionel Sambuc }
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambuc if (c == ELF_C_SET)
53*0a6a1f1dSLionel Sambuc r = a->ar_flags |= flags;
54*0a6a1f1dSLionel Sambuc else
55*0a6a1f1dSLionel Sambuc r = a->ar_flags &= ~flags;
56*0a6a1f1dSLionel Sambuc
57*0a6a1f1dSLionel Sambuc return (r & LIBELF_F_API_MASK);
58*0a6a1f1dSLionel Sambuc }
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc unsigned int
elf_flagdata(Elf_Data * d,Elf_Cmd c,unsigned int flags)61*0a6a1f1dSLionel Sambuc elf_flagdata(Elf_Data *d, Elf_Cmd c, unsigned int flags)
62*0a6a1f1dSLionel Sambuc {
63*0a6a1f1dSLionel Sambuc unsigned int r;
64*0a6a1f1dSLionel Sambuc struct _Libelf_Data *ld;
65*0a6a1f1dSLionel Sambuc
66*0a6a1f1dSLionel Sambuc if (d == NULL)
67*0a6a1f1dSLionel Sambuc return (0);
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc if ((c != ELF_C_SET && c != ELF_C_CLR) ||
70*0a6a1f1dSLionel Sambuc (flags & ~ELF_F_DIRTY) != 0) {
71*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
72*0a6a1f1dSLionel Sambuc return (0);
73*0a6a1f1dSLionel Sambuc }
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc ld = (struct _Libelf_Data *) d;
76*0a6a1f1dSLionel Sambuc
77*0a6a1f1dSLionel Sambuc if (c == ELF_C_SET)
78*0a6a1f1dSLionel Sambuc r = ld->d_flags |= flags;
79*0a6a1f1dSLionel Sambuc else
80*0a6a1f1dSLionel Sambuc r = ld->d_flags &= ~flags;
81*0a6a1f1dSLionel Sambuc
82*0a6a1f1dSLionel Sambuc return (r & LIBELF_F_API_MASK);
83*0a6a1f1dSLionel Sambuc }
84*0a6a1f1dSLionel Sambuc
85*0a6a1f1dSLionel Sambuc unsigned int
elf_flagehdr(Elf * e,Elf_Cmd c,unsigned int flags)86*0a6a1f1dSLionel Sambuc elf_flagehdr(Elf *e, Elf_Cmd c, unsigned int flags)
87*0a6a1f1dSLionel Sambuc {
88*0a6a1f1dSLionel Sambuc int ec;
89*0a6a1f1dSLionel Sambuc void *ehdr;
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc if (e == NULL)
92*0a6a1f1dSLionel Sambuc return (0);
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc if ((c != ELF_C_SET && c != ELF_C_CLR) ||
95*0a6a1f1dSLionel Sambuc (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 ||
96*0a6a1f1dSLionel Sambuc ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
97*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
98*0a6a1f1dSLionel Sambuc return (0);
99*0a6a1f1dSLionel Sambuc }
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel Sambuc if (ec == ELFCLASS32)
102*0a6a1f1dSLionel Sambuc ehdr = e->e_u.e_elf.e_ehdr.e_ehdr32;
103*0a6a1f1dSLionel Sambuc else
104*0a6a1f1dSLionel Sambuc ehdr = e->e_u.e_elf.e_ehdr.e_ehdr64;
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc if (ehdr == NULL) {
107*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(SEQUENCE, 0);
108*0a6a1f1dSLionel Sambuc return (0);
109*0a6a1f1dSLionel Sambuc }
110*0a6a1f1dSLionel Sambuc
111*0a6a1f1dSLionel Sambuc return (elf_flagelf(e, c, flags));
112*0a6a1f1dSLionel Sambuc }
113*0a6a1f1dSLionel Sambuc
114*0a6a1f1dSLionel Sambuc unsigned int
elf_flagelf(Elf * e,Elf_Cmd c,unsigned int flags)115*0a6a1f1dSLionel Sambuc elf_flagelf(Elf *e, Elf_Cmd c, unsigned int flags)
116*0a6a1f1dSLionel Sambuc {
117*0a6a1f1dSLionel Sambuc int r;
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc if (e == NULL)
120*0a6a1f1dSLionel Sambuc return (0);
121*0a6a1f1dSLionel Sambuc
122*0a6a1f1dSLionel Sambuc if ((c != ELF_C_SET && c != ELF_C_CLR) ||
123*0a6a1f1dSLionel Sambuc (e->e_kind != ELF_K_ELF) ||
124*0a6a1f1dSLionel Sambuc (flags & ~(ELF_F_ARCHIVE | ELF_F_ARCHIVE_SYSV |
125*0a6a1f1dSLionel Sambuc ELF_F_DIRTY | ELF_F_LAYOUT)) != 0) {
126*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
127*0a6a1f1dSLionel Sambuc return (0);
128*0a6a1f1dSLionel Sambuc }
129*0a6a1f1dSLionel Sambuc
130*0a6a1f1dSLionel Sambuc if ((flags & ELF_F_ARCHIVE_SYSV) && (flags & ELF_F_ARCHIVE) == 0) {
131*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
132*0a6a1f1dSLionel Sambuc return (0);
133*0a6a1f1dSLionel Sambuc }
134*0a6a1f1dSLionel Sambuc
135*0a6a1f1dSLionel Sambuc if ((flags & ELF_F_ARCHIVE) && e->e_cmd != ELF_C_WRITE) {
136*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(MODE, 0);
137*0a6a1f1dSLionel Sambuc return (0);
138*0a6a1f1dSLionel Sambuc }
139*0a6a1f1dSLionel Sambuc
140*0a6a1f1dSLionel Sambuc if (c == ELF_C_SET)
141*0a6a1f1dSLionel Sambuc r = e->e_flags |= flags;
142*0a6a1f1dSLionel Sambuc else
143*0a6a1f1dSLionel Sambuc r = e->e_flags &= ~flags;
144*0a6a1f1dSLionel Sambuc return (r & LIBELF_F_API_MASK);
145*0a6a1f1dSLionel Sambuc }
146*0a6a1f1dSLionel Sambuc
147*0a6a1f1dSLionel Sambuc unsigned int
elf_flagphdr(Elf * e,Elf_Cmd c,unsigned int flags)148*0a6a1f1dSLionel Sambuc elf_flagphdr(Elf *e, Elf_Cmd c, unsigned int flags)
149*0a6a1f1dSLionel Sambuc {
150*0a6a1f1dSLionel Sambuc int ec;
151*0a6a1f1dSLionel Sambuc void *phdr;
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc if (e == NULL)
154*0a6a1f1dSLionel Sambuc return (0);
155*0a6a1f1dSLionel Sambuc
156*0a6a1f1dSLionel Sambuc if ((c != ELF_C_SET && c != ELF_C_CLR) ||
157*0a6a1f1dSLionel Sambuc (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 ||
158*0a6a1f1dSLionel Sambuc ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
159*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
160*0a6a1f1dSLionel Sambuc return (0);
161*0a6a1f1dSLionel Sambuc }
162*0a6a1f1dSLionel Sambuc
163*0a6a1f1dSLionel Sambuc if (ec == ELFCLASS32)
164*0a6a1f1dSLionel Sambuc phdr = e->e_u.e_elf.e_phdr.e_phdr32;
165*0a6a1f1dSLionel Sambuc else
166*0a6a1f1dSLionel Sambuc phdr = e->e_u.e_elf.e_phdr.e_phdr64;
167*0a6a1f1dSLionel Sambuc
168*0a6a1f1dSLionel Sambuc if (phdr == NULL) {
169*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(SEQUENCE, 0);
170*0a6a1f1dSLionel Sambuc return (0);
171*0a6a1f1dSLionel Sambuc }
172*0a6a1f1dSLionel Sambuc
173*0a6a1f1dSLionel Sambuc return (elf_flagelf(e, c, flags));
174*0a6a1f1dSLionel Sambuc }
175*0a6a1f1dSLionel Sambuc
176*0a6a1f1dSLionel Sambuc unsigned int
elf_flagscn(Elf_Scn * s,Elf_Cmd c,unsigned int flags)177*0a6a1f1dSLionel Sambuc elf_flagscn(Elf_Scn *s, Elf_Cmd c, unsigned int flags)
178*0a6a1f1dSLionel Sambuc {
179*0a6a1f1dSLionel Sambuc int r;
180*0a6a1f1dSLionel Sambuc
181*0a6a1f1dSLionel Sambuc if (s == NULL)
182*0a6a1f1dSLionel Sambuc return (0);
183*0a6a1f1dSLionel Sambuc
184*0a6a1f1dSLionel Sambuc if ((c != ELF_C_SET && c != ELF_C_CLR) ||
185*0a6a1f1dSLionel Sambuc (flags & ~ELF_F_DIRTY) != 0) {
186*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
187*0a6a1f1dSLionel Sambuc return (0);
188*0a6a1f1dSLionel Sambuc }
189*0a6a1f1dSLionel Sambuc
190*0a6a1f1dSLionel Sambuc if (c == ELF_C_SET)
191*0a6a1f1dSLionel Sambuc r = s->s_flags |= flags;
192*0a6a1f1dSLionel Sambuc else
193*0a6a1f1dSLionel Sambuc r = s->s_flags &= ~flags;
194*0a6a1f1dSLionel Sambuc return (r & LIBELF_F_API_MASK);
195*0a6a1f1dSLionel Sambuc }
196*0a6a1f1dSLionel Sambuc
197*0a6a1f1dSLionel Sambuc unsigned int
elf_flagshdr(Elf_Scn * s,Elf_Cmd c,unsigned int flags)198*0a6a1f1dSLionel Sambuc elf_flagshdr(Elf_Scn *s, Elf_Cmd c, unsigned int flags)
199*0a6a1f1dSLionel Sambuc {
200*0a6a1f1dSLionel Sambuc return (elf_flagscn(s, c, flags));
201*0a6a1f1dSLionel Sambuc }
202