1 /* Create new ELF program header table.
2 Copyright (C) 1999-2010, 2014 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
41 # define LIBELFBITS 32
45 ElfW2(LIBELFBITS,Phdr) *
46 elfw2(LIBELFBITS,newphdr) (elf, count)
50 ElfW2(LIBELFBITS,Phdr) *result;
55 if (unlikely (elf->kind != ELF_K_ELF))
57 __libelf_seterrno (ELF_E_INVALID_HANDLE);
61 if (unlikely ((ElfW2(LIBELFBITS,Word)) count != count))
63 __libelf_seterrno (ELF_E_INVALID_OPERAND);
67 rwlock_wrlock (elf->lock);
70 elf->class = ELFW(ELFCLASS,LIBELFBITS);
71 else if (unlikely (elf->class != ELFW(ELFCLASS,LIBELFBITS)))
73 __libelf_seterrno (ELF_E_INVALID_CLASS);
78 if (unlikely (elf->state.ELFW(elf,LIBELFBITS).ehdr == NULL))
80 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
85 /* A COUNT of zero means remove existing table. */
88 /* Free the old program header. */
89 if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
91 if (elf->state.ELFW(elf,LIBELFBITS).phdr_flags & ELF_F_MALLOCED)
92 free (elf->state.ELFW(elf,LIBELFBITS).phdr);
94 /* Set the pointer to NULL. */
95 elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
96 /* Set the `e_phnum' member to the new value. */
97 elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = 0;
98 /* Also clear any old PN_XNUM extended value. */
99 if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt > 0)
100 elf->state.ELFW(elf,LIBELFBITS).scns.data[0]
101 .shdr.ELFW(e,LIBELFBITS)->sh_info = 0;
102 /* Also set the size. */
103 elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
104 sizeof (ElfW2(LIBELFBITS,Phdr));
106 elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
107 elf->flags |= ELF_F_DIRTY;
108 __libelf_seterrno (ELF_E_NOERROR);
113 else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
115 || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
117 if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
123 /* Allocate a new program header with the appropriate number of
125 result = (ElfW2(LIBELFBITS,Phdr) *)
126 realloc (elf->state.ELFW(elf,LIBELFBITS).phdr,
127 count * sizeof (ElfW2(LIBELFBITS,Phdr)));
129 __libelf_seterrno (ELF_E_NOMEM);
132 /* Now set the result. */
133 elf->state.ELFW(elf,LIBELFBITS).phdr = result;
134 if (count >= PN_XNUM)
136 /* We have to write COUNT into the zeroth section's sh_info. */
137 Elf_Scn *scn0 = &elf->state.ELFW(elf,LIBELFBITS).scns.data[0];
138 if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt == 0)
140 assert (elf->state.ELFW(elf,LIBELFBITS).scns.max > 0);
141 elf->state.ELFW(elf,LIBELFBITS).scns.cnt = 1;
143 scn0->shdr.ELFW(e,LIBELFBITS)->sh_info = count;
144 scn0->shdr_flags |= ELF_F_DIRTY;
145 elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = PN_XNUM;
148 /* Set the `e_phnum' member to the new value. */
149 elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = count;
150 /* Clear the whole memory. */
151 memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
152 /* Also set the size. */
153 elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
154 elf_typesize (LIBELFBITS, ELF_T_PHDR, 1);
155 /* Remember we allocated the array and mark the structure is
157 elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
158 ELF_F_DIRTY | ELF_F_MALLOCED;
159 /* We have to rewrite the entire file if the size of the
160 program header is changed. */
161 elf->flags |= ELF_F_DIRTY;
166 /* We have the same number of entries. Just clear the array. */
167 assert (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize
168 == elf_typesize (LIBELFBITS, ELF_T_PHDR, 1));
170 /* Mark the structure as modified. */
171 elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
173 result = elf->state.ELFW(elf,LIBELFBITS).phdr;
174 memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
178 rwlock_unlock (elf->lock);
182 INTDEF(elfw2(LIBELFBITS,newphdr))