~ubuntu-branches/debian/lenny/elfutils/lenny

« back to all changes in this revision

Viewing changes to libelf/elf32_getshdr.c

  • Committer: Bazaar Package Importer
  • Author(s): Kurt Roeckx
  • Date: 2006-08-27 15:48:23 UTC
  • Revision ID: james.westby@ubuntu.com-20060827154823-mjwd7ydlbxgwqn4u
Tags: upstream-0.123
ImportĀ upstreamĀ versionĀ 0.123

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Return section header.
 
2
   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 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 <assert.h>
 
56
#include <errno.h>
 
57
#include <unistd.h>
 
58
 
 
59
#include <system.h>
 
60
#include "libelfP.h"
 
61
#include "common.h"
 
62
 
 
63
#ifndef LIBELFBITS
 
64
# define LIBELFBITS 32
 
65
#endif
 
66
 
 
67
 
 
68
ElfW2(LIBELFBITS,Shdr) *
 
69
elfw2(LIBELFBITS,getshdr) (scn)
 
70
     Elf_Scn *scn;
 
71
{
 
72
  ElfW2(LIBELFBITS,Shdr) *result;
 
73
 
 
74
  if (scn == NULL)
 
75
    return NULL;
 
76
 
 
77
  if (unlikely (scn->elf->state.elf.ehdr == NULL))
 
78
    {
 
79
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
 
80
      return NULL;
 
81
    }
 
82
 
 
83
  if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
 
84
    {
 
85
      __libelf_seterrno (ELF_E_INVALID_CLASS);
 
86
      return NULL;
 
87
    }
 
88
 
 
89
  result = scn->shdr.ELFW(e,LIBELFBITS);
 
90
  if (result == NULL)
 
91
    {
 
92
      /* Read the section header table.  */
 
93
      Elf *elf = scn->elf;
 
94
      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
 
95
 
 
96
      rwlock_wrlock (elf->lock);
 
97
 
 
98
      /* Try again, maybe the data is there now.  */
 
99
      result = scn->shdr.ELFW(e,LIBELFBITS);
 
100
      if (result != NULL)
 
101
        goto out;
 
102
 
 
103
      size_t shnum;
 
104
      if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
 
105
        goto out;
 
106
      size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
 
107
 
 
108
      /* Allocate memory for the program headers.  We know the number
 
109
         of entries from the ELF header.  */
 
110
      ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
 
111
        (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
 
112
      if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
 
113
        {
 
114
          __libelf_seterrno (ELF_E_NOMEM);
 
115
          goto out;
 
116
        }
 
117
      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
 
118
 
 
119
      if (elf->map_address != NULL)
 
120
        {
 
121
          ElfW2(LIBELFBITS,Shdr) *notcvt;
 
122
 
 
123
          /* All the data is already mapped.  If we could use it
 
124
             directly this would already have happened.  */
 
125
          assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
 
126
                  || (! ALLOW_UNALIGNED
 
127
                      && (((uintptr_t) elf->map_address + elf->start_offset
 
128
                           + ehdr->e_shoff)
 
129
                          & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
 
130
 
 
131
          /* Now copy the data and at the same time convert the byte
 
132
             order.  */
 
133
          if (ALLOW_UNALIGNED
 
134
              || (((uintptr_t) elf->map_address + elf->start_offset
 
135
                   + ehdr->e_shoff)
 
136
                  & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) == 0)
 
137
            notcvt = (ElfW2(LIBELFBITS,Shdr) *)
 
138
              ((char *) elf->map_address
 
139
               + elf->start_offset + ehdr->e_shoff);
 
140
          else
 
141
            {
 
142
              notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
 
143
              memcpy (notcvt, ((char *) elf->map_address
 
144
                               + elf->start_offset + ehdr->e_shoff),
 
145
                      size);
 
146
            }
 
147
 
 
148
          for (size_t cnt = 0; cnt < shnum; ++cnt)
 
149
            {
 
150
              CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
 
151
              CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
 
152
              CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
 
153
              CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
 
154
              CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
 
155
              CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
 
156
              CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
 
157
              CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
 
158
              CONVERT_TO (shdr[cnt].sh_addralign, notcvt[cnt].sh_addralign);
 
159
              CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
 
160
            }
 
161
        }
 
162
      else if (likely (elf->fildes != -1))
 
163
        {
 
164
          /* Read the header.  */
 
165
          ssize_t n = pread_retry (elf->fildes,
 
166
                                   elf->state.ELFW(elf,LIBELFBITS).shdr, size,
 
167
                                   elf->start_offset + ehdr->e_shoff);
 
168
          if (unlikely ((size_t) n != size))
 
169
            {
 
170
              /* Severe problems.  We cannot read the data.  */
 
171
              __libelf_seterrno (ELF_E_READ_ERROR);
 
172
              goto free_and_out;
 
173
            }
 
174
 
 
175
          /* If the byte order of the file is not the same as the one
 
176
             of the host convert the data now.  */
 
177
          if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
 
178
            for (size_t cnt = 0; cnt < shnum; ++cnt)
 
179
              {
 
180
                CONVERT (shdr[cnt].sh_name);
 
181
                CONVERT (shdr[cnt].sh_type);
 
182
                CONVERT (shdr[cnt].sh_flags);
 
183
                CONVERT (shdr[cnt].sh_addr);
 
184
                CONVERT (shdr[cnt].sh_offset);
 
185
                CONVERT (shdr[cnt].sh_size);
 
186
                CONVERT (shdr[cnt].sh_link);
 
187
                CONVERT (shdr[cnt].sh_info);
 
188
                CONVERT (shdr[cnt].sh_addralign);
 
189
                CONVERT (shdr[cnt].sh_entsize);
 
190
              }
 
191
        }
 
192
      else
 
193
        {
 
194
          /* The file descriptor was already enabled and not all data was
 
195
             read.  Undo the allocation.  */
 
196
          __libelf_seterrno (ELF_E_FD_DISABLED);
 
197
 
 
198
        free_and_out:
 
199
          free (shdr);
 
200
          elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
 
201
          elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
 
202
 
 
203
          goto out;
 
204
        }
 
205
 
 
206
      /* Set the pointers in the `scn's.  */
 
207
      for (size_t cnt = 0; cnt < shnum; ++cnt)
 
208
        elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
 
209
          = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
 
210
 
 
211
      result = scn->shdr.ELFW(e,LIBELFBITS);
 
212
      assert (result != NULL);
 
213
 
 
214
    out:
 
215
      rwlock_unlock (elf->lock);
 
216
    }
 
217
 
 
218
  return result;
 
219
}
 
220
INTDEF(elfw2(LIBELFBITS,getshdr))