]>
Commit | Line | Data |
---|---|---|
fc618fd0 | 1 | /* Check for text relocations in DSOs. |
a334319f | 2 | Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
fc618fd0 UD |
3 | This file is part of the GNU C Library. |
4 | Contribute by Ulrich Drepper <drepper@redhat.com>. 2002. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
17 | License along with the GNU C Library; if not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
19 | 02111-1307 USA. */ | |
20 | ||
efd56f53 UD |
21 | #include <byteswap.h> |
22 | #include <elf.h> | |
23 | #include <endian.h> | |
24 | #include <fcntl.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | ||
30 | ||
31 | #ifdef BITS | |
32 | ||
33 | # define AB(name) _AB (name, BITS) | |
34 | # define _AB(name, bits) __AB (name, bits) | |
35 | # define __AB(name, bits) name##bits | |
36 | # define E(name) _E (name, BITS) | |
37 | # define _E(name, bits) __E (name, bits) | |
38 | # define __E(name, bits) Elf##bits##_##name | |
39 | # define SWAP(val) \ | |
40 | ({ __typeof (val) __res; \ | |
e5ffa64a UD |
41 | if (((ehdr.e_ident[EI_DATA] == ELFDATA2MSB \ |
42 | && BYTE_ORDER == LITTLE_ENDIAN) \ | |
43 | || (ehdr.e_ident[EI_DATA] == ELFDATA2LSB \ | |
44 | && BYTE_ORDER == BIG_ENDIAN)) \ | |
efd56f53 UD |
45 | && sizeof (val) != 1) \ |
46 | { \ | |
47 | if (sizeof (val) == 2) \ | |
48 | __res = bswap_16 (val); \ | |
49 | else if (sizeof (val) == 4) \ | |
50 | __res = bswap_32 (val); \ | |
51 | else \ | |
52 | __res = bswap_64 (val); \ | |
53 | } \ | |
54 | else \ | |
55 | __res = (val); \ | |
56 | __res; }) | |
57 | ||
58 | ||
59 | static int | |
60 | AB(handle_file) (const char *fname, int fd) | |
61 | { | |
62 | E(Ehdr) ehdr; | |
63 | ||
64 | if (pread (fd, &ehdr, sizeof (ehdr), 0) != sizeof (ehdr)) | |
65 | { | |
66 | read_error: | |
67 | printf ("%s: read error: %m\n", fname); | |
68 | return 1; | |
69 | } | |
70 | ||
71 | const size_t phnum = SWAP (ehdr.e_phnum); | |
72 | const size_t phentsize = SWAP (ehdr.e_phentsize); | |
73 | ||
74 | /* Read the program header. */ | |
75 | E(Phdr) *phdr = alloca (phentsize * phnum); | |
76 | if (pread (fd, phdr, phentsize * phnum, SWAP (ehdr.e_phoff)) | |
77 | != phentsize * phnum) | |
78 | goto read_error; | |
79 | ||
80 | /* Search for the PT_DYNAMIC entry. */ | |
81 | size_t cnt; | |
82 | for (cnt = 0; cnt < phnum; ++cnt) | |
894dc70b | 83 | if (SWAP (phdr[cnt].p_type) == PT_DYNAMIC) |
a334319f UD |
84 | break; |
85 | ||
86 | if (cnt == phnum) | |
efd56f53 UD |
87 | { |
88 | printf ("%s: no DYNAMIC segment found\n", fname); | |
89 | return 1; | |
90 | } | |
91 | ||
92 | /* Read the dynamic segment. */ | |
a334319f | 93 | size_t pmemsz = SWAP(phdr[cnt].p_memsz); |
efd56f53 | 94 | E(Dyn) *dyn = alloca (pmemsz); |
a334319f | 95 | if (pread (fd, dyn, pmemsz, SWAP(phdr[cnt].p_offset)) != pmemsz) |
efd56f53 UD |
96 | goto read_error; |
97 | ||
98 | /* Search for an DT_TEXTREL entry of DT_FLAGS with the DF_TEXTREL | |
99 | bit set. */ | |
100 | for (cnt = 0; (cnt + 1) * sizeof (E(Dyn)) - 1 < pmemsz; ++cnt) | |
101 | { | |
fc618fd0 UD |
102 | unsigned int tag = SWAP (dyn[cnt].d_tag); |
103 | ||
104 | if (tag == DT_NULL) | |
efd56f53 UD |
105 | /* We reached the end. */ |
106 | break; | |
107 | ||
fc618fd0 UD |
108 | if (tag == DT_TEXTREL |
109 | || (tag == DT_FLAGS | |
110 | && (SWAP (dyn[cnt].d_un.d_val) & DF_TEXTREL) != 0)) | |
efd56f53 UD |
111 | { |
112 | /* Urgh! The DSO has text relocations. */ | |
113 | printf ("%s: text relocations used\n", fname); | |
114 | return 1; | |
115 | } | |
116 | } | |
117 | ||
118 | printf ("%s: OK\n", fname); | |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
123 | # undef BITS | |
124 | #else | |
125 | ||
126 | # define BITS 32 | |
127 | # include "check-textrel.c" | |
128 | ||
129 | # define BITS 64 | |
130 | # include "check-textrel.c" | |
131 | ||
132 | ||
133 | static int | |
134 | handle_file (const char *fname) | |
135 | { | |
136 | int fd = open (fname, O_RDONLY); | |
137 | if (fd == -1) | |
138 | { | |
139 | printf ("cannot open %s: %m\n", fname); | |
140 | return 1; | |
141 | } | |
142 | ||
143 | /* Read was is supposed to be the ELF header. Read the initial | |
144 | bytes to determine whether this is a 32 or 64 bit file. */ | |
145 | char ident[EI_NIDENT]; | |
146 | if (read (fd, ident, EI_NIDENT) != EI_NIDENT) | |
147 | { | |
148 | printf ("%s: read error: %m\n", fname); | |
149 | close (fd); | |
150 | return 1; | |
151 | } | |
152 | ||
153 | if (memcmp (&ident[EI_MAG0], ELFMAG, SELFMAG) != 0) | |
154 | { | |
155 | printf ("%s: not an ELF file\n", fname); | |
156 | close (fd); | |
157 | return 1; | |
158 | } | |
159 | ||
160 | int result; | |
161 | if (ident[EI_CLASS] == ELFCLASS64) | |
162 | result = handle_file64 (fname, fd); | |
163 | else | |
164 | result = handle_file32 (fname, fd); | |
165 | ||
166 | close (fd); | |
167 | ||
168 | return result; | |
169 | } | |
170 | ||
171 | ||
172 | int | |
173 | main (int argc, char *argv[]) | |
174 | { | |
175 | int cnt; | |
176 | int result = 0; | |
177 | ||
178 | for (cnt = 1; cnt < argc; ++cnt) | |
179 | result |= handle_file (argv[cnt]); | |
180 | ||
181 | return result; | |
182 | } | |
183 | #endif |