1b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper/* Create new ELF program header table.
21ccdfb683ad6c7e59793136c3a657ddf131cafd1Mark Wielaard   Copyright (C) 1999-2010, 2014, 2015 Red Hat, Inc.
3de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   This file is part of elfutils.
4b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
6de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   This file is free software; you can redistribute it and/or modify
7de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   it under the terms of either
8b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
9de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard     * the GNU Lesser General Public License as published by the Free
10de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard       Software Foundation; either version 3 of the License, or (at
11de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard       your option) any later version
12de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard
13de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   or
14de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard
15de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard     * the GNU General Public License as published by the Free
16de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard       Software Foundation; either version 2 of the License, or (at
17de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard       your option) any later version
18de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard
19de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   or both in parallel, as here.
20de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard
21de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   elfutils is distributed in the hope that it will be useful, but
22361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   WITHOUT ANY WARRANTY; without even the implied warranty of
23361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   General Public License for more details.
25b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
26de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   You should have received copies of the GNU General Public License and
27de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   the GNU Lesser General Public License along with this program.  If
28de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard   not, see <http://www.gnu.org/licenses/>.  */
29b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
30b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#ifdef HAVE_CONFIG_H
31b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper# include <config.h>
32b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#endif
33b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
34b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <assert.h>
35b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <stdlib.h>
36b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <string.h>
37b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
38b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include "libelfP.h"
39b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
40b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#ifndef LIBELFBITS
41b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper# define LIBELFBITS 32
42b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#endif
43b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
44b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
45b08d5a8fb42f4586d756068065186b5af7e48daUlrich DrepperElfW2(LIBELFBITS,Phdr) *
461ccdfb683ad6c7e59793136c3a657ddf131cafd1Mark Wielaardelfw2(LIBELFBITS,newphdr) (Elf *elf, size_t count)
47b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
48b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  ElfW2(LIBELFBITS,Phdr) *result;
49b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
50b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (elf == NULL)
51b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    return NULL;
52b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
53b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (unlikely (elf->kind != ELF_K_ELF))
54b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
55b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      __libelf_seterrno (ELF_E_INVALID_HANDLE);
56b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      return NULL;
57b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
58b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
596fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath  if (unlikely ((ElfW2(LIBELFBITS,Word)) count != count))
606fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath    {
616fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath      __libelf_seterrno (ELF_E_INVALID_OPERAND);
626fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath      return NULL;
636fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath    }
646fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath
65b4d6f0f8064f2b706ea9035ef0393d8299671390Roland McGrath  rwlock_wrlock (elf->lock);
66b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
67b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (elf->class == 0)
68b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    elf->class = ELFW(ELFCLASS,LIBELFBITS);
69b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  else if (unlikely (elf->class != ELFW(ELFCLASS,LIBELFBITS)))
70b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
71b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      __libelf_seterrno (ELF_E_INVALID_CLASS);
72b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      result = NULL;
73b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      goto out;
74b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
75b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
76b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (unlikely (elf->state.ELFW(elf,LIBELFBITS).ehdr == NULL))
77b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
78b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
79b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      result = NULL;
80b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      goto out;
81b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
82b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
83b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* A COUNT of zero means remove existing table.  */
84b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (count == 0)
85b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
86b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      /* Free the old program header.  */
87b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
88b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	{
89b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  if (elf->state.ELFW(elf,LIBELFBITS).phdr_flags & ELF_F_MALLOCED)
90b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	    free (elf->state.ELFW(elf,LIBELFBITS).phdr);
91b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
92b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* Set the pointer to NULL.  */
93b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
94b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* Set the `e_phnum' member to the new value.  */
95b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = 0;
966fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	  /* Also clear any old PN_XNUM extended value.  */
976fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	  if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt > 0)
986fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	    elf->state.ELFW(elf,LIBELFBITS).scns.data[0]
996fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	      .shdr.ELFW(e,LIBELFBITS)->sh_info = 0;
100b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* Also set the size.  */
101b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
102b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	    sizeof (ElfW2(LIBELFBITS,Phdr));
103b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
104b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
105b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  elf->flags |= ELF_F_DIRTY;
106b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  __libelf_seterrno (ELF_E_NOERROR);
107b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	}
108b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
109b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      result = NULL;
110b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
111b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
1126fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	   || count == PN_XNUM
113b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	   || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
114b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
115720383c53b435de6647edd78060dd7d38ade25a5Jakub Jelinek      if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
116720383c53b435de6647edd78060dd7d38ade25a5Jakub Jelinek	{
117458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard	  __libelf_seterrno (ELF_E_INVALID_INDEX);
118458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard	  result = NULL;
119458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard	  goto out;
120458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard	}
121458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard
122458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard      Elf_Scn *scn0 = &elf->state.ELFW(elf,LIBELFBITS).scns.data[0];
123458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard      if (unlikely (count >= PN_XNUM && scn0->shdr.ELFW(e,LIBELFBITS) == NULL))
124458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard	{
125458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard	  /* Something is wrong with section zero, but we need it to write
126458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard	     the extended phdr count.  */
127458027d294ac6e8a8678d9fd0f96759d7196ae2bMark Wielaard	  __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
128720383c53b435de6647edd78060dd7d38ade25a5Jakub Jelinek	  result = NULL;
129720383c53b435de6647edd78060dd7d38ade25a5Jakub Jelinek	  goto out;
130720383c53b435de6647edd78060dd7d38ade25a5Jakub Jelinek	}
131720383c53b435de6647edd78060dd7d38ade25a5Jakub Jelinek
132b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      /* Allocate a new program header with the appropriate number of
133b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	 elements.  */
134b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      result = (ElfW2(LIBELFBITS,Phdr) *)
135b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	realloc (elf->state.ELFW(elf,LIBELFBITS).phdr,
136b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper		 count * sizeof (ElfW2(LIBELFBITS,Phdr)));
137b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      if (result == NULL)
138b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	__libelf_seterrno (ELF_E_NOMEM);
139b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      else
140b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	{
141b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* Now set the result.  */
142b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  elf->state.ELFW(elf,LIBELFBITS).phdr = result;
1436fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	  if (count >= PN_XNUM)
1446fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	    {
1456fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	      /* We have to write COUNT into the zeroth section's sh_info.  */
1466fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	      if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt == 0)
1476fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath		{
1486fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath		  assert (elf->state.ELFW(elf,LIBELFBITS).scns.max > 0);
1496fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath		  elf->state.ELFW(elf,LIBELFBITS).scns.cnt = 1;
1506fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath		}
1516fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	      scn0->shdr.ELFW(e,LIBELFBITS)->sh_info = count;
1526fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	      scn0->shdr_flags |= ELF_F_DIRTY;
1536fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	      elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = PN_XNUM;
1546fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	    }
1556fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	  else
1566fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	    /* Set the `e_phnum' member to the new value.  */
1576fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath	    elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = count;
158b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* Clear the whole memory.  */
159b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
160b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* Also set the size.  */
161b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
162b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	    elf_typesize (LIBELFBITS, ELF_T_PHDR, 1);
163b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* Remember we allocated the array and mark the structure is
164b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	     modified.  */
165b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
166b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	    ELF_F_DIRTY | ELF_F_MALLOCED;
167b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* We have to rewrite the entire file if the size of the
168b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	     program header is changed.  */
169b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  elf->flags |= ELF_F_DIRTY;
170b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	}
171b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
172b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  else
173b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
174b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      /* We have the same number of entries.  Just clear the array.  */
175b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      assert (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize
176b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	      == elf_typesize (LIBELFBITS, ELF_T_PHDR, 1));
177b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
178b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      /* Mark the structure as modified.  */
179b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
180b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
181b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
1826fd3cd104adf4107aa64e1c1e84028b4ea0b3296Roland McGrath      memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
183b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
184b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
185b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper out:
186b4d6f0f8064f2b706ea9035ef0393d8299671390Roland McGrath  rwlock_unlock (elf->lock);
187b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
188b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  return result;
189b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper}
190b08d5a8fb42f4586d756068065186b5af7e48daUlrich DrepperINTDEF(elfw2(LIBELFBITS,newphdr))
191