1 /* Locate source files or functions which caused text relocations.
2 Copyright (C) 2005-2010, 2012 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2005.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 elfutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
49 /* Name and version of program. */
50 static void print_version (FILE *stream, struct argp_state *state);
51 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
53 /* Bug report address. */
54 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
56 /* Values for the parameters which have no short form. */
57 #define OPT_DEBUGINFO 0x100
59 /* Definitions of arguments for argp functions. */
60 static const struct argp_option options[] =
62 { NULL, 0, NULL, 0, N_("Input Selection:"), 0 },
63 { "root", 'r', "PATH", 0, N_("Prepend PATH to all file names"), 0 },
64 { "debuginfo", OPT_DEBUGINFO, "PATH", 0,
65 N_("Use PATH as root of debuginfo hierarchy"), 0 },
67 { NULL, 0, NULL, 0, N_("Miscellaneous:"), 0 },
68 { NULL, 0, NULL, 0, NULL, 0 }
71 /* Short description of program. */
72 static const char doc[] = N_("\
73 Locate source of text relocations in FILEs (a.out by default).");
75 /* Strings for arguments in help texts. */
76 static const char args_doc[] = N_("[FILE...]");
78 /* Prototype for option handler. */
79 static error_t parse_opt (int key, char *arg, struct argp_state *state);
81 /* Data structure to communicate with argp functions. */
82 static struct argp argp =
84 options, parse_opt, args_doc, doc, NULL, NULL, NULL
88 /* Print symbols in file named FNAME. */
89 static int process_file (const char *fname, bool more_than_one);
91 /* Check for text relocations in the given file. The segment
92 information is known. */
93 static void check_rel (size_t nsegments, struct segments segments[nsegments],
94 GElf_Addr addr, Elf *elf, Elf_Scn *symscn, Dwarf *dw,
95 const char *fname, bool more_than_one,
100 /* User-provided root directory. */
101 static const char *rootdir = "/";
103 /* Root of debuginfo directory hierarchy. */
104 static const char *debuginfo_root;
108 main (int argc, char *argv[])
114 (void) setlocale (LC_ALL, "");
116 /* Make sure the message catalog can be found. */
117 (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
119 /* Initialize the message catalog. */
120 (void) textdomain (PACKAGE_TARNAME);
122 /* Parse and process arguments. */
123 (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL);
125 /* Tell the library which version we are expecting. */
126 elf_version (EV_CURRENT);
128 /* If the user has not specified the root directory for the
129 debuginfo hierarchy, we have to determine it ourselves. */
130 if (debuginfo_root == NULL)
132 // XXX The runtime should provide this information.
133 #if defined __ia64__ || defined __alpha__
134 debuginfo_root = "/usr/lib/debug";
136 debuginfo_root = (sizeof (long int) == 4
137 ? "/usr/lib/debug" : "/usr/lib64/debug");
141 if (remaining == argc)
142 result = process_file ("a.out", false);
145 /* Process all the remaining files. */
146 const bool more_than_one = remaining + 1 < argc;
149 result |= process_file (argv[remaining], more_than_one);
150 while (++remaining < argc);
157 /* Print the version information. */
159 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
161 fprintf (stream, "findtextrel (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
162 fprintf (stream, gettext ("\
163 Copyright (C) %s Red Hat, Inc.\n\
164 This is free software; see the source for copying conditions. There is NO\n\
165 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
167 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
171 /* Handle program arguments. */
173 parse_opt (int key, char *arg,
174 struct argp_state *state __attribute__ ((unused)))
183 debuginfo_root = arg;
187 return ARGP_ERR_UNKNOWN;
194 noop (void *arg __attribute__ ((unused)))
200 process_file (const char *fname, bool more_than_one)
203 void *knownsrcs = NULL;
205 size_t fname_len = strlen (fname);
206 size_t rootdir_len = strlen (rootdir);
207 const char *real_fname = fname;
208 if (fname[0] == '/' && (rootdir[0] != '/' || rootdir[1] != '\0'))
210 /* Prepend the user-provided root directory. */
211 char *new_fname = alloca (rootdir_len + fname_len + 2);
212 *((char *) mempcpy (stpcpy (mempcpy (new_fname, rootdir, rootdir_len),
214 fname, fname_len)) = '\0';
215 real_fname = new_fname;
218 int fd = open64 (real_fname, O_RDONLY);
221 error (0, errno, gettext ("cannot open '%s'"), fname);
225 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
228 error (0, 0, gettext ("cannot create ELF descriptor for '%s': %s"),
229 fname, elf_errmsg (-1));
233 /* Make sure the file is a DSO. */
235 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
238 error (0, 0, gettext ("cannot get ELF header '%s': %s"),
239 fname, elf_errmsg (-1));
247 if (ehdr->e_type != ET_DYN)
249 error (0, 0, gettext ("'%s' is not a DSO or PIE"), fname);
253 /* Determine whether the DSO has text relocations at all and locate
255 Elf_Scn *symscn = NULL;
257 bool seen_dynamic = false;
258 bool have_textrel = false;
259 while ((scn = elf_nextscn (elf, scn)) != NULL
260 && (!seen_dynamic || symscn == NULL))
262 /* Handle the section if it is a symbol table. */
264 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
269 gettext ("getting get section header of section %zu: %s"),
270 elf_ndxscn (scn), elf_errmsg (-1));
274 switch (shdr->sh_type)
281 Elf_Data *data = elf_getdata (scn, NULL);
283 for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize;
289 dyn = gelf_getdyn (data, cnt, &dynmem);
292 error (0, 0, gettext ("cannot read dynamic section: %s"),
297 if (dyn->d_tag == DT_TEXTREL
298 || (dyn->d_tag == DT_FLAGS
299 && (dyn->d_un.d_val & DF_TEXTREL) != 0))
313 error (0, 0, gettext ("no text relocations reported in '%s'"), fname);
319 /* Get the address ranges for the loaded segments. */
320 size_t nsegments_max = 10;
321 size_t nsegments = 0;
322 struct segments *segments
323 = (struct segments *) malloc (nsegments_max * sizeof (segments[0]));
324 if (segments == NULL)
325 error (1, errno, gettext ("while reading ELF file"));
327 for (int i = 0; i < ehdr->e_phnum; ++i)
330 GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
334 gettext ("cannot get program header index at offset %d: %s"),
340 if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
342 if (nsegments == nsegments_max)
346 = (struct segments *) realloc (segments,
348 * sizeof (segments[0]));
349 if (segments == NULL)
351 error (0, 0, gettext ("\
352 cannot get program header index at offset %d: %s"),
359 segments[nsegments].from = phdr->p_vaddr;
360 segments[nsegments].to = phdr->p_vaddr + phdr->p_memsz;
368 Dwarf *dw = dwarf_begin_elf (elf, DWARF_C_READ, NULL);
369 /* Look for debuginfo files if the information is not the in
370 opened file itself. This makes only sense if the input file
371 is specified with an absolute path. */
372 if (dw == NULL && fname[0] == '/')
374 size_t debuginfo_rootlen = strlen (debuginfo_root);
375 char *difname = (char *) alloca (rootdir_len + debuginfo_rootlen
377 strcpy (mempcpy (stpcpy (mempcpy (mempcpy (difname, rootdir,
385 fd2 = open64 (difname, O_RDONLY);
387 && (elf2 = elf_begin (fd2, ELF_C_READ_MMAP, NULL)) != NULL)
388 dw = dwarf_begin_elf (elf2, DWARF_C_READ, NULL);
391 /* Look at all relocations and determine which modify
392 write-protected segments. */
394 while ((scn = elf_nextscn (elf, scn)) != NULL)
396 /* Handle the section if it is a symbol table. */
398 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
403 gettext ("cannot get section header of section %Zu: %s"),
404 elf_ndxscn (scn), elf_errmsg (-1));
409 if ((shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
412 symscn = elf_getscn (elf, shdr->sh_link);
415 error (0, 0, gettext ("\
416 cannot get symbol table section %zu in '%s': %s"),
417 (size_t) shdr->sh_link, fname, elf_errmsg (-1));
423 if (shdr->sh_type == SHT_REL)
425 Elf_Data *data = elf_getdata (scn, NULL);
428 (size_t) cnt < shdr->sh_size / shdr->sh_entsize;
432 GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
435 error (0, 0, gettext ("\
436 cannot get relocation at index %d in section %zu in '%s': %s"),
437 cnt, elf_ndxscn (scn), fname, elf_errmsg (-1));
442 check_rel (nsegments, segments, rel->r_offset, elf,
443 symscn, dw, fname, more_than_one, &knownsrcs);
446 else if (shdr->sh_type == SHT_RELA)
448 Elf_Data *data = elf_getdata (scn, NULL);
451 (size_t) cnt < shdr->sh_size / shdr->sh_entsize;
455 GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
458 error (0, 0, gettext ("\
459 cannot get relocation at index %d in section %zu in '%s': %s"),
460 cnt, elf_ndxscn (scn), fname, elf_errmsg (-1));
465 check_rel (nsegments, segments, rela->r_offset, elf,
466 symscn, dw, fname, more_than_one, &knownsrcs);
481 tdestroy (knownsrcs, noop);
488 ptrcompare (const void *p1, const void *p2)
490 if ((uintptr_t) p1 < (uintptr_t) p2)
492 if ((uintptr_t) p1 > (uintptr_t) p2)
499 check_rel (size_t nsegments, struct segments segments[nsegments],
500 GElf_Addr addr, Elf *elf, Elf_Scn *symscn, Dwarf *dw,
501 const char *fname, bool more_than_one, void **knownsrcs)
503 for (size_t cnt = 0; cnt < nsegments; ++cnt)
504 if (segments[cnt].from <= addr && segments[cnt].to > addr)
512 printf ("%s: ", fname);
514 if ((die = dwarf_addrdie (dw, addr, &die_mem)) != NULL
515 && (line = dwarf_getsrc_die (die, addr)) != NULL
516 && (src = dwarf_linesrc (line, NULL, NULL)) != NULL)
518 /* There can be more than one relocation against one file.
519 Try to avoid multiple messages. And yes, the code uses
520 pointer comparison. */
521 if (tfind (src, knownsrcs, ptrcompare) == NULL)
523 printf (gettext ("%s not compiled with -fpic/-fPIC\n"), src);
524 tsearch (src, knownsrcs, ptrcompare);
530 /* At least look at the symbol table to see which function
531 the modified address is in. */
532 Elf_Data *symdata = elf_getdata (symscn, NULL);
534 GElf_Shdr *shdr = gelf_getshdr (symscn, &shdr_mem);
537 GElf_Addr lowaddr = 0;
539 GElf_Addr highaddr = ~0ul;
544 for (int i = 0; (size_t) i < shdr->sh_size / shdr->sh_entsize;
547 sym = gelf_getsym (symdata, i, &sym_mem);
551 if (sym->st_value < addr && sym->st_value > lowaddr)
553 lowaddr = sym->st_value;
556 if (sym->st_value > addr && sym->st_value < highaddr)
558 highaddr = sym->st_value;
565 sym = gelf_getsym (symdata, lowidx, &sym_mem);
566 assert (sym != NULL);
568 const char *lowstr = elf_strptr (elf, shdr->sh_link,
571 if (sym->st_value + sym->st_size > addr)
573 /* It is this function. */
574 if (tfind (lowstr, knownsrcs, ptrcompare) == NULL)
577 the file containing the function '%s' is not compiled with -fpic/-fPIC\n"),
579 tsearch (lowstr, knownsrcs, ptrcompare);
582 else if (highidx == -1)
584 the file containing the function '%s' might not be compiled with -fpic/-fPIC\n"),
588 sym = gelf_getsym (symdata, highidx, &sym_mem);
589 assert (sym != NULL);
592 either the file containing the function '%s' or the file containing the function '%s' is not compiled with -fpic/-fPIC\n"),
593 lowstr, elf_strptr (elf, shdr->sh_link,
598 else if (highidx != -1)
600 sym = gelf_getsym (symdata, highidx, &sym_mem);
601 assert (sym != NULL);
604 the file containing the function '%s' might not be compiled with -fpic/-fPIC\n"),
605 elf_strptr (elf, shdr->sh_link, sym->st_name));
612 a relocation modifies memory at offset %llu in a write-protected segment\n"),
613 (unsigned long long int) addr);
619 #include "debugpred.h"