Replace FSF snail mail address with URLs.
[glibc.git] / elf / check-localplt.c
blobedab1d2d0afe6f320d93f7479342addab707a667
1 /* Show local PLT use in DSOs.
2 Copyright (C) 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contribute by Ulrich Drepper <drepper@redhat.com>. 2006.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <byteswap.h>
21 #include <elf.h>
22 #include <endian.h>
23 #include <fcntl.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
31 #ifdef BITS
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 EE(name) _EE (name, BITS)
40 # define _EE(name, bits) __EE (name, bits)
41 # define __EE(name, bits) ELF##bits##_##name
42 # define SWAP(val) \
43 ({ __typeof (val) __res; \
44 if (((ehdr.e_ident[EI_DATA] == ELFDATA2MSB \
45 && BYTE_ORDER == LITTLE_ENDIAN) \
46 || (ehdr.e_ident[EI_DATA] == ELFDATA2LSB \
47 && BYTE_ORDER == BIG_ENDIAN)) \
48 && sizeof (val) != 1) \
49 { \
50 if (sizeof (val) == 2) \
51 __res = bswap_16 (val); \
52 else if (sizeof (val) == 4) \
53 __res = bswap_32 (val); \
54 else \
55 __res = bswap_64 (val); \
56 } \
57 else \
58 __res = (val); \
59 __res; })
62 static int
63 AB(handle_file) (const char *fname, int fd)
65 E(Ehdr) ehdr;
67 if (pread (fd, &ehdr, sizeof (ehdr), 0) != sizeof (ehdr))
69 read_error:
70 printf ("%s: read error: %m\n", fname);
71 return 1;
74 const size_t phnum = SWAP (ehdr.e_phnum);
75 const size_t phentsize = SWAP (ehdr.e_phentsize);
77 /* Read the program header. */
78 E(Phdr) *phdr = alloca (phentsize * phnum);
79 if (pread (fd, phdr, phentsize * phnum, SWAP (ehdr.e_phoff))
80 != phentsize * phnum)
81 goto read_error;
83 /* Search for the PT_DYNAMIC entry. */
84 size_t cnt;
85 E(Phdr) *dynphdr = NULL;
86 for (cnt = 0; cnt < phnum; ++cnt)
87 if (SWAP (phdr[cnt].p_type) == PT_DYNAMIC)
89 dynphdr = &phdr[cnt];
90 break;
93 if (dynphdr == NULL)
95 printf ("%s: no DYNAMIC segment found\n", fname);
96 return 1;
99 /* Read the dynamic segment. */
100 size_t pmemsz = SWAP(dynphdr->p_memsz);
101 E(Dyn) *dyn = alloca (pmemsz);
102 if (pread64 (fd, dyn, pmemsz, SWAP(dynphdr->p_offset)) != pmemsz)
103 goto read_error;
105 /* Search for an DT_PLTREL, DT_JMPREL, DT_PLTRELSZ, DT_STRTAB,
106 DT_STRSZ, and DT_SYMTAB entries. */
107 size_t pltrel_idx = SIZE_MAX;
108 size_t jmprel_idx = SIZE_MAX;
109 size_t pltrelsz_idx = SIZE_MAX;
110 size_t strtab_idx = SIZE_MAX;
111 size_t strsz_idx = SIZE_MAX;
112 size_t symtab_idx = SIZE_MAX;
113 for (cnt = 0; (cnt + 1) * sizeof (E(Dyn)) - 1 < pmemsz; ++cnt)
115 unsigned int tag = SWAP (dyn[cnt].d_tag);
117 if (tag == DT_NULL)
118 /* We reached the end. */
119 break;
121 if (tag == DT_PLTREL)
122 pltrel_idx = cnt;
123 else if (tag == DT_JMPREL)
124 jmprel_idx = cnt;
125 else if (tag == DT_PLTRELSZ)
126 pltrelsz_idx = cnt;
127 else if (tag == DT_STRTAB)
128 strtab_idx = cnt;
129 else if (tag == DT_STRSZ)
130 strsz_idx = cnt;
131 else if (tag == DT_SYMTAB)
132 symtab_idx = cnt;
135 if (pltrel_idx == SIZE_MAX || jmprel_idx == SIZE_MAX
136 || pltrelsz_idx == SIZE_MAX || strtab_idx == SIZE_MAX
137 || strsz_idx == SIZE_MAX || symtab_idx == SIZE_MAX)
139 puts ("not all PLT information found");
140 return 1;
143 E(Xword) relsz = SWAP (dyn[pltrelsz_idx].d_un.d_val);
145 void *relmem = NULL;
146 char *strtab = NULL;
147 E(Xword) symtab_offset = 0;
149 /* Find the offset of DT_JMPREL and load the data. */
150 for (cnt = 0; cnt < phnum; ++cnt)
151 if (SWAP (phdr[cnt].p_type) == PT_LOAD)
153 E(Addr) vaddr = SWAP (phdr[cnt].p_vaddr);
154 E(Xword) memsz = SWAP (phdr[cnt].p_memsz);
156 if (vaddr <= SWAP (dyn[jmprel_idx].d_un.d_val)
157 && vaddr + memsz >= SWAP (dyn[jmprel_idx].d_un.d_val) + relsz)
159 relmem = alloca (SWAP (dyn[pltrelsz_idx].d_un.d_val));
160 if (pread64 (fd, relmem, relsz,
161 SWAP (phdr[cnt].p_offset)
162 + SWAP (dyn[jmprel_idx].d_un.d_val) - vaddr)
163 != relsz)
165 puts ("cannot read JMPREL");
166 return 1;
170 if (vaddr <= SWAP (dyn[symtab_idx].d_un.d_val)
171 && vaddr + memsz > SWAP (dyn[symtab_idx].d_un.d_val))
172 symtab_offset = (SWAP (phdr[cnt].p_offset)
173 + SWAP (dyn[symtab_idx].d_un.d_val) - vaddr);
175 if (vaddr <= SWAP (dyn[strtab_idx].d_un.d_val)
176 && vaddr + memsz >= (SWAP (dyn[strtab_idx].d_un.d_val)
177 + SWAP(dyn[strsz_idx].d_un.d_val)))
179 strtab = alloca (SWAP(dyn[strsz_idx].d_un.d_val));
180 if (pread64 (fd, strtab, SWAP(dyn[strsz_idx].d_un.d_val),
181 SWAP (phdr[cnt].p_offset)
182 + SWAP (dyn[strtab_idx].d_un.d_val) - vaddr)
183 != SWAP(dyn[strsz_idx].d_un.d_val))
185 puts ("cannot read STRTAB");
186 return 1;
191 if (relmem == NULL || strtab == NULL || symtab_offset == 0)
193 puts ("couldn't load PLT data");
194 return 1;
197 if (SWAP (dyn[pltrel_idx].d_un.d_val) == DT_RELA)
198 for (E(Rela) *rela = relmem; (char *) rela - (char *) relmem < relsz;
199 ++rela)
201 E(Sym) sym;
203 if (pread64 (fd, &sym, sizeof (sym),
204 symtab_offset
205 + EE(R_SYM) (SWAP (rela->r_info)) * sizeof (sym))
206 != sizeof (sym))
208 puts ("cannot read symbol");
209 return 1;
212 if (sym.st_value != 0)
213 /* This symbol is locally defined. */
214 printf ("%s: %s\n", basename (fname), strtab + SWAP (sym.st_name));
216 else
217 for (E(Rel) *rel = relmem; (char *) rel - (char *) relmem < relsz; ++rel)
219 E(Sym) sym;
221 if (pread64 (fd, &sym, sizeof (sym),
222 symtab_offset
223 + EE(R_SYM) (SWAP (rel->r_info)) * sizeof (sym))
224 != sizeof (sym))
226 puts ("cannot read symbol");
227 return 1;
230 if (sym.st_value != 0)
231 /* This symbol is locally defined. */
232 printf ("%s: %s\n", basename (fname), strtab + SWAP (sym.st_name));
235 return 0;
238 # undef BITS
239 #else
241 # define BITS 32
242 # include "check-localplt.c"
244 # define BITS 64
245 # include "check-localplt.c"
248 static int
249 handle_file (const char *fname)
251 int fd = open (fname, O_RDONLY);
252 if (fd == -1)
254 printf ("cannot open %s: %m\n", fname);
255 return 1;
258 /* Read was is supposed to be the ELF header. Read the initial
259 bytes to determine whether this is a 32 or 64 bit file. */
260 char ident[EI_NIDENT];
261 if (read (fd, ident, EI_NIDENT) != EI_NIDENT)
263 printf ("%s: read error: %m\n", fname);
264 close (fd);
265 return 1;
268 if (memcmp (&ident[EI_MAG0], ELFMAG, SELFMAG) != 0)
270 printf ("%s: not an ELF file\n", fname);
271 close (fd);
272 return 1;
275 int result;
276 if (ident[EI_CLASS] == ELFCLASS64)
277 result = handle_file64 (fname, fd);
278 else
279 result = handle_file32 (fname, fd);
281 close (fd);
283 return result;
288 main (int argc, char *argv[])
290 int cnt;
291 int result = 0;
293 for (cnt = 1; cnt < argc; ++cnt)
294 result |= handle_file (argv[cnt]);
296 return result;
298 #endif