1 /* $NetBSD: elf_flag.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2006,2008-2009,2011 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: elf_flag.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
36 ELFTC_VCSID("Id: elf_flag.c 2272 2011-12-03 17:07:31Z jkoshy ");
39 elf_flagarhdr(Elf_Arhdr
*a
, Elf_Cmd c
, unsigned int flags
)
46 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
47 (flags
& ~ELF_F_DIRTY
) != 0) {
48 LIBELF_SET_ERROR(ARGUMENT
, 0);
53 r
= a
->ar_flags
|= flags
;
55 r
= a
->ar_flags
&= ~flags
;
57 return (r
& LIBELF_F_API_MASK
);
61 elf_flagdata(Elf_Data
*d
, Elf_Cmd c
, unsigned int flags
)
64 struct _Libelf_Data
*ld
;
69 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
70 (flags
& ~ELF_F_DIRTY
) != 0) {
71 LIBELF_SET_ERROR(ARGUMENT
, 0);
75 ld
= (struct _Libelf_Data
*) d
;
78 r
= ld
->d_flags
|= flags
;
80 r
= ld
->d_flags
&= ~flags
;
82 return (r
& LIBELF_F_API_MASK
);
86 elf_flagehdr(Elf
*e
, Elf_Cmd c
, unsigned int flags
)
94 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
95 (e
->e_kind
!= ELF_K_ELF
) || (flags
& ~ELF_F_DIRTY
) != 0 ||
96 ((ec
= e
->e_class
) != ELFCLASS32
&& ec
!= ELFCLASS64
)) {
97 LIBELF_SET_ERROR(ARGUMENT
, 0);
101 if (ec
== ELFCLASS32
)
102 ehdr
= e
->e_u
.e_elf
.e_ehdr
.e_ehdr32
;
104 ehdr
= e
->e_u
.e_elf
.e_ehdr
.e_ehdr64
;
107 LIBELF_SET_ERROR(SEQUENCE
, 0);
111 return (elf_flagelf(e
, c
, flags
));
115 elf_flagelf(Elf
*e
, Elf_Cmd c
, unsigned int flags
)
122 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
123 (e
->e_kind
!= ELF_K_ELF
) ||
124 (flags
& ~(ELF_F_ARCHIVE
| ELF_F_ARCHIVE_SYSV
|
125 ELF_F_DIRTY
| ELF_F_LAYOUT
)) != 0) {
126 LIBELF_SET_ERROR(ARGUMENT
, 0);
130 if ((flags
& ELF_F_ARCHIVE_SYSV
) && (flags
& ELF_F_ARCHIVE
) == 0) {
131 LIBELF_SET_ERROR(ARGUMENT
, 0);
135 if ((flags
& ELF_F_ARCHIVE
) && e
->e_cmd
!= ELF_C_WRITE
) {
136 LIBELF_SET_ERROR(MODE
, 0);
141 r
= e
->e_flags
|= flags
;
143 r
= e
->e_flags
&= ~flags
;
144 return (r
& LIBELF_F_API_MASK
);
148 elf_flagphdr(Elf
*e
, Elf_Cmd c
, unsigned int flags
)
156 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
157 (e
->e_kind
!= ELF_K_ELF
) || (flags
& ~ELF_F_DIRTY
) != 0 ||
158 ((ec
= e
->e_class
) != ELFCLASS32
&& ec
!= ELFCLASS64
)) {
159 LIBELF_SET_ERROR(ARGUMENT
, 0);
163 if (ec
== ELFCLASS32
)
164 phdr
= e
->e_u
.e_elf
.e_phdr
.e_phdr32
;
166 phdr
= e
->e_u
.e_elf
.e_phdr
.e_phdr64
;
169 LIBELF_SET_ERROR(SEQUENCE
, 0);
173 return (elf_flagelf(e
, c
, flags
));
177 elf_flagscn(Elf_Scn
*s
, Elf_Cmd c
, unsigned int flags
)
184 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
185 (flags
& ~ELF_F_DIRTY
) != 0) {
186 LIBELF_SET_ERROR(ARGUMENT
, 0);
191 r
= s
->s_flags
|= flags
;
193 r
= s
->s_flags
&= ~flags
;
194 return (r
& LIBELF_F_API_MASK
);
198 elf_flagshdr(Elf_Scn
*s
, Elf_Cmd c
, unsigned int flags
)
200 return (elf_flagscn(s
, c
, flags
));