1*7304104dSAndroid Build Coastguard Worker /* Create new ELF program header table.
2*7304104dSAndroid Build Coastguard Worker Copyright (C) 1999-2010, 2014, 2015 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker Written by Ulrich Drepper <[email protected]>, 1998.
5*7304104dSAndroid Build Coastguard Worker
6*7304104dSAndroid Build Coastguard Worker This file is free software; you can redistribute it and/or modify
7*7304104dSAndroid Build Coastguard Worker it under the terms of either
8*7304104dSAndroid Build Coastguard Worker
9*7304104dSAndroid Build Coastguard Worker * the GNU Lesser General Public License as published by the Free
10*7304104dSAndroid Build Coastguard Worker Software Foundation; either version 3 of the License, or (at
11*7304104dSAndroid Build Coastguard Worker your option) any later version
12*7304104dSAndroid Build Coastguard Worker
13*7304104dSAndroid Build Coastguard Worker or
14*7304104dSAndroid Build Coastguard Worker
15*7304104dSAndroid Build Coastguard Worker * the GNU General Public License as published by the Free
16*7304104dSAndroid Build Coastguard Worker Software Foundation; either version 2 of the License, or (at
17*7304104dSAndroid Build Coastguard Worker your option) any later version
18*7304104dSAndroid Build Coastguard Worker
19*7304104dSAndroid Build Coastguard Worker or both in parallel, as here.
20*7304104dSAndroid Build Coastguard Worker
21*7304104dSAndroid Build Coastguard Worker elfutils is distributed in the hope that it will be useful, but
22*7304104dSAndroid Build Coastguard Worker WITHOUT ANY WARRANTY; without even the implied warranty of
23*7304104dSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24*7304104dSAndroid Build Coastguard Worker General Public License for more details.
25*7304104dSAndroid Build Coastguard Worker
26*7304104dSAndroid Build Coastguard Worker You should have received copies of the GNU General Public License and
27*7304104dSAndroid Build Coastguard Worker the GNU Lesser General Public License along with this program. If
28*7304104dSAndroid Build Coastguard Worker not, see <http://www.gnu.org/licenses/>. */
29*7304104dSAndroid Build Coastguard Worker
30*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
31*7304104dSAndroid Build Coastguard Worker # include <config.h>
32*7304104dSAndroid Build Coastguard Worker #endif
33*7304104dSAndroid Build Coastguard Worker
34*7304104dSAndroid Build Coastguard Worker #include <assert.h>
35*7304104dSAndroid Build Coastguard Worker #include <stdlib.h>
36*7304104dSAndroid Build Coastguard Worker #include <string.h>
37*7304104dSAndroid Build Coastguard Worker
38*7304104dSAndroid Build Coastguard Worker #include "libelfP.h"
39*7304104dSAndroid Build Coastguard Worker
40*7304104dSAndroid Build Coastguard Worker #ifndef LIBELFBITS
41*7304104dSAndroid Build Coastguard Worker # define LIBELFBITS 32
42*7304104dSAndroid Build Coastguard Worker #endif
43*7304104dSAndroid Build Coastguard Worker
44*7304104dSAndroid Build Coastguard Worker
ElfW2(LIBELFBITS,Phdr)45*7304104dSAndroid Build Coastguard Worker ElfW2(LIBELFBITS,Phdr) *
46*7304104dSAndroid Build Coastguard Worker elfw2(LIBELFBITS,newphdr) (Elf *elf, size_t count)
47*7304104dSAndroid Build Coastguard Worker {
48*7304104dSAndroid Build Coastguard Worker ElfW2(LIBELFBITS,Phdr) *result;
49*7304104dSAndroid Build Coastguard Worker
50*7304104dSAndroid Build Coastguard Worker if (elf == NULL)
51*7304104dSAndroid Build Coastguard Worker return NULL;
52*7304104dSAndroid Build Coastguard Worker
53*7304104dSAndroid Build Coastguard Worker if (unlikely (elf->kind != ELF_K_ELF))
54*7304104dSAndroid Build Coastguard Worker {
55*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_HANDLE);
56*7304104dSAndroid Build Coastguard Worker return NULL;
57*7304104dSAndroid Build Coastguard Worker }
58*7304104dSAndroid Build Coastguard Worker
59*7304104dSAndroid Build Coastguard Worker /* This check is correct, it is for sh_info, which is either
60*7304104dSAndroid Build Coastguard Worker Elf32_Word or Elf64_Word, both being 32 bits. But count is size_t
61*7304104dSAndroid Build Coastguard Worker so might not fit on 32bit ELF files. */
62*7304104dSAndroid Build Coastguard Worker if (unlikely ((ElfW2(LIBELFBITS,Word)) count != count))
63*7304104dSAndroid Build Coastguard Worker {
64*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_OPERAND);
65*7304104dSAndroid Build Coastguard Worker return NULL;
66*7304104dSAndroid Build Coastguard Worker }
67*7304104dSAndroid Build Coastguard Worker
68*7304104dSAndroid Build Coastguard Worker rwlock_wrlock (elf->lock);
69*7304104dSAndroid Build Coastguard Worker
70*7304104dSAndroid Build Coastguard Worker if (elf->class == 0)
71*7304104dSAndroid Build Coastguard Worker elf->class = ELFW(ELFCLASS,LIBELFBITS);
72*7304104dSAndroid Build Coastguard Worker else if (unlikely (elf->class != ELFW(ELFCLASS,LIBELFBITS)))
73*7304104dSAndroid Build Coastguard Worker {
74*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_CLASS);
75*7304104dSAndroid Build Coastguard Worker result = NULL;
76*7304104dSAndroid Build Coastguard Worker goto out;
77*7304104dSAndroid Build Coastguard Worker }
78*7304104dSAndroid Build Coastguard Worker
79*7304104dSAndroid Build Coastguard Worker if (unlikely (elf->state.ELFW(elf,LIBELFBITS).ehdr == NULL))
80*7304104dSAndroid Build Coastguard Worker {
81*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
82*7304104dSAndroid Build Coastguard Worker result = NULL;
83*7304104dSAndroid Build Coastguard Worker goto out;
84*7304104dSAndroid Build Coastguard Worker }
85*7304104dSAndroid Build Coastguard Worker
86*7304104dSAndroid Build Coastguard Worker /* A COUNT of zero means remove existing table. */
87*7304104dSAndroid Build Coastguard Worker if (count == 0)
88*7304104dSAndroid Build Coastguard Worker {
89*7304104dSAndroid Build Coastguard Worker /* Free the old program header. */
90*7304104dSAndroid Build Coastguard Worker if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
91*7304104dSAndroid Build Coastguard Worker {
92*7304104dSAndroid Build Coastguard Worker if (elf->state.ELFW(elf,LIBELFBITS).phdr_flags & ELF_F_MALLOCED)
93*7304104dSAndroid Build Coastguard Worker free (elf->state.ELFW(elf,LIBELFBITS).phdr);
94*7304104dSAndroid Build Coastguard Worker
95*7304104dSAndroid Build Coastguard Worker /* Set the pointer to NULL. */
96*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
97*7304104dSAndroid Build Coastguard Worker /* Set the `e_phnum' member to the new value. */
98*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = 0;
99*7304104dSAndroid Build Coastguard Worker /* Also clear any old PN_XNUM extended value. */
100*7304104dSAndroid Build Coastguard Worker if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt > 0)
101*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).scns.data[0]
102*7304104dSAndroid Build Coastguard Worker .shdr.ELFW(e,LIBELFBITS)->sh_info = 0;
103*7304104dSAndroid Build Coastguard Worker /* Also set the size. */
104*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
105*7304104dSAndroid Build Coastguard Worker sizeof (ElfW2(LIBELFBITS,Phdr));
106*7304104dSAndroid Build Coastguard Worker
107*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
108*7304104dSAndroid Build Coastguard Worker elf->flags |= ELF_F_DIRTY;
109*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_NOERROR);
110*7304104dSAndroid Build Coastguard Worker }
111*7304104dSAndroid Build Coastguard Worker
112*7304104dSAndroid Build Coastguard Worker result = NULL;
113*7304104dSAndroid Build Coastguard Worker }
114*7304104dSAndroid Build Coastguard Worker else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
115*7304104dSAndroid Build Coastguard Worker || count == PN_XNUM
116*7304104dSAndroid Build Coastguard Worker || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
117*7304104dSAndroid Build Coastguard Worker {
118*7304104dSAndroid Build Coastguard Worker if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
119*7304104dSAndroid Build Coastguard Worker {
120*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
121*7304104dSAndroid Build Coastguard Worker result = NULL;
122*7304104dSAndroid Build Coastguard Worker goto out;
123*7304104dSAndroid Build Coastguard Worker }
124*7304104dSAndroid Build Coastguard Worker
125*7304104dSAndroid Build Coastguard Worker Elf_Scn *scn0 = &elf->state.ELFW(elf,LIBELFBITS).scns.data[0];
126*7304104dSAndroid Build Coastguard Worker if (unlikely (count >= PN_XNUM && scn0->shdr.ELFW(e,LIBELFBITS) == NULL))
127*7304104dSAndroid Build Coastguard Worker {
128*7304104dSAndroid Build Coastguard Worker /* Something is wrong with section zero, but we need it to write
129*7304104dSAndroid Build Coastguard Worker the extended phdr count. */
130*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
131*7304104dSAndroid Build Coastguard Worker result = NULL;
132*7304104dSAndroid Build Coastguard Worker goto out;
133*7304104dSAndroid Build Coastguard Worker }
134*7304104dSAndroid Build Coastguard Worker
135*7304104dSAndroid Build Coastguard Worker /* Allocate a new program header with the appropriate number of
136*7304104dSAndroid Build Coastguard Worker elements. */
137*7304104dSAndroid Build Coastguard Worker result = (ElfW2(LIBELFBITS,Phdr) *)
138*7304104dSAndroid Build Coastguard Worker realloc (elf->state.ELFW(elf,LIBELFBITS).phdr,
139*7304104dSAndroid Build Coastguard Worker count * sizeof (ElfW2(LIBELFBITS,Phdr)));
140*7304104dSAndroid Build Coastguard Worker if (result == NULL)
141*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_NOMEM);
142*7304104dSAndroid Build Coastguard Worker else
143*7304104dSAndroid Build Coastguard Worker {
144*7304104dSAndroid Build Coastguard Worker /* Now set the result. */
145*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).phdr = result;
146*7304104dSAndroid Build Coastguard Worker if (count >= PN_XNUM)
147*7304104dSAndroid Build Coastguard Worker {
148*7304104dSAndroid Build Coastguard Worker /* We have to write COUNT into the zeroth section's sh_info. */
149*7304104dSAndroid Build Coastguard Worker if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt == 0)
150*7304104dSAndroid Build Coastguard Worker {
151*7304104dSAndroid Build Coastguard Worker assert (elf->state.ELFW(elf,LIBELFBITS).scns.max > 0);
152*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).scns.cnt = 1;
153*7304104dSAndroid Build Coastguard Worker }
154*7304104dSAndroid Build Coastguard Worker scn0->shdr.ELFW(e,LIBELFBITS)->sh_info = count;
155*7304104dSAndroid Build Coastguard Worker scn0->shdr_flags |= ELF_F_DIRTY;
156*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = PN_XNUM;
157*7304104dSAndroid Build Coastguard Worker }
158*7304104dSAndroid Build Coastguard Worker else
159*7304104dSAndroid Build Coastguard Worker /* Set the `e_phnum' member to the new value. */
160*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = count;
161*7304104dSAndroid Build Coastguard Worker /* Clear the whole memory. */
162*7304104dSAndroid Build Coastguard Worker memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
163*7304104dSAndroid Build Coastguard Worker /* Also set the size. */
164*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
165*7304104dSAndroid Build Coastguard Worker elf_typesize (LIBELFBITS, ELF_T_PHDR, 1);
166*7304104dSAndroid Build Coastguard Worker /* Remember we allocated the array and mark the structure is
167*7304104dSAndroid Build Coastguard Worker modified. */
168*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
169*7304104dSAndroid Build Coastguard Worker ELF_F_DIRTY | ELF_F_MALLOCED;
170*7304104dSAndroid Build Coastguard Worker /* We have to rewrite the entire file if the size of the
171*7304104dSAndroid Build Coastguard Worker program header is changed. */
172*7304104dSAndroid Build Coastguard Worker elf->flags |= ELF_F_DIRTY;
173*7304104dSAndroid Build Coastguard Worker }
174*7304104dSAndroid Build Coastguard Worker }
175*7304104dSAndroid Build Coastguard Worker else
176*7304104dSAndroid Build Coastguard Worker {
177*7304104dSAndroid Build Coastguard Worker /* We have the same number of entries. Just clear the array. */
178*7304104dSAndroid Build Coastguard Worker assert (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize
179*7304104dSAndroid Build Coastguard Worker == elf_typesize (LIBELFBITS, ELF_T_PHDR, 1));
180*7304104dSAndroid Build Coastguard Worker
181*7304104dSAndroid Build Coastguard Worker /* Mark the structure as modified. */
182*7304104dSAndroid Build Coastguard Worker elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
183*7304104dSAndroid Build Coastguard Worker
184*7304104dSAndroid Build Coastguard Worker result = elf->state.ELFW(elf,LIBELFBITS).phdr;
185*7304104dSAndroid Build Coastguard Worker memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
186*7304104dSAndroid Build Coastguard Worker }
187*7304104dSAndroid Build Coastguard Worker
188*7304104dSAndroid Build Coastguard Worker out:
189*7304104dSAndroid Build Coastguard Worker rwlock_unlock (elf->lock);
190*7304104dSAndroid Build Coastguard Worker
191*7304104dSAndroid Build Coastguard Worker return result;
192*7304104dSAndroid Build Coastguard Worker }
193*7304104dSAndroid Build Coastguard Worker INTDEF(elfw2(LIBELFBITS,newphdr))
194