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/>. */
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
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) \
50 if (sizeof (val) == 2) \
51 __res = bswap_16 (val); \
52 else if (sizeof (val) == 4) \
53 __res = bswap_32 (val); \
55 __res = bswap_64 (val); \
63 AB(handle_file
) (const char *fname
, int fd
)
67 if (pread (fd
, &ehdr
, sizeof (ehdr
), 0) != sizeof (ehdr
))
70 printf ("%s: read error: %m\n", fname
);
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
))
83 /* Search for the PT_DYNAMIC entry. */
85 E(Phdr
) *dynphdr
= NULL
;
86 for (cnt
= 0; cnt
< phnum
; ++cnt
)
87 if (SWAP (phdr
[cnt
].p_type
) == PT_DYNAMIC
)
95 printf ("%s: no DYNAMIC segment found\n", fname
);
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
)
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
);
118 /* We reached the end. */
121 if (tag
== DT_PLTREL
)
123 else if (tag
== DT_JMPREL
)
125 else if (tag
== DT_PLTRELSZ
)
127 else if (tag
== DT_STRTAB
)
129 else if (tag
== DT_STRSZ
)
131 else if (tag
== DT_SYMTAB
)
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");
143 E(Xword
) relsz
= SWAP (dyn
[pltrelsz_idx
].d_un
.d_val
);
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
)
165 puts ("cannot read JMPREL");
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");
191 if (relmem
== NULL
|| strtab
== NULL
|| symtab_offset
== 0)
193 puts ("couldn't load PLT data");
197 if (SWAP (dyn
[pltrel_idx
].d_un
.d_val
) == DT_RELA
)
198 for (E(Rela
) *rela
= relmem
; (char *) rela
- (char *) relmem
< relsz
;
203 if (pread64 (fd
, &sym
, sizeof (sym
),
205 + EE(R_SYM
) (SWAP (rela
->r_info
)) * sizeof (sym
))
208 puts ("cannot read symbol");
212 if (sym
.st_value
!= 0)
213 /* This symbol is locally defined. */
214 printf ("%s: %s\n", basename (fname
), strtab
+ SWAP (sym
.st_name
));
217 for (E(Rel
) *rel
= relmem
; (char *) rel
- (char *) relmem
< relsz
; ++rel
)
221 if (pread64 (fd
, &sym
, sizeof (sym
),
223 + EE(R_SYM
) (SWAP (rel
->r_info
)) * sizeof (sym
))
226 puts ("cannot read symbol");
230 if (sym
.st_value
!= 0)
231 /* This symbol is locally defined. */
232 printf ("%s: %s\n", basename (fname
), strtab
+ SWAP (sym
.st_name
));
242 # include "check-localplt.c"
245 # include "check-localplt.c"
249 handle_file (const char *fname
)
251 int fd
= open (fname
, O_RDONLY
);
254 printf ("cannot open %s: %m\n", fname
);
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
);
268 if (memcmp (&ident
[EI_MAG0
], ELFMAG
, SELFMAG
) != 0)
270 printf ("%s: not an ELF file\n", fname
);
276 if (ident
[EI_CLASS
] == ELFCLASS64
)
277 result
= handle_file64 (fname
, fd
);
279 result
= handle_file32 (fname
, fd
);
288 main (int argc
, char *argv
[])
293 for (cnt
= 1; cnt
< argc
; ++cnt
)
294 result
|= handle_file (argv
[cnt
]);