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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
34 # define AB(name) _AB (name, BITS)
35 # define _AB(name, bits) __AB (name, bits)
36 # define __AB(name, bits) name##bits
37 # define E(name) _E (name, BITS)
38 # define _E(name, bits) __E (name, bits)
39 # define __E(name, bits) Elf##bits##_##name
40 # define EE(name) _EE (name, BITS)
41 # define _EE(name, bits) __EE (name, bits)
42 # define __EE(name, bits) ELF##bits##_##name
44 ({ __typeof (val) __res; \
45 if (((ehdr.e_ident[EI_DATA] == ELFDATA2MSB \
46 && BYTE_ORDER == LITTLE_ENDIAN) \
47 || (ehdr.e_ident[EI_DATA] == ELFDATA2LSB \
48 && BYTE_ORDER == BIG_ENDIAN)) \
49 && sizeof (val) != 1) \
51 if (sizeof (val) == 2) \
52 __res = bswap_16 (val); \
53 else if (sizeof (val) == 4) \
54 __res = bswap_32 (val); \
56 __res = bswap_64 (val); \
64 AB(handle_file) (const char *fname, int fd)
68 if (pread (fd, &ehdr, sizeof (ehdr), 0) != sizeof (ehdr))
71 printf ("%s: read error: %m\n", fname);
75 const size_t phnum = SWAP (ehdr.e_phnum);
76 const size_t phentsize = SWAP (ehdr.e_phentsize);
78 /* Read the program header. */
79 E(Phdr) *phdr = alloca (phentsize * phnum);
80 if (pread (fd, phdr, phentsize * phnum, SWAP (ehdr.e_phoff))
84 /* Search for the PT_DYNAMIC entry. */
86 E(Phdr) *dynphdr = NULL;
87 for (cnt = 0; cnt < phnum; ++cnt)
88 if (SWAP (phdr[cnt].p_type) == PT_DYNAMIC)
96 printf ("%s: no DYNAMIC segment found\n", fname);
100 /* Read the dynamic segment. */
101 size_t pmemsz = SWAP(dynphdr->p_memsz);
102 E(Dyn) *dyn = alloca (pmemsz);
103 if (pread64 (fd, dyn, pmemsz, SWAP(dynphdr->p_offset)) != pmemsz)
106 /* Search for an DT_PLTREL, DT_JMPREL, DT_PLTRELSZ, DT_STRTAB,
107 DT_STRSZ, and DT_SYMTAB entries. */
108 size_t pltrel_idx = SIZE_MAX;
109 size_t jmprel_idx = SIZE_MAX;
110 size_t pltrelsz_idx = SIZE_MAX;
111 size_t strtab_idx = SIZE_MAX;
112 size_t strsz_idx = SIZE_MAX;
113 size_t symtab_idx = SIZE_MAX;
114 for (cnt = 0; (cnt + 1) * sizeof (E(Dyn)) - 1 < pmemsz; ++cnt)
116 unsigned int tag = SWAP (dyn[cnt].d_tag);
119 /* We reached the end. */
122 if (tag == DT_PLTREL)
124 else if (tag == DT_JMPREL)
126 else if (tag == DT_PLTRELSZ)
128 else if (tag == DT_STRTAB)
130 else if (tag == DT_STRSZ)
132 else if (tag == DT_SYMTAB)
136 if (pltrel_idx == SIZE_MAX || jmprel_idx == SIZE_MAX
137 || pltrelsz_idx == SIZE_MAX || strtab_idx == SIZE_MAX
138 || strsz_idx == SIZE_MAX || symtab_idx == SIZE_MAX)
140 puts ("not all PLT information found");
144 E(Xword) relsz = SWAP (dyn[pltrelsz_idx].d_un.d_val);
148 E(Xword) symtab_offset = 0;
150 /* Find the offset of DT_JMPREL and load the data. */
151 for (cnt = 0; cnt < phnum; ++cnt)
152 if (SWAP (phdr[cnt].p_type) == PT_LOAD)
154 E(Addr) vaddr = SWAP (phdr[cnt].p_vaddr);
155 E(Xword) memsz = SWAP (phdr[cnt].p_memsz);
157 if (vaddr <= SWAP (dyn[jmprel_idx].d_un.d_val)
158 && vaddr + memsz >= SWAP (dyn[jmprel_idx].d_un.d_val) + relsz)
160 relmem = alloca (SWAP (dyn[pltrelsz_idx].d_un.d_val));
161 if (pread64 (fd, relmem, relsz,
162 SWAP (phdr[cnt].p_offset)
163 + SWAP (dyn[jmprel_idx].d_un.d_val) - vaddr)
166 puts ("cannot read JMPREL");
171 if (vaddr <= SWAP (dyn[symtab_idx].d_un.d_val)
172 && vaddr + memsz > SWAP (dyn[symtab_idx].d_un.d_val))
173 symtab_offset = (SWAP (phdr[cnt].p_offset)
174 + SWAP (dyn[symtab_idx].d_un.d_val) - vaddr);
176 if (vaddr <= SWAP (dyn[strtab_idx].d_un.d_val)
177 && vaddr + memsz >= (SWAP (dyn[strtab_idx].d_un.d_val)
178 + SWAP(dyn[strsz_idx].d_un.d_val)))
180 strtab = alloca (SWAP(dyn[strsz_idx].d_un.d_val));
181 if (pread64 (fd, strtab, SWAP(dyn[strsz_idx].d_un.d_val),
182 SWAP (phdr[cnt].p_offset)
183 + SWAP (dyn[strtab_idx].d_un.d_val) - vaddr)
184 != SWAP(dyn[strsz_idx].d_un.d_val))
186 puts ("cannot read STRTAB");
192 if (relmem == NULL || strtab == NULL || symtab_offset == 0)
194 puts ("couldn't load PLT data");
198 if (SWAP (dyn[pltrel_idx].d_un.d_val) == DT_RELA)
199 for (E(Rela) *rela = relmem; (char *) rela - (char *) relmem < relsz;
204 if (pread64 (fd, &sym, sizeof (sym),
206 + EE(R_SYM) (SWAP (rela->r_info)) * sizeof (sym))
209 puts ("cannot read symbol");
213 if (sym.st_value != 0)
214 /* This symbol is locally defined. */
215 printf ("%s: %s\n", basename (fname), strtab + SWAP (sym.st_name));
218 for (E(Rel) *rel = relmem; (char *) rel - (char *) relmem < relsz; ++rel)
222 if (pread64 (fd, &sym, sizeof (sym),
224 + EE(R_SYM) (SWAP (rel->r_info)) * sizeof (sym))
227 puts ("cannot read symbol");
231 if (sym.st_value != 0)
232 /* This symbol is locally defined. */
233 printf ("%s: %s\n", basename (fname), strtab + SWAP (sym.st_name));
243 # include "check-localplt.c"
246 # include "check-localplt.c"
250 handle_file (const char *fname)
252 int fd = open (fname, O_RDONLY);
255 printf ("cannot open %s: %m\n", fname);
259 /* Read was is supposed to be the ELF header. Read the initial
260 bytes to determine whether this is a 32 or 64 bit file. */
261 char ident[EI_NIDENT];
262 if (read (fd, ident, EI_NIDENT) != EI_NIDENT)
264 printf ("%s: read error: %m\n", fname);
269 if (memcmp (&ident[EI_MAG0], ELFMAG, SELFMAG) != 0)
271 printf ("%s: not an ELF file\n", fname);
277 if (ident[EI_CLASS] == ELFCLASS64)
278 result = handle_file64 (fname, fd);
280 result = handle_file32 (fname, fd);
289 main (int argc, char *argv[])
294 for (cnt = 1; cnt < argc; ++cnt)
295 result |= handle_file (argv[cnt]);