1 /* $NetBSD: libelf_phdr.c,v 1.5 2024/03/03 17:37:34 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006,2008 Joseph Koshy
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
31 #endif
32
33 #include <sys/cdefs.h>
34
35 #include <assert.h>
36 #include <gelf.h>
37 #include <libelf.h>
38 #include <stdlib.h>
39
40 #include "_libelf.h"
41
42 __RCSID("$NetBSD: libelf_phdr.c,v 1.5 2024/03/03 17:37:34 christos Exp $");
43 ELFTC_VCSID("Id: libelf_phdr.c 3977 2022-05-01 06:45:34Z jkoshy");
44
45 void *
_libelf_getphdr(Elf * e,int ec)46 _libelf_getphdr(Elf *e, int ec)
47 {
48 size_t phnum;
49 size_t fsz, msz;
50 uint64_t phoff;
51 Elf32_Ehdr *eh32;
52 Elf64_Ehdr *eh64;
53 void *ehdr, *phdr;
54 _libelf_translator_function *xlator;
55
56 assert(ec == ELFCLASS32 || ec == ELFCLASS64);
57
58 if (e == NULL) {
59 LIBELF_SET_ERROR(ARGUMENT, 0);
60 return (NULL);
61 }
62
63 if ((phdr = (ec == ELFCLASS32 ?
64 (void *) e->e_u.e_elf.e_phdr.e_phdr32 :
65 (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL)
66 return (phdr);
67
68 /*
69 * Check the PHDR related fields in the EHDR for sanity.
70 */
71
72 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
73 return (NULL);
74
75 phnum = e->e_u.e_elf.e_nphdr;
76
77 if (ec == ELFCLASS32) {
78 eh32 = (Elf32_Ehdr *) ehdr;
79 phoff = (uint64_t) eh32->e_phoff;
80 } else {
81 eh64 = (Elf64_Ehdr *) ehdr;
82 phoff = (uint64_t) eh64->e_phoff;
83 }
84
85 fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
86
87 assert(fsz > 0);
88
89 if (phoff + fsz < phoff) { /* Numeric overflow. */
90 LIBELF_SET_ERROR(HEADER, 0);
91 return (NULL);
92 }
93
94 if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
95 LIBELF_SET_ERROR(HEADER, 0);
96 return (NULL);
97 }
98
99 if ((msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT)) == 0)
100 return (NULL);
101
102 if ((phdr = calloc(phnum, msz)) == NULL) {
103 LIBELF_SET_ERROR(RESOURCE, 0);
104 return (NULL);
105 }
106
107 if (ec == ELFCLASS32)
108 e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
109 else
110 e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
111
112
113 xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec,
114 _libelf_elfmachine(e));
115 (*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum,
116 e->e_byteorder != LIBELF_PRIVATE(byteorder));
117
118 return (phdr);
119 }
120
121 void *
_libelf_newphdr(Elf * e,int ec,size_t count)122 _libelf_newphdr(Elf *e, int ec, size_t count)
123 {
124 void *ehdr, *newphdr, *oldphdr;
125 size_t msz;
126
127 if (e == NULL) {
128 LIBELF_SET_ERROR(ARGUMENT, 0);
129 return (NULL);
130 }
131
132 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
133 LIBELF_SET_ERROR(SEQUENCE, 0);
134 return (NULL);
135 }
136
137 assert(e->e_class == ec);
138 assert(ec == ELFCLASS32 || ec == ELFCLASS64);
139 assert(e->e_version == EV_CURRENT);
140
141 if ((msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version)) == 0)
142 return (NULL);
143
144 newphdr = NULL;
145 if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
146 LIBELF_SET_ERROR(RESOURCE, 0);
147 return (NULL);
148 }
149
150 if (ec == ELFCLASS32) {
151 if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
152 free(oldphdr);
153 e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
154 } else {
155 if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
156 free(oldphdr);
157 e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
158 }
159
160 e->e_u.e_elf.e_nphdr = count;
161
162 elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
163
164 return (newphdr);
165 }
166