Deleted Added
full compact
elf_shnum.c (164190) elf_shnum.c (165535)
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

--- 11 unchanged lines hidden (view full) ---

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#include <sys/cdefs.h>
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

--- 11 unchanged lines hidden (view full) ---

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#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/libelf/elf_shnum.c 164190 2006-11-11 17:16:35Z jkoshy $");
28__FBSDID("$FreeBSD: head/lib/libelf/elf_shnum.c 165535 2006-12-25 02:22:22Z jkoshy $");
29
30#include <ar.h>
31#include <libelf.h>
32
33#include "_libelf.h"
34
35int
29
30#include <ar.h>
31#include <libelf.h>
32
33#include "_libelf.h"
34
35int
36_libelf_getshnum(Elf *e, void *eh, int ec, size_t *shnum)
37{
38 Elf64_Off off;
39 Elf_Scn *scn;
40 void *sh;
41 size_t n;
42
43 if (ec == ELFCLASS32) {
44 n = ((Elf32_Ehdr *) eh)->e_shnum;
45 off = (Elf64_Off) ((Elf32_Ehdr *) eh)->e_shoff;
46 } else {
47 n = ((Elf64_Ehdr *) eh)->e_shnum;
48 off = ((Elf64_Ehdr *) eh)->e_shoff;
49 }
50
51 if (n != 0) {
52 *shnum = n;
53 return (1);
54 }
55
56 if (off == 0L) {
57 *shnum = (size_t) 0;
58 return (1);
59 }
60
61 /*
62 * If 'e_shnum' is zero and 'e_shoff' is non-zero, the file is
63 * using extended section numbering, and the true section
64 * number is kept in the 'sh_size' field of the section header
65 * at offset SHN_UNDEF.
66 */
67 if ((scn = elf_getscn(e, (size_t) SHN_UNDEF)) == NULL)
68 return (0);
69 if ((sh = _libelf_getshdr(scn, ec)) == NULL)
70 return (0);
71
72 if (ec == ELFCLASS32)
73 *shnum = ((Elf32_Shdr *) sh)->sh_size;
74 else
75 *shnum = ((Elf64_Shdr *) sh)->sh_size;
76
77 return (1);
78}
79
80int
81_libelf_setshnum(Elf *e, void *eh, int ec, size_t shnum)
82{
83 Elf_Scn *scn;
84 void *sh;
85
86 if (shnum < SHN_LORESERVE) {
87 if (ec == ELFCLASS32)
88 ((Elf32_Ehdr *) eh)->e_shnum = shnum;
89 else
90 ((Elf64_Ehdr *) eh)->e_shnum = shnum;
91 return (1);
92 }
93
94 if ((scn = elf_getscn(e, (size_t) SHN_UNDEF)) == NULL)
95 return (0);
96 if ((sh = _libelf_getshdr(scn, ec)) == NULL)
97 return (0);
98
99 if (ec == ELFCLASS32)
100 ((Elf32_Shdr *) sh)->sh_size = shnum;
101 else
102 ((Elf64_Shdr *) sh)->sh_size = shnum;
103
104 (void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
105
106 return (1);
107}
108
109int
110elf_getshnum(Elf *e, size_t *shnum)
111{
112 void *eh;
113 int ec;
114
115 if (e == NULL || e->e_kind != ELF_K_ELF ||
36elf_getshnum(Elf *e, size_t *shnum)
37{
38 void *eh;
39 int ec;
40
41 if (e == NULL || e->e_kind != ELF_K_ELF ||
116 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
117 ((eh = _libelf_ehdr(e, ec, 0)) == NULL)) {
42 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
118 LIBELF_SET_ERROR(ARGUMENT, 0);
119 return (0);
120 }
121
43 LIBELF_SET_ERROR(ARGUMENT, 0);
44 return (0);
45 }
46
122 return (_libelf_getshnum(e, eh, ec, shnum));
47 if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
48 return (0);
49
50 *shnum = e->e_u.e_elf.e_nscn;
51
52 return (1);
123}
53}