2 * Copyright (c) 2006,2008 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
36 elf_flagarhdr(Elf_Arhdr
*a
, Elf_Cmd c
, unsigned int flags
)
43 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
44 (flags
& ~ELF_F_DIRTY
) != 0) {
45 LIBELF_SET_ERROR(ARGUMENT
, 0);
50 r
= a
->ar_flags
|= flags
;
52 r
= a
->ar_flags
&= ~flags
;
58 elf_flagdata(Elf_Data
*d
, Elf_Cmd c
, unsigned int flags
)
67 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) || (scn
= d
->d_scn
) == NULL
||
68 (e
= scn
->s_elf
) == NULL
|| e
->e_kind
!= ELF_K_ELF
||
69 (flags
& ~ELF_F_DIRTY
) != 0) {
70 LIBELF_SET_ERROR(ARGUMENT
, 0);
75 r
= scn
->s_flags
|= flags
;
77 r
= scn
->s_flags
&= ~flags
;
83 elf_flagehdr(Elf
*e
, Elf_Cmd c
, unsigned int flags
)
91 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
92 (e
->e_kind
!= ELF_K_ELF
) || (flags
& ~ELF_F_DIRTY
) != 0 ||
93 ((ec
= e
->e_class
) != ELFCLASS32
&& ec
!= ELFCLASS64
)) {
94 LIBELF_SET_ERROR(ARGUMENT
, 0);
99 ehdr
= e
->e_u
.e_elf
.e_ehdr
.e_ehdr32
;
101 ehdr
= e
->e_u
.e_elf
.e_ehdr
.e_ehdr64
;
104 LIBELF_SET_ERROR(SEQUENCE
, 0);
108 return (elf_flagelf(e
, c
, flags
));
112 elf_flagelf(Elf
*e
, Elf_Cmd c
, unsigned int flags
)
119 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
120 (e
->e_kind
!= ELF_K_ELF
) ||
121 (flags
& ~(ELF_F_ARCHIVE
| ELF_F_ARCHIVE_SYSV
|
122 ELF_F_DIRTY
| ELF_F_LAYOUT
)) != 0) {
123 LIBELF_SET_ERROR(ARGUMENT
, 0);
127 if ((flags
& ELF_F_ARCHIVE_SYSV
) && (flags
& ELF_F_ARCHIVE
) == 0) {
128 LIBELF_SET_ERROR(ARGUMENT
, 0);
132 if ((flags
& ELF_F_ARCHIVE
) && e
->e_cmd
!= ELF_C_WRITE
) {
133 LIBELF_SET_ERROR(MODE
, 0);
138 r
= e
->e_flags
|= flags
;
140 r
= e
->e_flags
&= ~flags
;
145 elf_flagphdr(Elf
*e
, Elf_Cmd c
, unsigned int flags
)
153 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
154 (e
->e_kind
!= ELF_K_ELF
) || (flags
& ~ELF_F_DIRTY
) != 0 ||
155 ((ec
= e
->e_class
) != ELFCLASS32
&& ec
!= ELFCLASS64
)) {
156 LIBELF_SET_ERROR(ARGUMENT
, 0);
160 if (ec
== ELFCLASS32
)
161 phdr
= e
->e_u
.e_elf
.e_phdr
.e_phdr32
;
163 phdr
= e
->e_u
.e_elf
.e_phdr
.e_phdr64
;
166 LIBELF_SET_ERROR(SEQUENCE
, 0);
170 return (elf_flagelf(e
, c
, flags
));
174 elf_flagscn(Elf_Scn
*s
, Elf_Cmd c
, unsigned int flags
)
181 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
182 (flags
& ~ELF_F_DIRTY
) != 0) {
183 LIBELF_SET_ERROR(ARGUMENT
, 0);
188 r
= s
->s_flags
|= flags
;
190 r
= s
->s_flags
&= ~flags
;
195 elf_flagshdr(Elf_Scn
*s
, Elf_Cmd c
, unsigned int flags
)
197 return (elf_flagscn(s
, c
, flags
));