blob: 1e26e8540d4b5df3eb583eaeae1c2867b92745bc [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Return section header.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 2.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
22#include <assert.h>
23#include <unistd.h>
24
25#include "libelfP.h"
26#include "common.h"
27
28#ifndef LIBELFBITS
29# define LIBELFBITS 32
30#endif
31
32
33ElfW2(LIBELFBITS,Shdr) *
34elfw2(LIBELFBITS,getshdr) (scn)
35 Elf_Scn *scn;
36{
37 ElfW2(LIBELFBITS,Shdr) *result;
38
39 if (scn == NULL)
40 return NULL;
41
42 if (unlikely (scn->elf->state.elf.ehdr == NULL))
43 {
44 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
45 return NULL;
46 }
47
48 if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
49 {
50 __libelf_seterrno (ELF_E_INVALID_CLASS);
51 return NULL;
52 }
53
54 result = scn->shdr.ELFW(e,LIBELFBITS);
55 if (result == NULL)
56 {
57 /* Read the section header table. */
58 Elf *elf = scn->elf;
59 ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
60
61 rwlock_wrlock (elf->lock);
62
63 /* Try again, maybe the data is there now. */
64 result = scn->shdr.ELFW(e,LIBELFBITS);
65 if (result != NULL)
66 goto out;
67
68 size_t shnum;
69 if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
70 goto out;
71 size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
72
73 /* Allocate memory for the program headers. We know the number
74 of entries from the ELF header. */
75 ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
76 (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
77 if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
78 {
79 __libelf_seterrno (ELF_E_NOMEM);
80 goto out;
81 }
82 elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
83
84 if (elf->map_address != NULL)
85 {
86 ElfW2(LIBELFBITS,Shdr) *notcvt;
87
88 /* All the data is already mapped. If we could use it
89 directly this would already have happened. */
90 assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
91 || (! ALLOW_UNALIGNED
92 && (ehdr->e_shoff
93 & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
94
95 /* Now copy the data and at the same time convert the byte
96 order. */
97 if (ALLOW_UNALIGNED
98 || (ehdr->e_shoff
99 & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) == 0)
100 notcvt = (ElfW2(LIBELFBITS,Shdr) *)
101 ((char *) elf->map_address
102 + elf->start_offset + ehdr->e_shoff);
103 else
104 {
105 notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
106 memcpy (notcvt, ((char *) elf->map_address
107 + elf->start_offset + ehdr->e_shoff),
108 size);
109 }
110
111 for (size_t cnt = 0; cnt < shnum; ++cnt)
112 {
113 CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
114 CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
115 CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
116 CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
117 CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
118 CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
119 CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
120 CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
121 CONVERT_TO (shdr[cnt].sh_addralign, notcvt[cnt].sh_addralign);
122 CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
123 }
124 }
125 else if (elf->fildes != -1)
126 {
127 /* Read the header. */
128 if ((size_t) pread (elf->fildes,
129 elf->state.ELFW(elf,LIBELFBITS).shdr, size,
130 elf->start_offset + ehdr->e_shoff) != size)
131 {
132 /* Severe problems. We cannot read the data. */
133 __libelf_seterrno (ELF_E_READ_ERROR);
134 goto free_and_out;
135 }
136
137 /* If the byte order of the file is not the same as the one
138 of the host convert the data now. */
139 if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
140 for (size_t cnt = 0; cnt < shnum; ++cnt)
141 {
142 CONVERT (shdr[cnt].sh_name);
143 CONVERT (shdr[cnt].sh_type);
144 CONVERT (shdr[cnt].sh_flags);
145 CONVERT (shdr[cnt].sh_addr);
146 CONVERT (shdr[cnt].sh_offset);
147 CONVERT (shdr[cnt].sh_size);
148 CONVERT (shdr[cnt].sh_link);
149 CONVERT (shdr[cnt].sh_info);
150 CONVERT (shdr[cnt].sh_addralign);
151 CONVERT (shdr[cnt].sh_entsize);
152 }
153 }
154 else
155 {
156 /* The file descriptor was already enabled and not all data was
157 read. Undo the allocation. */
158 __libelf_seterrno (ELF_E_FD_DISABLED);
159
160 free_and_out:
161 free (shdr);
162 elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
163 elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
164
165 goto out;
166 }
167
168 /* Set the pointers in the `scn's. */
169 for (size_t cnt = 0; cnt < shnum; ++cnt)
170 elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
171 = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
172
173 result = scn->shdr.ELFW(e,LIBELFBITS);
174 assert (result != NULL);
175
176 out:
177 rwlock_unlock (elf->lock);
178 }
179
180 return result;
181}
182INTDEF(elfw2(LIBELFBITS,getshdr))