File: | src/lib/libelf/gelf_phdr.c |
Warning: | line 72, column 8 Although the value stored to 'eh32' is used in the enclosing expression, the value is never actually read from 'eh32' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /*- |
2 | * Copyright (c) 2006,2008 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 |
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. |
13 | * |
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 |
24 | * SUCH DAMAGE. |
25 | */ |
26 | |
27 | #include <gelf.h> |
28 | #include <libelf.h> |
29 | #include <limits.h> |
30 | #include <stdint.h> |
31 | |
32 | #include "_libelf.h" |
33 | |
34 | ELFTC_VCSID("$Id: gelf_phdr.c,v 1.1 2019/02/01 05:27:38 jsg Exp $"); |
35 | |
36 | Elf32_Phdr * |
37 | elf32_getphdr(Elf *e) |
38 | { |
39 | return (_libelf_getphdr(e, ELFCLASS321)); |
40 | } |
41 | |
42 | Elf64_Phdr * |
43 | elf64_getphdr(Elf *e) |
44 | { |
45 | return (_libelf_getphdr(e, ELFCLASS642)); |
46 | } |
47 | |
48 | GElf_Phdr * |
49 | gelf_getphdr(Elf *e, int index, GElf_Phdr *d) |
50 | { |
51 | int ec; |
52 | Elf32_Ehdr *eh32; |
53 | Elf64_Ehdr *eh64; |
54 | Elf32_Phdr *ep32; |
55 | Elf64_Phdr *ep64; |
56 | size_t phnum; |
57 | |
58 | if (d == NULL((void *)0) || e == NULL((void *)0) || |
59 | ((ec = e->e_class) != ELFCLASS321 && ec != ELFCLASS642) || |
60 | (e->e_kind != ELF_K_ELF) || index < 0 || |
61 | elf_getphdrnum(e, &phnum) < 0) { |
62 | LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) | (((0)) << 8)); } while (0); |
63 | return (NULL((void *)0)); |
64 | } |
65 | |
66 | if ((size_t)index >= phnum) { |
67 | LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) | (((0)) << 8)); } while (0); |
68 | return (NULL((void *)0)); |
69 | } |
70 | |
71 | if (ec == ELFCLASS321) { |
72 | if ((eh32 = _libelf_ehdr(e, ELFCLASS321, 0)) == NULL((void *)0) || |
Although the value stored to 'eh32' is used in the enclosing expression, the value is never actually read from 'eh32' | |
73 | ((ep32 = _libelf_getphdr(e, ELFCLASS321)) == NULL((void *)0))) |
74 | return (NULL((void *)0)); |
75 | |
76 | ep32 += index; |
77 | |
78 | d->p_type = ep32->p_type; |
79 | d->p_offset = ep32->p_offset; |
80 | d->p_vaddr = (Elf64_Addr) ep32->p_vaddr; |
81 | d->p_paddr = (Elf64_Addr) ep32->p_paddr; |
82 | d->p_filesz = (Elf64_Xword) ep32->p_filesz; |
83 | d->p_memsz = (Elf64_Xword) ep32->p_memsz; |
84 | d->p_flags = ep32->p_flags; |
85 | d->p_align = (Elf64_Xword) ep32->p_align; |
86 | |
87 | } else { |
88 | if ((eh64 = _libelf_ehdr(e, ELFCLASS642, 0)) == NULL((void *)0) || |
89 | (ep64 = _libelf_getphdr(e, ELFCLASS642)) == NULL((void *)0)) |
90 | return (NULL((void *)0)); |
91 | |
92 | ep64 += index; |
93 | |
94 | *d = *ep64; |
95 | } |
96 | |
97 | return (d); |
98 | } |
99 | |
100 | Elf32_Phdr * |
101 | elf32_newphdr(Elf *e, size_t count) |
102 | { |
103 | return (_libelf_newphdr(e, ELFCLASS321, count)); |
104 | } |
105 | |
106 | Elf64_Phdr * |
107 | elf64_newphdr(Elf *e, size_t count) |
108 | { |
109 | return (_libelf_newphdr(e, ELFCLASS642, count)); |
110 | } |
111 | |
112 | void * |
113 | gelf_newphdr(Elf *e, size_t count) |
114 | { |
115 | if (e == NULL((void *)0)) { |
116 | LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) | (((0)) << 8)); } while (0); |
117 | return (NULL((void *)0)); |
118 | } |
119 | return (_libelf_newphdr(e, e->e_class, count)); |
120 | } |
121 | |
122 | int |
123 | gelf_update_phdr(Elf *e, int ndx, GElf_Phdr *s) |
124 | { |
125 | int ec; |
126 | size_t phnum; |
127 | void *ehdr; |
128 | Elf32_Phdr *ph32; |
129 | Elf64_Phdr *ph64; |
130 | |
131 | if (s == NULL((void *)0) || e == NULL((void *)0) || e->e_kind != ELF_K_ELF || |
132 | ((ec = e->e_class) != ELFCLASS321 && ec != ELFCLASS642) || |
133 | elf_getphdrnum(e, &phnum) < 0) { |
134 | LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) | (((0)) << 8)); } while (0); |
135 | return (0); |
136 | } |
137 | |
138 | if (e->e_cmd == ELF_C_READ) { |
139 | LIBELF_SET_ERROR(MODE, 0)do { (_libelf.libelf_error) = (((ELF_E_MODE) & 0xFF) | (( (0)) << 8)); } while (0); |
140 | return (0); |
141 | } |
142 | |
143 | if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL((void *)0)) |
144 | return (0); |
145 | |
146 | if (ndx < 0 || (size_t)ndx > phnum) { |
147 | LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) | (((0)) << 8)); } while (0); |
148 | return (0); |
149 | } |
150 | |
151 | (void) elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY0x002U); |
152 | |
153 | if (ec == ELFCLASS642) { |
154 | ph64 = e->e_u.e_elf.e_phdr.e_phdr64 + ndx; |
155 | *ph64 = *s; |
156 | return (1); |
157 | } |
158 | |
159 | ph32 = e->e_u.e_elf.e_phdr.e_phdr32 + ndx; |
160 | |
161 | ph32->p_type = s->p_type; |
162 | ph32->p_flags = s->p_flags; |
163 | LIBELF_COPY_U32(ph32, s, p_offset)do { if ((s)->p_offset > 0xffffffffU) { do { (_libelf.libelf_error ) = (((ELF_E_RANGE) & 0xFF) | (((0)) << 8)); } while (0); return (0); } (ph32)->p_offset = (s)->p_offset & 0xFFFFFFFFU; } while (0); |
164 | LIBELF_COPY_U32(ph32, s, p_vaddr)do { if ((s)->p_vaddr > 0xffffffffU) { do { (_libelf.libelf_error ) = (((ELF_E_RANGE) & 0xFF) | (((0)) << 8)); } while (0); return (0); } (ph32)->p_vaddr = (s)->p_vaddr & 0xFFFFFFFFU; } while (0); |
165 | LIBELF_COPY_U32(ph32, s, p_paddr)do { if ((s)->p_paddr > 0xffffffffU) { do { (_libelf.libelf_error ) = (((ELF_E_RANGE) & 0xFF) | (((0)) << 8)); } while (0); return (0); } (ph32)->p_paddr = (s)->p_paddr & 0xFFFFFFFFU; } while (0); |
166 | LIBELF_COPY_U32(ph32, s, p_filesz)do { if ((s)->p_filesz > 0xffffffffU) { do { (_libelf.libelf_error ) = (((ELF_E_RANGE) & 0xFF) | (((0)) << 8)); } while (0); return (0); } (ph32)->p_filesz = (s)->p_filesz & 0xFFFFFFFFU; } while (0); |
167 | LIBELF_COPY_U32(ph32, s, p_memsz)do { if ((s)->p_memsz > 0xffffffffU) { do { (_libelf.libelf_error ) = (((ELF_E_RANGE) & 0xFF) | (((0)) << 8)); } while (0); return (0); } (ph32)->p_memsz = (s)->p_memsz & 0xFFFFFFFFU; } while (0); |
168 | LIBELF_COPY_U32(ph32, s, p_align)do { if ((s)->p_align > 0xffffffffU) { do { (_libelf.libelf_error ) = (((ELF_E_RANGE) & 0xFF) | (((0)) << 8)); } while (0); return (0); } (ph32)->p_align = (s)->p_align & 0xFFFFFFFFU; } while (0); |
169 | |
170 | return (1); |
171 | } |