2 * Copyright (c) 2006 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
33 elf_flagdata(Elf_Data
*d
, Elf_Cmd c
, unsigned int flags
)
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);
50 r
= scn
->s_flags
|= flags
;
52 r
= scn
->s_flags
&= ~flags
;
58 elf_flagehdr(Elf
*e
, Elf_Cmd c
, unsigned int flags
)
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);
74 ehdr
= e
->e_u
.e_elf
.e_ehdr
.e_ehdr32
;
76 ehdr
= e
->e_u
.e_elf
.e_ehdr
.e_ehdr64
;
79 LIBELF_SET_ERROR(SEQUENCE
, 0);
83 return (elf_flagelf(e
, c
, flags
));
87 elf_flagelf(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
) ||
96 (flags
& ~(ELF_F_DIRTY
|ELF_F_LAYOUT
)) != 0) {
97 LIBELF_SET_ERROR(ARGUMENT
, 0);
102 r
= e
->e_flags
|= flags
;
104 r
= e
->e_flags
&= ~flags
;
109 elf_flagphdr(Elf
*e
, Elf_Cmd c
, unsigned int flags
)
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);
124 if (ec
== ELFCLASS32
)
125 phdr
= e
->e_u
.e_elf
.e_phdr
.e_phdr32
;
127 phdr
= e
->e_u
.e_elf
.e_phdr
.e_phdr64
;
130 LIBELF_SET_ERROR(SEQUENCE
, 0);
134 return (elf_flagelf(e
, c
, flags
));
138 elf_flagscn(Elf_Scn
*s
, Elf_Cmd c
, unsigned int flags
)
145 if ((c
!= ELF_C_SET
&& c
!= ELF_C_CLR
) ||
146 (flags
& ~ELF_F_DIRTY
) != 0) {
147 LIBELF_SET_ERROR(ARGUMENT
, 0);
152 r
= s
->s_flags
|= flags
;
154 r
= s
->s_flags
&= ~flags
;
159 elf_flagshdr(Elf_Scn
*s
, Elf_Cmd c
, unsigned int flags
)
161 return (elf_flagscn(s
, c
, flags
));