1/* Get ELF program header table.
2   Copyright (C) 1998-2010 Red Hat, Inc.
3   This file is part of Red Hat elfutils.
4   Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
6   Red Hat elfutils is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 2 of the License.
9
10   Red Hat elfutils is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   General Public License for more details.
14
15   You should have received a copy of the GNU General Public License along
16   with Red Hat elfutils; if not, write to the Free Software Foundation,
17   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
19   In addition, as a special exception, Red Hat, Inc. gives You the
20   additional right to link the code of Red Hat elfutils with code licensed
21   under any Open Source Initiative certified open source license
22   (http://www.opensource.org/licenses/index.php) which requires the
23   distribution of source code with any binary distribution and to
24   distribute linked combinations of the two.  Non-GPL Code permitted under
25   this exception must only link to the code of Red Hat elfutils through
26   those well defined interfaces identified in the file named EXCEPTION
27   found in the source code files (the "Approved Interfaces").  The files
28   of Non-GPL Code may instantiate templates or use macros or inline
29   functions from the Approved Interfaces without causing the resulting
30   work to be covered by the GNU General Public License.  Only Red Hat,
31   Inc. may make changes or additions to the list of Approved Interfaces.
32   Red Hat's grant of this exception is conditioned upon your not adding
33   any new exceptions.  If you wish to add a new Approved Interface or
34   exception, please contact Red Hat.  You must obey the GNU General Public
35   License in all respects for all of the Red Hat elfutils code and other
36   code used in conjunction with Red Hat elfutils except the Non-GPL Code
37   covered by this exception.  If you modify this file, you may extend this
38   exception to your version of the file, but you are not obligated to do
39   so.  If you do not wish to provide this exception without modification,
40   you must delete this exception statement from your version and license
41   this file solely under the GPL without exception.
42
43   Red Hat elfutils is an included package of the Open Invention Network.
44   An included package of the Open Invention Network is a package for which
45   Open Invention Network licensees cross-license their patents.  No patent
46   license is granted, either expressly or impliedly, by designation as an
47   included package.  Should you wish to participate in the Open Invention
48   Network licensing program, please visit www.openinventionnetwork.com
49   <http://www.openinventionnetwork.com>.  */
50
51#ifdef HAVE_CONFIG_H
52# include <config.h>
53#endif
54
55#include <errno.h>
56#include <stdbool.h>
57#include <stdlib.h>
58#include <unistd.h>
59#include <assert.h>
60
61#include <system.h>
62#include "libelfP.h"
63#include "common.h"
64
65#ifndef LIBELFBITS
66# define LIBELFBITS 32
67#endif
68
69ElfW2(LIBELFBITS,Phdr) *
70__elfw2(LIBELFBITS,getphdr_wrlock) (elf)
71     Elf *elf;
72{
73  ElfW2(LIBELFBITS,Phdr) *result;
74
75  /* If the program header entry has already been filled in the code
76     below must already have been run.  So the class is set, too.  No
77     need to waste any more time here.  */
78  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
79  if (likely (result != NULL))
80    return result;
81
82  if (elf->class == 0)
83    elf->class = ELFW(ELFCLASS,LIBELFBITS);
84  else if (elf->class != ELFW(ELFCLASS,LIBELFBITS))
85    {
86      __libelf_seterrno (ELF_E_INVALID_CLASS);
87      result = NULL;
88      goto out;
89    }
90
91  if (likely (result == NULL))
92    {
93      /* Read the section header table.  */
94      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
95
96      /* If no program header exists return NULL.  */
97      size_t phnum;
98      if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
99	goto out;
100      if (phnum == 0)
101	{
102	  __libelf_seterrno (ELF_E_NO_PHDR);
103	  goto out;
104	}
105
106      size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
107
108      if (ehdr->e_phoff > elf->maximum_size
109	  || elf->maximum_size - ehdr->e_phoff < size)
110	{
111	  __libelf_seterrno (ELF_E_INVALID_DATA);
112	  goto out;
113	}
114
115      if (elf->map_address != NULL)
116	{
117	  /* All the data is already mapped.  Use it.  */
118	  void *file_phdr = ((char *) elf->map_address
119			     + elf->start_offset + ehdr->e_phoff);
120	  if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
121	      && (ALLOW_UNALIGNED
122		  || ((uintptr_t) file_phdr
123		      & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
124	    /* Simply use the mapped data.  */
125	    elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
126	  else
127	    {
128	      ElfW2(LIBELFBITS,Phdr) *notcvt;
129	      ElfW2(LIBELFBITS,Phdr) *phdr;
130
131	      /* Allocate memory for the program headers.  We know the number
132		 of entries from the ELF header.  */
133	      phdr = elf->state.ELFW(elf,LIBELFBITS).phdr =
134		(ElfW2(LIBELFBITS,Phdr) *) malloc (size);
135	      if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
136		{
137		  __libelf_seterrno (ELF_E_NOMEM);
138		  goto out;
139		}
140	      elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
141		ELF_F_MALLOCED | ELF_F_DIRTY;
142
143	      /* Now copy the data and at the same time convert the
144		 byte order.  */
145
146	      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
147		{
148		  assert (! ALLOW_UNALIGNED);
149		  memcpy (phdr, file_phdr, size);
150		}
151	      else
152		{
153		  if (ALLOW_UNALIGNED
154		      || ((uintptr_t) file_phdr
155			  & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0)
156		    notcvt = file_phdr;
157		  else
158		    {
159		      notcvt = (ElfW2(LIBELFBITS,Phdr) *) alloca (size);
160		      memcpy (notcvt, file_phdr, size);
161		    }
162
163		  for (size_t cnt = 0; cnt < phnum; ++cnt)
164		    {
165		      CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type);
166		      CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset);
167		      CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr);
168		      CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr);
169		      CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz);
170		      CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz);
171		      CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags);
172		      CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align);
173		    }
174		}
175	    }
176	}
177      else if (likely (elf->fildes != -1))
178	{
179	  /* Allocate memory for the program headers.  We know the number
180	     of entries from the ELF header.  */
181	  elf->state.ELFW(elf,LIBELFBITS).phdr =
182	    (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
183	  if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
184	    {
185	      __libelf_seterrno (ELF_E_NOMEM);
186	      goto out;
187	    }
188	  elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_MALLOCED;
189
190	  /* Read the header.  */
191	  ssize_t n = pread_retry (elf->fildes,
192				   elf->state.ELFW(elf,LIBELFBITS).phdr, size,
193				   elf->start_offset + ehdr->e_phoff);
194	  if (unlikely ((size_t) n != size))
195	    {
196	      /* Severe problems.  We cannot read the data.  */
197	      __libelf_seterrno (ELF_E_READ_ERROR);
198	      free (elf->state.ELFW(elf,LIBELFBITS).phdr);
199	      elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
200	      goto out;
201	    }
202
203	  /* If the byte order of the file is not the same as the one
204	     of the host convert the data now.  */
205	  if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
206	    {
207	      ElfW2(LIBELFBITS,Phdr) *phdr
208		= elf->state.ELFW(elf,LIBELFBITS).phdr;
209
210	      for (size_t cnt = 0; cnt < phnum; ++cnt)
211		{
212		  CONVERT (phdr[cnt].p_type);
213		  CONVERT (phdr[cnt].p_offset);
214		  CONVERT (phdr[cnt].p_vaddr);
215		  CONVERT (phdr[cnt].p_paddr);
216		  CONVERT (phdr[cnt].p_filesz);
217		  CONVERT (phdr[cnt].p_memsz);
218		  CONVERT (phdr[cnt].p_flags);
219		  CONVERT (phdr[cnt].p_align);
220		}
221	    }
222	}
223      else
224	{
225	  /* The file descriptor was already enabled and not all data was
226	     read.  */
227	  __libelf_seterrno (ELF_E_FD_DISABLED);
228	  goto out;
229	}
230
231      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
232    }
233
234 out:
235  return result;
236}
237
238ElfW2(LIBELFBITS,Phdr) *
239elfw2(LIBELFBITS,getphdr) (elf)
240     Elf *elf;
241{
242  ElfW2(LIBELFBITS,Phdr) *result;
243
244  if (elf == NULL)
245    return NULL;
246
247  if (unlikely (elf->kind != ELF_K_ELF))
248    {
249      __libelf_seterrno (ELF_E_INVALID_HANDLE);
250      return NULL;
251    }
252
253  /* If the program header entry has already been filled in the code
254   * in getphdr_wrlock must already have been run.  So the class is
255   * set, too.  No need to waste any more time here.  */
256  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
257  if (likely (result != NULL))
258    return result;
259
260  rwlock_wrlock (elf->lock);
261  result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
262  rwlock_unlock (elf->lock);
263
264  return result;
265}
266INTDEF(elfw2(LIBELFBITS,getphdr))
267