| File: | src/lib/libelf/libelf_ehdr.c |
| Warning: | line 76, column 7 Although the value stored to 'shtype' is used in the enclosing expression, the value is never actually read from 'shtype' |
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 <assert.h> |
| 28 | #include <gelf.h> |
| 29 | #include <libelf.h> |
| 30 | #include <stdlib.h> |
| 31 | |
| 32 | #include "_libelf.h" |
| 33 | |
| 34 | ELFTC_VCSID("$Id: libelf_ehdr.c,v 1.3 2020/05/18 06:46:23 jsg Exp $")__asm__(".ident\t\"" "$Id: libelf_ehdr.c,v 1.3 2020/05/18 06:46:23 jsg Exp $" "\""); |
| 35 | |
| 36 | /* |
| 37 | * Retrieve counts for sections, phdrs and the section string table index |
| 38 | * from section header #0 of the ELF object. |
| 39 | */ |
| 40 | static int |
| 41 | _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum, |
| 42 | uint16_t strndx) |
| 43 | { |
| 44 | size_t fsz; |
| 45 | Elf_Scn *scn; |
| 46 | uint32_t shtype; |
| 47 | _libelf_translator_function *xlator; |
| 48 | |
| 49 | assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn))(((((&e->e_u.e_elf.e_scn)->stqh_first) == ((void *) 0))) ? (void)0 : __assert2("/usr/src/lib/libelf/libelf_ehdr.c" , 49, __func__, "STAILQ_EMPTY(&e->e_u.e_elf.e_scn)")); |
| 50 | |
| 51 | fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1); |
| 52 | assert(fsz > 0)((fsz > 0) ? (void)0 : __assert2("/usr/src/lib/libelf/libelf_ehdr.c" , 52, __func__, "fsz > 0")); |
| 53 | |
| 54 | if (shoff + fsz < shoff) { /* Numeric overflow. */ |
| 55 | LIBELF_SET_ERROR(HEADER, 0)do { (_libelf.libelf_error) = (((ELF_E_HEADER) & 0xFF) | ( ((0)) << 8)); } while (0); |
| 56 | return (0); |
| 57 | } |
| 58 | |
| 59 | if ((uint64_t) e->e_rawsize < shoff + fsz) { |
| 60 | LIBELF_SET_ERROR(HEADER, 0)do { (_libelf.libelf_error) = (((ELF_E_HEADER) & 0xFF) | ( ((0)) << 8)); } while (0); |
| 61 | return (0); |
| 62 | } |
| 63 | |
| 64 | if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL((void *)0)) |
| 65 | return (0); |
| 66 | |
| 67 | xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec, |
| 68 | _libelf_elfmachine(e)); |
| 69 | (*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr), |
| 70 | (unsigned char *) e->e_rawfile + shoff, (size_t) 1, |
| 71 | e->e_byteorder != LIBELF_PRIVATE(byteorder)(_libelf.libelf_byteorder)); |
| 72 | |
| 73 | #define GET_SHDR_MEMBER(M) ((ec == ELFCLASS321) ? scn->s_shdr.s_shdr32.M : \ |
| 74 | scn->s_shdr.s_shdr64.M) |
| 75 | |
| 76 | if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL0) { |
Although the value stored to 'shtype' is used in the enclosing expression, the value is never actually read from 'shtype' | |
| 77 | LIBELF_SET_ERROR(SECTION, 0)do { (_libelf.libelf_error) = (((ELF_E_SECTION) & 0xFF) | (((0)) << 8)); } while (0); |
| 78 | return (0); |
| 79 | } |
| 80 | |
| 81 | e->e_u.e_elf.e_nscn = (size_t) GET_SHDR_MEMBER(sh_size); |
| 82 | e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM0xffff) ? phnum : |
| 83 | GET_SHDR_MEMBER(sh_info); |
| 84 | e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX0xffff) ? strndx : |
| 85 | GET_SHDR_MEMBER(sh_link); |
| 86 | #undef GET_SHDR_MEMBER |
| 87 | |
| 88 | return (1); |
| 89 | } |
| 90 | |
| 91 | #define EHDR_INIT(E,SZ)do { ElfSZ_Ehdr *eh = (E); eh->e_ident[0] = 0x7f; eh->e_ident [1] = 'E'; eh->e_ident[2] = 'L'; eh->e_ident[3] = 'F'; eh ->e_ident[4] = ELFCLASSSZ; eh->e_ident[5] = 0; eh->e_ident [6] = (_libelf.libelf_version) & 0xFFU; eh->e_machine = 0; eh->e_type = ELF_K_NONE; eh->e_version = (_libelf.libelf_version ); } while (0) do { \ |
| 92 | Elf##SZ##_Ehdr *eh = (E); \ |
| 93 | eh->e_ident[EI_MAG00] = ELFMAG00x7f; \ |
| 94 | eh->e_ident[EI_MAG11] = ELFMAG1'E'; \ |
| 95 | eh->e_ident[EI_MAG22] = ELFMAG2'L'; \ |
| 96 | eh->e_ident[EI_MAG33] = ELFMAG3'F'; \ |
| 97 | eh->e_ident[EI_CLASS4] = ELFCLASS2##SZ; \ |
| 98 | eh->e_ident[EI_DATA5] = ELFDATANONE0; \ |
| 99 | eh->e_ident[EI_VERSION6] = LIBELF_PRIVATE(version)(_libelf.libelf_version) & 0xFFU; \ |
| 100 | eh->e_machine = EM_NONE0; \ |
| 101 | eh->e_type = ELF_K_NONE; \ |
| 102 | eh->e_version = LIBELF_PRIVATE(version)(_libelf.libelf_version); \ |
| 103 | } while (0) |
| 104 | |
| 105 | void * |
| 106 | _libelf_ehdr(Elf *e, int ec, int allocate) |
| 107 | { |
| 108 | void *ehdr; |
| 109 | size_t fsz, msz; |
| 110 | uint16_t phnum, shnum, strndx; |
| 111 | uint64_t shoff; |
| 112 | int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s, |
| 113 | size_t _c, int _swap); |
| 114 | |
| 115 | assert(ec == ELFCLASS32 || ec == ELFCLASS64)((ec == 1 || ec == 2) ? (void)0 : __assert2("/usr/src/lib/libelf/libelf_ehdr.c" , 115, __func__, "ec == ELFCLASS32 || ec == ELFCLASS64")); |
| 116 | |
| 117 | if (e == NULL((void *)0) || e->e_kind != ELF_K_ELF) { |
| 118 | LIBELF_SET_ERROR(ARGUMENT, 0)do { (_libelf.libelf_error) = (((ELF_E_ARGUMENT) & 0xFF) | (((0)) << 8)); } while (0); |
| 119 | return (NULL((void *)0)); |
| 120 | } |
| 121 | |
| 122 | if (e->e_class != ELFCLASSNONE0 && e->e_class != ec) { |
| 123 | LIBELF_SET_ERROR(CLASS, 0)do { (_libelf.libelf_error) = (((ELF_E_CLASS) & 0xFF) | ( ((0)) << 8)); } while (0); |
| 124 | return (NULL((void *)0)); |
| 125 | } |
| 126 | |
| 127 | if (e->e_version != EV_CURRENT1) { |
| 128 | LIBELF_SET_ERROR(VERSION, 0)do { (_libelf.libelf_error) = (((ELF_E_VERSION) & 0xFF) | (((0)) << 8)); } while (0); |
| 129 | return (NULL((void *)0)); |
| 130 | } |
| 131 | |
| 132 | if (e->e_class == ELFCLASSNONE0) |
| 133 | e->e_class = ec; |
| 134 | |
| 135 | if (ec == ELFCLASS321) |
| 136 | ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32; |
| 137 | else |
| 138 | ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64; |
| 139 | |
| 140 | if (ehdr != NULL((void *)0)) /* already have a translated ehdr */ |
| 141 | return (ehdr); |
| 142 | |
| 143 | fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1); |
| 144 | assert(fsz > 0)((fsz > 0) ? (void)0 : __assert2("/usr/src/lib/libelf/libelf_ehdr.c" , 144, __func__, "fsz > 0")); |
| 145 | |
| 146 | if (e->e_cmd != ELF_C_WRITE && e->e_rawsize < (off_t) fsz) { |
| 147 | LIBELF_SET_ERROR(HEADER, 0)do { (_libelf.libelf_error) = (((ELF_E_HEADER) & 0xFF) | ( ((0)) << 8)); } while (0); |
| 148 | return (NULL((void *)0)); |
| 149 | } |
| 150 | |
| 151 | if ((msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT1)) == 0) |
| 152 | return (NULL((void *)0)); |
| 153 | |
| 154 | if ((ehdr = calloc((size_t) 1, msz)) == NULL((void *)0)) { |
| 155 | LIBELF_SET_ERROR(RESOURCE, 0)do { (_libelf.libelf_error) = (((ELF_E_RESOURCE) & 0xFF) | (((0)) << 8)); } while (0); |
| 156 | return (NULL((void *)0)); |
| 157 | } |
| 158 | |
| 159 | if (ec == ELFCLASS321) { |
| 160 | e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr; |
| 161 | EHDR_INIT(ehdr,32)do { Elf32_Ehdr *eh = (ehdr); eh->e_ident[0] = 0x7f; eh-> e_ident[1] = 'E'; eh->e_ident[2] = 'L'; eh->e_ident[3] = 'F'; eh->e_ident[4] = 1; eh->e_ident[5] = 0; eh->e_ident [6] = (_libelf.libelf_version) & 0xFFU; eh->e_machine = 0; eh->e_type = ELF_K_NONE; eh->e_version = (_libelf.libelf_version ); } while (0); |
| 162 | } else { |
| 163 | e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr; |
| 164 | EHDR_INIT(ehdr,64)do { Elf64_Ehdr *eh = (ehdr); eh->e_ident[0] = 0x7f; eh-> e_ident[1] = 'E'; eh->e_ident[2] = 'L'; eh->e_ident[3] = 'F'; eh->e_ident[4] = 2; eh->e_ident[5] = 0; eh->e_ident [6] = (_libelf.libelf_version) & 0xFFU; eh->e_machine = 0; eh->e_type = ELF_K_NONE; eh->e_version = (_libelf.libelf_version ); } while (0); |
| 165 | } |
| 166 | |
| 167 | if (allocate) |
| 168 | e->e_flags |= ELF_F_DIRTY0x002U; |
| 169 | |
| 170 | if (e->e_cmd == ELF_C_WRITE) |
| 171 | return (ehdr); |
| 172 | |
| 173 | xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec, |
| 174 | _libelf_elfmachine(e)); |
| 175 | (*xlator)((unsigned char*) ehdr, msz, e->e_rawfile, (size_t) 1, |
| 176 | e->e_byteorder != LIBELF_PRIVATE(byteorder)(_libelf.libelf_byteorder)); |
| 177 | |
| 178 | if (ec == ELFCLASS321) { |
| 179 | phnum = ((Elf32_Ehdr *) ehdr)->e_phnum; |
| 180 | shnum = ((Elf32_Ehdr *) ehdr)->e_shnum; |
| 181 | shoff = ((Elf32_Ehdr *) ehdr)->e_shoff; |
| 182 | strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx; |
| 183 | } else { |
| 184 | phnum = ((Elf64_Ehdr *) ehdr)->e_phnum; |
| 185 | shnum = ((Elf64_Ehdr *) ehdr)->e_shnum; |
| 186 | shoff = ((Elf64_Ehdr *) ehdr)->e_shoff; |
| 187 | strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx; |
| 188 | } |
| 189 | |
| 190 | if (shnum >= SHN_LORESERVE0xff00 || |
| 191 | (shoff == 0LL && (shnum != 0 || phnum == PN_XNUM0xffff || |
| 192 | strndx == SHN_XINDEX0xffff))) { |
| 193 | LIBELF_SET_ERROR(HEADER, 0)do { (_libelf.libelf_error) = (((ELF_E_HEADER) & 0xFF) | ( ((0)) << 8)); } while (0); |
| 194 | return (NULL((void *)0)); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * If extended numbering is being used, read the correct |
| 199 | * number of sections and program header entries. |
| 200 | */ |
| 201 | if ((shnum == 0 && shoff != 0) || phnum == PN_XNUM0xffff || strndx == SHN_XINDEX0xffff) { |
| 202 | if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0) |
| 203 | return (NULL((void *)0)); |
| 204 | } else { |
| 205 | /* not using extended numbering */ |
| 206 | e->e_u.e_elf.e_nphdr = phnum; |
| 207 | e->e_u.e_elf.e_nscn = shnum; |
| 208 | e->e_u.e_elf.e_strndx = strndx; |
| 209 | } |
| 210 | |
| 211 | return (ehdr); |
| 212 | } |