• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Return converted data from raw chunk of ELF file.
2    Copyright (C) 2007 Red Hat, Inc.
3    This file is part of Red Hat elfutils.
4 
5    Red Hat elfutils is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by the
7    Free Software Foundation; version 2 of the License.
8 
9    Red Hat elfutils is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License along
15    with Red Hat elfutils; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17 
18    In addition, as a special exception, Red Hat, Inc. gives You the
19    additional right to link the code of Red Hat elfutils with code licensed
20    under any Open Source Initiative certified open source license
21    (http://www.opensource.org/licenses/index.php) which requires the
22    distribution of source code with any binary distribution and to
23    distribute linked combinations of the two.  Non-GPL Code permitted under
24    this exception must only link to the code of Red Hat elfutils through
25    those well defined interfaces identified in the file named EXCEPTION
26    found in the source code files (the "Approved Interfaces").  The files
27    of Non-GPL Code may instantiate templates or use macros or inline
28    functions from the Approved Interfaces without causing the resulting
29    work to be covered by the GNU General Public License.  Only Red Hat,
30    Inc. may make changes or additions to the list of Approved Interfaces.
31    Red Hat's grant of this exception is conditioned upon your not adding
32    any new exceptions.  If you wish to add a new Approved Interface or
33    exception, please contact Red Hat.  You must obey the GNU General Public
34    License in all respects for all of the Red Hat elfutils code and other
35    code used in conjunction with Red Hat elfutils except the Non-GPL Code
36    covered by this exception.  If you modify this file, you may extend this
37    exception to your version of the file, but you are not obligated to do
38    so.  If you do not wish to provide this exception without modification,
39    you must delete this exception statement from your version and license
40    this file solely under the GPL without exception.
41 
42    Red Hat elfutils is an included package of the Open Invention Network.
43    An included package of the Open Invention Network is a package for which
44    Open Invention Network licensees cross-license their patents.  No patent
45    license is granted, either expressly or impliedly, by designation as an
46    included package.  Should you wish to participate in the Open Invention
47    Network licensing program, please visit www.openinventionnetwork.com
48    <http://www.openinventionnetwork.com>.  */
49 
50 #ifdef HAVE_CONFIG_H
51 # include <config.h>
52 #endif
53 
54 #include <assert.h>
55 #include <errno.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include <system.h>
61 #include "libelfP.h"
62 #include "common.h"
63 
64 Elf_Data *
elf_getdata_rawchunk(elf,offset,size,type)65 elf_getdata_rawchunk (elf, offset, size, type)
66      Elf *elf;
67      /* ANDROID_CHANGE_BEGIN */
68 #if 0
69      off64_t offset;
70 #else
71      off_t offset;
72 #endif
73      /* ANDROID_CHANGE_END */
74      size_t size;
75      Elf_Type type;
76 {
77   if (unlikely (elf == NULL))
78     return NULL;
79 
80   if (unlikely (elf->kind != ELF_K_ELF))
81     {
82       /* No valid descriptor.  */
83       __libelf_seterrno (ELF_E_INVALID_HANDLE);
84       return NULL;
85     }
86 
87    /* ANDROID_CHANGE_BEGIN */
88 #if 0
89   if (unlikely (size > elf->maximum_size
90 		|| (off64_t) (elf->maximum_size - size) < offset))
91 #else
92   if (unlikely (size > elf->maximum_size
93 		|| (off_t) (elf->maximum_size - size) < offset))
94 #endif
95    /* ANDROID_CHANGE_END */
96     {
97       /* Invalid request.  */
98       __libelf_seterrno (ELF_E_INVALID_OP);
99       return NULL;
100     }
101 
102   if (type >= ELF_T_NUM)
103     {
104       __libelf_seterrno (ELF_E_UNKNOWN_TYPE);
105       return NULL;
106     }
107 
108   /* Get the raw bytes from the file.  */
109   void *rawchunk;
110   int flags = 0;
111   Elf_Data *result = NULL;
112 
113   rwlock_rdlock (elf->lock);
114 
115   /* If the file is mmap'ed we can use it directly.  */
116   if (elf->map_address != NULL)
117     rawchunk = elf->map_address + elf->start_offset + offset;
118   else
119     {
120       /* We allocate the memory and read the data from the file.  */
121       rawchunk = malloc (size);
122       if (rawchunk == NULL)
123 	{
124 	nomem:
125 	  __libelf_seterrno (ELF_E_NOMEM);
126 	  goto out;
127 	}
128 
129       /* Read the file content.  */
130       if (unlikely ((size_t) pread_retry (elf->fildes, rawchunk, size,
131 					  elf->start_offset + offset)
132 		    != size))
133 	{
134 	  /* Something went wrong.  */
135 	  free (rawchunk);
136 	  __libelf_seterrno (ELF_E_READ_ERROR);
137 	  goto out;
138 	}
139 
140       flags = ELF_F_MALLOCED;
141     }
142 
143   /* Copy and/or convert the data as needed for aligned native-order access.  */
144   size_t align = __libelf_type_align (elf->class, type);
145   void *buffer;
146   if (elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA)
147     {
148       if (((uintptr_t) rawchunk & (align - 1)) == 0)
149 	/* No need to copy, we can use the raw data.  */
150 	buffer = rawchunk;
151       else
152 	{
153 	  /* A malloc'd block is always sufficiently aligned.  */
154 	  assert (flags == 0);
155 
156 	  buffer = malloc (size);
157 	  if (unlikely (buffer == NULL))
158 	    goto nomem;
159 	  flags = ELF_F_MALLOCED;
160 
161 	  /* The copy will be appropriately aligned for direct access.  */
162 	  memcpy (buffer, rawchunk, size);
163 	}
164     }
165   else
166     {
167       if (flags)
168 	buffer = rawchunk;
169       else
170 	{
171 	  buffer = malloc (size);
172 	  if (unlikely (buffer == NULL))
173 	    goto nomem;
174 	  flags = ELF_F_MALLOCED;
175 	}
176 
177       /* Call the conversion function.  */
178       (*__elf_xfctstom[LIBELF_EV_IDX][LIBELF_EV_IDX][elf->class - 1][type])
179 	(buffer, rawchunk, size, 0);
180     }
181 
182   /* Allocate the dummy container to point at this buffer.  */
183   Elf_Data_Chunk *chunk = calloc (1, sizeof *chunk);
184   if (chunk == NULL)
185     {
186       if (flags)
187 	free (buffer);
188       goto nomem;
189     }
190 
191   chunk->dummy_scn.elf = elf;
192   chunk->dummy_scn.flags = flags;
193   chunk->data.s = &chunk->dummy_scn;
194   chunk->data.d.d_buf = buffer;
195   chunk->data.d.d_size = size;
196   chunk->data.d.d_type = type;
197   chunk->data.d.d_align = align;
198   chunk->data.d.d_version = __libelf_version;
199 
200   rwlock_unlock (elf->lock);
201   rwlock_wrlock (elf->lock);
202 
203   chunk->next = elf->state.elf.rawchunks;
204   elf->state.elf.rawchunks = chunk;
205   result = &chunk->data.d;
206 
207  out:
208   rwlock_unlock (elf->lock);
209   return result;
210 }
211