Imported Upstream version 0.155
[platform/upstream/elfutils.git] / src / findtextrel.c
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.
5
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.
10
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.
15
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/>.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <argp.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <error.h>
27 #include <fcntl.h>
28 #include <gelf.h>
29 #include <libdw.h>
30 #include <libintl.h>
31 #include <locale.h>
32 #include <search.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include <system.h>
40
41
42 struct segments
43 {
44   GElf_Addr from;
45   GElf_Addr to;
46 };
47
48
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;
52
53 /* Bug report address.  */
54 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
55
56 /* Values for the parameters which have no short form.  */
57 #define OPT_DEBUGINFO 0x100
58
59 /* Definitions of arguments for argp functions.  */
60 static const struct argp_option options[] =
61 {
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 },
66
67   { NULL, 0, NULL, 0, N_("Miscellaneous:"), 0 },
68   { NULL, 0, NULL, 0, NULL, 0 }
69 };
70
71 /* Short description of program.  */
72 static const char doc[] = N_("\
73 Locate source of text relocations in FILEs (a.out by default).");
74
75 /* Strings for arguments in help texts.  */
76 static const char args_doc[] = N_("[FILE...]");
77
78 /* Prototype for option handler.  */
79 static error_t parse_opt (int key, char *arg, struct argp_state *state);
80
81 /* Data structure to communicate with argp functions.  */
82 static struct argp argp =
83 {
84   options, parse_opt, args_doc, doc, NULL, NULL, NULL
85 };
86
87
88 /* Print symbols in file named FNAME.  */
89 static int process_file (const char *fname, bool more_than_one);
90
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,
96                        void **knownsrcs);
97
98
99
100 /* User-provided root directory.  */
101 static const char *rootdir = "/";
102
103 /* Root of debuginfo directory hierarchy.  */
104 static const char *debuginfo_root;
105
106
107 int
108 main (int argc, char *argv[])
109 {
110   int remaining;
111   int result = 0;
112
113   /* Set locale.  */
114   (void) setlocale (LC_ALL, "");
115
116   /* Make sure the message catalog can be found.  */
117   (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
118
119   /* Initialize the message catalog.  */
120   (void) textdomain (PACKAGE_TARNAME);
121
122   /* Parse and process arguments.  */
123   (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL);
124
125   /* Tell the library which version we are expecting.  */
126   elf_version (EV_CURRENT);
127
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)
131     {
132       // XXX The runtime should provide this information.
133 #if defined __ia64__ || defined __alpha__
134       debuginfo_root = "/usr/lib/debug";
135 #else
136       debuginfo_root = (sizeof (long int) == 4
137                         ? "/usr/lib/debug" : "/usr/lib64/debug");
138 #endif
139     }
140
141   if (remaining == argc)
142     result = process_file ("a.out", false);
143   else
144     {
145       /* Process all the remaining files.  */
146       const bool more_than_one = remaining + 1 < argc;
147
148       do
149         result |= process_file (argv[remaining], more_than_one);
150       while (++remaining < argc);
151     }
152
153   return result;
154 }
155
156
157 /* Print the version information.  */
158 static void
159 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
160 {
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\
166 "), "2012");
167   fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
168 }
169
170
171 /* Handle program arguments.  */
172 static error_t
173 parse_opt (int key, char *arg,
174            struct argp_state *state __attribute__ ((unused)))
175 {
176   switch (key)
177     {
178     case 'r':
179       rootdir = arg;
180       break;
181
182     case OPT_DEBUGINFO:
183       debuginfo_root = arg;
184       break;
185
186     default:
187       return ARGP_ERR_UNKNOWN;
188     }
189   return 0;
190 }
191
192
193 static void
194 noop (void *arg __attribute__ ((unused)))
195 {
196 }
197
198
199 static int
200 process_file (const char *fname, bool more_than_one)
201 {
202   int result = 0;
203   void *knownsrcs = NULL;
204
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'))
209     {
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),
213                                   "/"),
214                           fname, fname_len)) = '\0';
215       real_fname = new_fname;
216     }
217
218   int fd = open64 (real_fname, O_RDONLY);
219   if (fd == -1)
220     {
221       error (0, errno, gettext ("cannot open '%s'"), fname);
222       return 1;
223     }
224
225   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
226   if (elf == NULL)
227     {
228       error (0, 0, gettext ("cannot create ELF descriptor for '%s': %s"),
229              fname, elf_errmsg (-1));
230       goto err_close;
231     }
232
233   /* Make sure the file is a DSO.  */
234   GElf_Ehdr ehdr_mem;
235   GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
236   if (ehdr == NULL)
237     {
238       error (0, 0, gettext ("cannot get ELF header '%s': %s"),
239              fname, elf_errmsg (-1));
240     err_elf_close:
241       elf_end (elf);
242     err_close:
243       close (fd);
244       return 1;
245     }
246
247   if (ehdr->e_type != ET_DYN)
248     {
249       error (0, 0, gettext ("'%s' is not a DSO or PIE"), fname);
250       goto err_elf_close;
251     }
252
253   /* Determine whether the DSO has text relocations at all and locate
254      the symbol table.  */
255   Elf_Scn *symscn = NULL;
256   Elf_Scn *scn = NULL;
257   bool seen_dynamic = false;
258   bool have_textrel = false;
259   while ((scn = elf_nextscn (elf, scn)) != NULL
260          && (!seen_dynamic || symscn == NULL))
261     {
262       /* Handle the section if it is a symbol table.  */
263       GElf_Shdr shdr_mem;
264       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
265
266       if (shdr == NULL)
267         {
268           error (0, 0,
269                  gettext ("getting get section header of section %zu: %s"),
270                  elf_ndxscn (scn), elf_errmsg (-1));
271           goto err_elf_close;
272         }
273
274       switch (shdr->sh_type)
275         {
276         case SHT_DYNAMIC:
277           if (!seen_dynamic)
278             {
279               seen_dynamic = true;
280
281               Elf_Data *data = elf_getdata (scn, NULL);
282
283               for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize;
284                    ++cnt)
285                 {
286                   GElf_Dyn dynmem;
287                   GElf_Dyn *dyn;
288
289                   dyn = gelf_getdyn (data, cnt, &dynmem);
290                   if (dyn == NULL)
291                     {
292                       error (0, 0, gettext ("cannot read dynamic section: %s"),
293                              elf_errmsg (-1));
294                       goto err_elf_close;
295                     }
296
297                   if (dyn->d_tag == DT_TEXTREL
298                       || (dyn->d_tag == DT_FLAGS
299                           && (dyn->d_un.d_val & DF_TEXTREL) != 0))
300                     have_textrel = true;
301                 }
302             }
303           break;
304
305         case SHT_SYMTAB:
306           symscn = scn;
307           break;
308         }
309     }
310
311   if (!have_textrel)
312     {
313       error (0, 0, gettext ("no text relocations reported in '%s'"), fname);
314       return 1;
315     }
316
317   int fd2 = -1;
318   Elf *elf2 = NULL;
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"));
326
327   for (int i = 0; i < ehdr->e_phnum; ++i)
328     {
329       GElf_Phdr phdr_mem;
330       GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
331       if (phdr == NULL)
332         {
333           error (0, 0,
334                  gettext ("cannot get program header index at offset %d: %s"),
335                  i, elf_errmsg (-1));
336           result = 1;
337           goto next;
338         }
339
340       if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
341         {
342           if (nsegments == nsegments_max)
343             {
344               nsegments_max *= 2;
345               segments
346                 = (struct segments *) realloc (segments,
347                                                nsegments_max
348                                                * sizeof (segments[0]));
349               if (segments == NULL)
350                 {
351                   error (0, 0, gettext ("\
352 cannot get program header index at offset %d: %s"),
353                          i, elf_errmsg (-1));
354                   result = 1;
355                   goto next;
356                 }
357             }
358
359           segments[nsegments].from = phdr->p_vaddr;
360           segments[nsegments].to = phdr->p_vaddr + phdr->p_memsz;
361           ++nsegments;
362         }
363     }
364
365   if (nsegments > 0)
366     {
367
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] == '/')
373         {
374           size_t debuginfo_rootlen = strlen (debuginfo_root);
375           char *difname = (char *) alloca (rootdir_len + debuginfo_rootlen
376                                            + fname_len + 8);
377           strcpy (mempcpy (stpcpy (mempcpy (mempcpy (difname, rootdir,
378                                                      rootdir_len),
379                                             debuginfo_root,
380                                             debuginfo_rootlen),
381                                    "/"),
382                            fname, fname_len),
383                   ".debug");
384
385           fd2 = open64 (difname, O_RDONLY);
386           if (fd2 != -1
387               && (elf2 = elf_begin (fd2, ELF_C_READ_MMAP, NULL)) != NULL)
388             dw = dwarf_begin_elf (elf2, DWARF_C_READ, NULL);
389         }
390
391       /* Look at all relocations and determine which modify
392          write-protected segments.  */
393       scn = NULL;
394       while ((scn = elf_nextscn (elf, scn)) != NULL)
395         {
396           /* Handle the section if it is a symbol table.  */
397           GElf_Shdr shdr_mem;
398           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
399
400           if (shdr == NULL)
401             {
402               error (0, 0,
403                      gettext ("cannot get section header of section %Zu: %s"),
404                      elf_ndxscn (scn), elf_errmsg (-1));
405               result = 1;
406               goto next;
407             }
408
409           if ((shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
410               && symscn == NULL)
411             {
412               symscn = elf_getscn (elf, shdr->sh_link);
413               if (symscn == NULL)
414                 {
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));
418                   result = 1;
419                   goto next;
420                 }
421             }
422
423           if (shdr->sh_type == SHT_REL)
424             {
425               Elf_Data *data = elf_getdata (scn, NULL);
426
427               for (int cnt = 0;
428                    (size_t) cnt < shdr->sh_size / shdr->sh_entsize;
429                    ++cnt)
430                 {
431                   GElf_Rel rel_mem;
432                   GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
433                   if (rel == NULL)
434                     {
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));
438                       result = 1;
439                       goto next;
440                     }
441
442                   check_rel (nsegments, segments, rel->r_offset, elf,
443                              symscn, dw, fname, more_than_one, &knownsrcs);
444                 }
445             }
446           else if (shdr->sh_type == SHT_RELA)
447             {
448               Elf_Data *data = elf_getdata (scn, NULL);
449
450               for (int cnt = 0;
451                    (size_t) cnt < shdr->sh_size / shdr->sh_entsize;
452                    ++cnt)
453                 {
454                   GElf_Rela rela_mem;
455                   GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
456                   if (rela == NULL)
457                     {
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));
461                       result = 1;
462                       goto next;
463                     }
464
465                   check_rel (nsegments, segments, rela->r_offset, elf,
466                              symscn, dw, fname, more_than_one, &knownsrcs);
467                 }
468             }
469         }
470
471       dwarf_end (dw);
472     }
473
474  next:
475   elf_end (elf);
476   elf_end (elf2);
477   close (fd);
478   if (fd2 != -1)
479     close (fd2);
480
481   tdestroy (knownsrcs, noop);
482
483   return result;
484 }
485
486
487 static int
488 ptrcompare (const void *p1, const void *p2)
489 {
490   if ((uintptr_t) p1 < (uintptr_t) p2)
491     return -1;
492   if ((uintptr_t) p1 > (uintptr_t) p2)
493     return 1;
494   return 0;
495 }
496
497
498 static void
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)
502 {
503   for (size_t cnt = 0; cnt < nsegments; ++cnt)
504     if (segments[cnt].from <= addr && segments[cnt].to > addr)
505       {
506         Dwarf_Die die_mem;
507         Dwarf_Die *die;
508         Dwarf_Line *line;
509         const char *src;
510
511         if (more_than_one)
512           printf ("%s: ", fname);
513
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)
517           {
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)
522               {
523                 printf (gettext ("%s not compiled with -fpic/-fPIC\n"), src);
524                 tsearch (src, knownsrcs, ptrcompare);
525               }
526             return;
527           }
528         else
529           {
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);
533             GElf_Shdr shdr_mem;
534             GElf_Shdr *shdr = gelf_getshdr (symscn, &shdr_mem);
535             if (shdr != NULL)
536               {
537                 GElf_Addr lowaddr = 0;
538                 int lowidx = -1;
539                 GElf_Addr highaddr = ~0ul;
540                 int highidx = -1;
541                 GElf_Sym sym_mem;
542                 GElf_Sym *sym;
543
544                 for (int i = 0; (size_t) i < shdr->sh_size / shdr->sh_entsize;
545                      ++i)
546                   {
547                     sym = gelf_getsym (symdata, i, &sym_mem);
548                     if (sym == NULL)
549                       continue;
550
551                     if (sym->st_value < addr && sym->st_value > lowaddr)
552                       {
553                         lowaddr = sym->st_value;
554                         lowidx = i;
555                       }
556                     if (sym->st_value > addr && sym->st_value < highaddr)
557                       {
558                         highaddr = sym->st_value;
559                         highidx = i;
560                       }
561                   }
562
563                 if (lowidx != -1)
564                   {
565                     sym = gelf_getsym (symdata, lowidx, &sym_mem);
566                     assert (sym != NULL);
567
568                     const char *lowstr = elf_strptr (elf, shdr->sh_link,
569                                                      sym->st_name);
570
571                     if (sym->st_value + sym->st_size > addr)
572                       {
573                         /* It is this function.  */
574                         if (tfind (lowstr, knownsrcs, ptrcompare) == NULL)
575                           {
576                             printf (gettext ("\
577 the file containing the function '%s' is not compiled with -fpic/-fPIC\n"),
578                                     lowstr);
579                             tsearch (lowstr, knownsrcs, ptrcompare);
580                           }
581                       }
582                     else if (highidx == -1)
583                       printf (gettext ("\
584 the file containing the function '%s' might not be compiled with -fpic/-fPIC\n"),
585                               lowstr);
586                     else
587                       {
588                         sym = gelf_getsym (symdata, highidx, &sym_mem);
589                         assert (sym != NULL);
590
591                         printf (gettext ("\
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,
594                                                     sym->st_name));
595                       }
596                     return;
597                   }
598                 else if (highidx != -1)
599                   {
600                     sym = gelf_getsym (symdata, highidx, &sym_mem);
601                     assert (sym != NULL);
602
603                     printf (gettext ("\
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));
606                     return;
607                   }
608               }
609           }
610
611         printf (gettext ("\
612 a relocation modifies memory at offset %llu in a write-protected segment\n"),
613                 (unsigned long long int) addr);
614         break;
615       }
616 }
617
618
619 #include "debugpred.h"