LCOV - code coverage report
Current view: top level - libdwfl - linux-kernel-modules.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 0 357 0.0 %
Date: 2017-01-05 09:15:16 Functions: 0 16 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Standard libdwfl callbacks for debugging the running Linux kernel.
       2             :    Copyright (C) 2005-2011, 2013, 2014, 2015 Red Hat, Inc.
       3             :    This file is part of elfutils.
       4             : 
       5             :    This file is free software; you can redistribute it and/or modify
       6             :    it under the terms of either
       7             : 
       8             :      * the GNU Lesser General Public License as published by the Free
       9             :        Software Foundation; either version 3 of the License, or (at
      10             :        your option) any later version
      11             : 
      12             :    or
      13             : 
      14             :      * the GNU General Public License as published by the Free
      15             :        Software Foundation; either version 2 of the License, or (at
      16             :        your option) any later version
      17             : 
      18             :    or both in parallel, as here.
      19             : 
      20             :    elfutils is distributed in the hope that it will be useful, but
      21             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      22             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23             :    General Public License for more details.
      24             : 
      25             :    You should have received copies of the GNU General Public License and
      26             :    the GNU Lesser General Public License along with this program.  If
      27             :    not, see <http://www.gnu.org/licenses/>.  */
      28             : 
      29             : /* In case we have a bad fts we include this before config.h because it
      30             :    can't handle _FILE_OFFSET_BITS.
      31             :    Everything we need here is fine if its declarations just come first.  */
      32             : #ifdef BAD_FTS
      33             :   #include <fts.h>
      34             : #endif
      35             : 
      36             : #include <config.h>
      37             : 
      38             : #include "libdwflP.h"
      39             : #include <inttypes.h>
      40             : #include <errno.h>
      41             : #include <stdio.h>
      42             : #include <stdio_ext.h>
      43             : #include <string.h>
      44             : #include <stdlib.h>
      45             : #include <sys/utsname.h>
      46             : #include <fcntl.h>
      47             : #include <unistd.h>
      48             : 
      49             : /* If fts.h is included before config.h, its indirect inclusions may not
      50             :    give us the right LFS aliases of these functions, so map them manually.  */
      51             : #ifdef BAD_FTS
      52             :   #ifdef _FILE_OFFSET_BITS
      53             :     #define open open64
      54             :     #define fopen fopen64
      55             :   #endif
      56             : #else
      57             :   #include <fts.h>
      58             : #endif
      59             : 
      60             : 
      61             : #define KERNEL_MODNAME  "kernel"
      62             : 
      63             : #define MODULEDIRFMT    "/lib/modules/%s"
      64             : 
      65             : #define KNOTESFILE      "/sys/kernel/notes"
      66             : #define MODNOTESFMT     "/sys/module/%s/notes"
      67             : #define KSYMSFILE       "/proc/kallsyms"
      68             : #define MODULELIST      "/proc/modules"
      69             : #define SECADDRDIRFMT   "/sys/module/%s/sections/"
      70             : #define MODULE_SECT_NAME_LEN 32 /* Minimum any linux/module.h has had.  */
      71             : 
      72             : 
      73             : static const char *vmlinux_suffixes[] =
      74             :   {
      75             :     ".gz",
      76             : #ifdef USE_BZLIB
      77             :     ".bz2",
      78             : #endif
      79             : #ifdef USE_LZMA
      80             :     ".xz",
      81             : #endif
      82             :   };
      83             : 
      84             : /* Try to open the given file as it is or under the debuginfo directory.  */
      85             : static int
      86           0 : try_kernel_name (Dwfl *dwfl, char **fname, bool try_debug)
      87             : {
      88           0 :   if (*fname == NULL)
      89             :     return -1;
      90             : 
      91             :   /* Don't bother trying *FNAME itself here if the path will cause it to be
      92             :      tried because we give its own basename as DEBUGLINK_FILE.  */
      93           0 :   int fd = ((((dwfl->callbacks->debuginfo_path
      94           0 :                ? *dwfl->callbacks->debuginfo_path : NULL)
      95           0 :               ?: DEFAULT_DEBUGINFO_PATH)[0] == ':') ? -1
      96           0 :             : TEMP_FAILURE_RETRY (open (*fname, O_RDONLY)));
      97             : 
      98           0 :   if (fd < 0)
      99             :     {
     100           0 :       Dwfl_Module fakemod = { .dwfl = dwfl };
     101             : 
     102           0 :       if (try_debug)
     103             :         /* Passing NULL for DEBUGLINK_FILE searches for both the basenamer
     104             :            "vmlinux" and the default of basename + ".debug", to look for
     105             :            "vmlinux.debug" files.  */
     106           0 :         fd = INTUSE(dwfl_standard_find_debuginfo) (&fakemod, NULL, NULL, 0,
     107             :                                                    *fname, NULL, 0,
     108             :                                                    &fakemod.debug.name);
     109             :       else
     110             :         /* Try the file's unadorned basename as DEBUGLINK_FILE,
     111             :            to look only for "vmlinux" files.  */
     112           0 :         fd = INTUSE(dwfl_standard_find_debuginfo) (&fakemod, NULL, NULL, 0,
     113           0 :                                                    *fname, basename (*fname),
     114             :                                                    0, &fakemod.debug.name);
     115             : 
     116           0 :       if (fakemod.debug.name != NULL)
     117             :         {
     118           0 :           free (*fname);
     119           0 :           *fname = fakemod.debug.name;
     120             :         }
     121             :     }
     122             : 
     123           0 :   if (fd < 0)
     124           0 :     for (size_t i = 0;
     125             :          i < sizeof vmlinux_suffixes / sizeof vmlinux_suffixes[0];
     126           0 :          ++i)
     127             :       {
     128             :         char *zname;
     129           0 :         if (asprintf (&zname, "%s%s", *fname, vmlinux_suffixes[i]) > 0)
     130             :           {
     131           0 :             fd = TEMP_FAILURE_RETRY (open (zname, O_RDONLY));
     132           0 :             if (fd < 0)
     133           0 :               free (zname);
     134             :             else
     135             :               {
     136           0 :                 free (*fname);
     137           0 :                 *fname = zname;
     138             :               }
     139             :           }
     140             :       }
     141             : 
     142           0 :   if (fd < 0)
     143             :     {
     144           0 :       free (*fname);
     145           0 :       *fname = NULL;
     146             :     }
     147             : 
     148             :   return fd;
     149             : }
     150             : 
     151             : static inline const char *
     152             : kernel_release (void)
     153             : {
     154             :   /* Cache the `uname -r` string we'll use.  */
     155             :   static struct utsname utsname;
     156           0 :   if (utsname.release[0] == '\0' && uname (&utsname) != 0)
     157             :     return NULL;
     158             :   return utsname.release;
     159             : }
     160             : 
     161             : static int
     162           0 : find_kernel_elf (Dwfl *dwfl, const char *release, char **fname)
     163             : {
     164           0 :   if ((release[0] == '/'
     165           0 :        ? asprintf (fname, "%s/vmlinux", release)
     166           0 :        : asprintf (fname, "/boot/vmlinux-%s", release)) < 0)
     167             :     return -1;
     168             : 
     169           0 :   int fd = try_kernel_name (dwfl, fname, true);
     170           0 :   if (fd < 0 && release[0] != '/')
     171             :     {
     172           0 :       free (*fname);
     173           0 :       if (asprintf (fname, MODULEDIRFMT "/vmlinux", release) < 0)
     174             :         return -1;
     175           0 :       fd = try_kernel_name (dwfl, fname, true);
     176             :     }
     177             : 
     178             :   return fd;
     179             : }
     180             : 
     181             : static int
     182           0 : get_release (Dwfl *dwfl, const char **release)
     183             : {
     184           0 :   if (dwfl == NULL)
     185             :     return -1;
     186             : 
     187           0 :   const char *release_string = release == NULL ? NULL : *release;
     188           0 :   if (release_string == NULL)
     189             :     {
     190           0 :       release_string = kernel_release ();
     191           0 :       if (release_string == NULL)
     192           0 :         return errno;
     193           0 :       if (release != NULL)
     194           0 :         *release = release_string;
     195             :     }
     196             : 
     197             :   return 0;
     198             : }
     199             : 
     200             : static int
     201           0 : report_kernel (Dwfl *dwfl, const char **release,
     202             :                int (*predicate) (const char *module, const char *file))
     203             : {
     204           0 :   int result = get_release (dwfl, release);
     205           0 :   if (unlikely (result != 0))
     206             :     return result;
     207             : 
     208             :   char *fname;
     209           0 :   int fd = find_kernel_elf (dwfl, *release, &fname);
     210             : 
     211           0 :   if (fd < 0)
     212           0 :     result = ((predicate != NULL && !(*predicate) (KERNEL_MODNAME, NULL))
     213           0 :               ? 0 : errno ?: ENOENT);
     214             :   else
     215             :     {
     216           0 :       bool report = true;
     217             : 
     218           0 :       if (predicate != NULL)
     219             :         {
     220             :           /* Let the predicate decide whether to use this one.  */
     221           0 :           int want = (*predicate) (KERNEL_MODNAME, fname);
     222           0 :           if (want < 0)
     223           0 :             result = errno;
     224           0 :           report = want > 0;
     225             :         }
     226             : 
     227           0 :       if (report)
     228             :         {
     229             :           /* Note that on some architectures (e.g. x86_64) the vmlinux
     230             :              is ET_EXEC, while on others (e.g. ppc64) it is ET_DYN.
     231             :              In both cases the phdr p_vaddr load address will be non-zero.
     232             :              We want the image to be placed as if it was ET_DYN, so
     233             :              pass true for add_p_vaddr which will do the right thing
     234             :              (in combination with a zero base) in either case.  */
     235           0 :           Dwfl_Module *mod = INTUSE(dwfl_report_elf) (dwfl, KERNEL_MODNAME,
     236             :                                                       fname, fd, 0, true);
     237           0 :           if (mod == NULL)
     238             :             result = -1;
     239             :           else
     240             :             /* The kernel is ET_EXEC, but always treat it as relocatable.  */
     241           0 :             mod->e_type = ET_DYN;
     242             :         }
     243             : 
     244           0 :       free (fname);
     245             : 
     246           0 :       if (!report || result < 0)
     247           0 :         close (fd);
     248             :     }
     249             : 
     250             :   return result;
     251             : }
     252             : 
     253             : /* Look for a kernel debug archive.  If we find one, report all its modules.
     254             :    If not, return ENOENT.  */
     255             : static int
     256           0 : report_kernel_archive (Dwfl *dwfl, const char **release,
     257             :                        int (*predicate) (const char *module, const char *file))
     258             : {
     259           0 :   int result = get_release (dwfl, release);
     260           0 :   if (unlikely (result != 0))
     261             :     return result;
     262             : 
     263             :   char *archive;
     264           0 :   int res = (((*release)[0] == '/')
     265             :              ? asprintf (&archive, "%s/debug.a", *release)
     266           0 :              : asprintf (&archive, MODULEDIRFMT "/debug.a", *release));
     267           0 :   if (unlikely (res < 0))
     268             :     return ENOMEM;
     269             : 
     270           0 :   int fd = try_kernel_name (dwfl, &archive, false);
     271           0 :   if (fd < 0)
     272           0 :     result = errno ?: ENOENT;
     273             :   else
     274             :     {
     275             :       /* We have the archive file open!  */
     276           0 :       Dwfl_Module *last = __libdwfl_report_offline (dwfl, NULL, archive, fd,
     277             :                                                     true, predicate);
     278           0 :       if (unlikely (last == NULL))
     279             :         result = -1;
     280             :       else
     281             :         {
     282             :           /* Find the kernel and move it to the head of the list.  */
     283           0 :           Dwfl_Module **tailp = &dwfl->modulelist, **prevp = tailp;
     284           0 :           for (Dwfl_Module *m = *prevp; m != NULL; m = *(prevp = &m->next))
     285           0 :             if (!m->gc && m->e_type != ET_REL && !strcmp (m->name, "kernel"))
     286             :               {
     287           0 :                 *prevp = m->next;
     288           0 :                 m->next = *tailp;
     289           0 :                 *tailp = m;
     290           0 :                 break;
     291             :               }
     292             :         }
     293             :     }
     294             : 
     295           0 :   free (archive);
     296           0 :   return result;
     297             : }
     298             : 
     299             : static size_t
     300           0 : check_suffix (const FTSENT *f, size_t namelen)
     301             : {
     302             : #define TRY(sfx)                                                        \
     303             :   if ((namelen ? f->fts_namelen == namelen + sizeof sfx - 1          \
     304             :        : f->fts_namelen >= sizeof sfx)                                    \
     305             :       && !memcmp (f->fts_name + f->fts_namelen - (sizeof sfx - 1),        \
     306             :                   sfx, sizeof sfx))                                     \
     307             :     return sizeof sfx - 1
     308             : 
     309           0 :   TRY (".ko");
     310           0 :   TRY (".ko.gz");
     311             : #if USE_BZLIB
     312           0 :   TRY (".ko.bz2");
     313             : #endif
     314             : #if USE_LZMA
     315           0 :   TRY (".ko.xz");
     316             : #endif
     317             : 
     318           0 :   return 0;
     319             : 
     320             : #undef  TRY
     321             : }
     322             : 
     323             : /* Report a kernel and all its modules found on disk, for offline use.
     324             :    If RELEASE starts with '/', it names a directory to look in;
     325             :    if not, it names a directory to find under /lib/modules/;
     326             :    if null, /lib/modules/`uname -r` is used.
     327             :    Returns zero on success, -1 if dwfl_report_module failed,
     328             :    or an errno code if finding the files on disk failed.  */
     329             : int
     330           0 : dwfl_linux_kernel_report_offline (Dwfl *dwfl, const char *release,
     331             :                                   int (*predicate) (const char *module,
     332             :                                                     const char *file))
     333             : {
     334           0 :   int result = report_kernel_archive (dwfl, &release, predicate);
     335           0 :   if (result != ENOENT)
     336             :     return result;
     337             : 
     338             :   /* First report the kernel.  */
     339           0 :   result = report_kernel (dwfl, &release, predicate);
     340           0 :   if (result == 0)
     341             :     {
     342             :       /* Do "find /lib/modules/RELEASE -name *.ko".  */
     343             : 
     344           0 :       char *modulesdir[] = { NULL, NULL };
     345           0 :       if (release[0] == '/')
     346           0 :         modulesdir[0] = (char *) release;
     347             :       else
     348             :         {
     349           0 :           if (asprintf (&modulesdir[0], MODULEDIRFMT, release) < 0)
     350           0 :             return errno;
     351             :         }
     352             : 
     353           0 :       FTS *fts = fts_open (modulesdir, FTS_NOSTAT | FTS_LOGICAL, NULL);
     354           0 :       if (modulesdir[0] == (char *) release)
     355           0 :         modulesdir[0] = NULL;
     356           0 :       if (fts == NULL)
     357             :         {
     358           0 :           free (modulesdir[0]);
     359           0 :           return errno;
     360             :         }
     361             : 
     362             :       FTSENT *f;
     363           0 :       while ((f = fts_read (fts)) != NULL)
     364             :         {
     365             :           /* Skip a "source" subtree, which tends to be large.
     366             :              This insane hard-coding of names is what depmod does too.  */
     367           0 :           if (f->fts_namelen == sizeof "source" - 1
     368           0 :               && !strcmp (f->fts_name, "source"))
     369             :             {
     370           0 :               fts_set (fts, f, FTS_SKIP);
     371           0 :               continue;
     372             :             }
     373             : 
     374           0 :           switch (f->fts_info)
     375             :             {
     376             :             case FTS_F:
     377             :             case FTS_SL:
     378             :             case FTS_NSOK:;
     379             :               /* See if this file name matches "*.ko".  */
     380           0 :               const size_t suffix = check_suffix (f, 0);
     381           0 :               if (suffix)
     382             :                 {
     383             :                   /* We have a .ko file to report.  Following the algorithm
     384             :                      by which the kernel makefiles set KBUILD_MODNAME, we
     385             :                      replace all ',' or '-' with '_' in the file name and
     386             :                      call that the module name.  Modules could well be
     387             :                      built using different embedded names than their file
     388             :                      names.  To handle that, we would have to look at the
     389             :                      __this_module.name contents in the module's text.  */
     390             : 
     391           0 :                   char *name = strndup (f->fts_name, f->fts_namelen - suffix);
     392           0 :                   if (unlikely (name == NULL))
     393             :                     {
     394           0 :                       __libdwfl_seterrno (DWFL_E_NOMEM);
     395           0 :                       result = -1;
     396           0 :                       break;
     397             :                     }
     398           0 :                   for (size_t i = 0; i < f->fts_namelen - suffix; ++i)
     399           0 :                     if (name[i] == '-' || name[i] == ',')
     400           0 :                       name[i] = '_';
     401             : 
     402           0 :                   if (predicate != NULL)
     403             :                     {
     404             :                       /* Let the predicate decide whether to use this one.  */
     405           0 :                       int want = (*predicate) (name, f->fts_path);
     406           0 :                       if (want < 0)
     407             :                         {
     408           0 :                           result = -1;
     409           0 :                           free (name);
     410           0 :                           break;
     411             :                         }
     412           0 :                       if (!want)
     413             :                         {
     414           0 :                           free (name);
     415           0 :                           continue;
     416             :                         }
     417             :                     }
     418             : 
     419           0 :                   if (dwfl_report_offline (dwfl, name, f->fts_path, -1) == NULL)
     420             :                     {
     421           0 :                       free (name);
     422           0 :                       result = -1;
     423           0 :                       break;
     424             :                     }
     425           0 :                   free (name);
     426             :                 }
     427           0 :               continue;
     428             : 
     429             :             case FTS_ERR:
     430             :             case FTS_DNR:
     431             :             case FTS_NS:
     432           0 :               result = f->fts_errno;
     433           0 :               break;
     434             : 
     435             :             case FTS_SLNONE:
     436             :             default:
     437           0 :               continue;
     438             :             }
     439             : 
     440             :           /* We only get here in error cases.  */
     441             :           break;
     442             :         }
     443           0 :       fts_close (fts);
     444           0 :       free (modulesdir[0]);
     445             :     }
     446             : 
     447             :   return result;
     448             : }
     449             : INTDEF (dwfl_linux_kernel_report_offline)
     450             : 
     451             : 
     452             : /* State of read_address used by intuit_kernel_bounds. */
     453             : struct read_address_state {
     454             :   FILE *f;
     455             :   char *line;
     456             :   size_t linesz;
     457             :   size_t n;
     458             :   char *p;
     459             :   const char *type;
     460             : };
     461             : 
     462             : static inline bool
     463           0 : read_address (struct read_address_state *state, Dwarf_Addr *addr)
     464             : {
     465           0 :   if ((state->n = getline (&state->line, &state->linesz, state->f)) < 1 ||
     466           0 :       state->line[state->n - 2] == ']')
     467             :     return false;
     468           0 :   *addr = strtoull (state->line, &state->p, 16);
     469           0 :   state->p += strspn (state->p, " \t");
     470           0 :   state->type = strsep (&state->p, " \t\n");
     471           0 :   if (state->type == NULL)
     472             :     return false;
     473           0 :   return state->p != NULL && state->p != state->line;
     474             : }
     475             : 
     476             : 
     477             : /* Grovel around to guess the bounds of the runtime kernel image.  */
     478             : static int
     479           0 : intuit_kernel_bounds (Dwarf_Addr *start, Dwarf_Addr *end, Dwarf_Addr *notes)
     480             : {
     481           0 :   struct read_address_state state = { NULL, NULL, 0, 0, NULL, NULL };
     482             : 
     483           0 :   state.f = fopen (KSYMSFILE, "r");
     484           0 :   if (state.f == NULL)
     485           0 :     return errno;
     486             : 
     487           0 :   (void) __fsetlocking (state.f, FSETLOCKING_BYCALLER);
     488             : 
     489           0 :   *notes = 0;
     490             : 
     491             :   int result;
     492             :   do
     493           0 :     result = read_address (&state, start) ? 0 : -1;
     494           0 :   while (result == 0 && strchr ("TtRr", *state.type) == NULL);
     495             : 
     496           0 :   if (result == 0)
     497             :     {
     498           0 :       *end = *start;
     499           0 :       while (read_address (&state, end))
     500           0 :         if (*notes == 0 && !strcmp (state.p, "__start_notes\n"))
     501           0 :           *notes = *end;
     502             : 
     503           0 :       Dwarf_Addr round_kernel = sysconf (_SC_PAGE_SIZE);
     504           0 :       *start &= -(Dwarf_Addr) round_kernel;
     505           0 :       *end += round_kernel - 1;
     506           0 :       *end &= -(Dwarf_Addr) round_kernel;
     507           0 :       if (*start >= *end || *end - *start < round_kernel)
     508           0 :         result = -1;
     509             :     }
     510           0 :   free (state.line);
     511             : 
     512           0 :   if (result == -1)
     513           0 :     result = ferror_unlocked (state.f) ? errno : ENOEXEC;
     514             : 
     515           0 :   fclose (state.f);
     516             : 
     517           0 :   return result;
     518             : }
     519             : 
     520             : 
     521             : /* Look for a build ID note in NOTESFILE and associate the ID with MOD.  */
     522             : static int
     523           0 : check_notes (Dwfl_Module *mod, const char *notesfile,
     524             :              Dwarf_Addr vaddr, const char *secname)
     525             : {
     526           0 :   int fd = open (notesfile, O_RDONLY);
     527           0 :   if (fd < 0)
     528             :     return 1;
     529             : 
     530             :   assert (sizeof (Elf32_Nhdr) == sizeof (GElf_Nhdr));
     531             :   assert (sizeof (Elf64_Nhdr) == sizeof (GElf_Nhdr));
     532             :   union
     533             :   {
     534             :     GElf_Nhdr nhdr;
     535             :     unsigned char data[8192];
     536             :   } buf;
     537             : 
     538           0 :   ssize_t n = read (fd, buf.data, sizeof buf);
     539           0 :   close (fd);
     540             : 
     541           0 :   if (n <= 0)
     542             :     return 1;
     543             : 
     544             :   unsigned char *p = buf.data;
     545           0 :   while (p < &buf.data[n])
     546             :     {
     547             :       /* No translation required since we are reading the native kernel.  */
     548           0 :       GElf_Nhdr *nhdr = (void *) p;
     549           0 :       p += sizeof *nhdr;
     550           0 :       unsigned char *name = p;
     551           0 :       p += (nhdr->n_namesz + 3) & -4U;
     552           0 :       unsigned char *bits = p;
     553           0 :       p += (nhdr->n_descsz + 3) & -4U;
     554             : 
     555           0 :       if (p <= &buf.data[n]
     556           0 :           && nhdr->n_type == NT_GNU_BUILD_ID
     557           0 :           && nhdr->n_namesz == sizeof "GNU"
     558           0 :           && !memcmp (name, "GNU", sizeof "GNU"))
     559             :         {
     560             :           /* Found it.  For a module we must figure out its VADDR now.  */
     561             : 
     562           0 :           if (secname != NULL
     563           0 :               && (INTUSE(dwfl_linux_kernel_module_section_address)
     564           0 :                   (mod, NULL, mod->name, 0, secname, 0, NULL, &vaddr) != 0
     565           0 :                   || vaddr == (GElf_Addr) -1l))
     566           0 :             vaddr = 0;
     567             : 
     568           0 :           if (vaddr != 0)
     569           0 :             vaddr += bits - buf.data;
     570           0 :           return INTUSE(dwfl_module_report_build_id) (mod, bits,
     571           0 :                                                       nhdr->n_descsz, vaddr);
     572             :         }
     573             :     }
     574             : 
     575             :   return 0;
     576             : }
     577             : 
     578             : /* Look for a build ID for the kernel.  */
     579             : static int
     580             : check_kernel_notes (Dwfl_Module *kernelmod, GElf_Addr vaddr)
     581             : {
     582           0 :   return check_notes (kernelmod, KNOTESFILE, vaddr, NULL) < 0 ? -1 : 0;
     583             : }
     584             : 
     585             : /* Look for a build ID for a loaded kernel module.  */
     586             : static int
     587           0 : check_module_notes (Dwfl_Module *mod)
     588             : {
     589           0 :   char *dirs[2] = { NULL, NULL };
     590           0 :   if (asprintf (&dirs[0], MODNOTESFMT, mod->name) < 0)
     591             :     return ENOMEM;
     592             : 
     593           0 :   FTS *fts = fts_open (dirs, FTS_NOSTAT | FTS_LOGICAL, NULL);
     594           0 :   if (fts == NULL)
     595             :     {
     596           0 :       free (dirs[0]);
     597           0 :       return 0;
     598             :     }
     599             : 
     600             :   int result = 0;
     601             :   FTSENT *f;
     602           0 :   while ((f = fts_read (fts)) != NULL)
     603             :     {
     604           0 :       switch (f->fts_info)
     605             :         {
     606             :         case FTS_F:
     607             :         case FTS_SL:
     608             :         case FTS_NSOK:
     609           0 :           result = check_notes (mod, f->fts_accpath, 0, f->fts_name);
     610           0 :           if (result > 0)    /* Nothing found.  */
     611             :             {
     612           0 :               result = 0;
     613           0 :               continue;
     614             :             }
     615             :           break;
     616             : 
     617             :         case FTS_ERR:
     618             :         case FTS_DNR:
     619           0 :           result = f->fts_errno;
     620           0 :           break;
     621             : 
     622             :         case FTS_NS:
     623             :         case FTS_SLNONE:
     624             :         default:
     625           0 :           continue;
     626             :         }
     627             : 
     628             :       /* We only get here when finished or in error cases.  */
     629             :       break;
     630             :     }
     631           0 :   fts_close (fts);
     632           0 :   free (dirs[0]);
     633             : 
     634           0 :   return result;
     635             : }
     636             : 
     637             : int
     638           0 : dwfl_linux_kernel_report_kernel (Dwfl *dwfl)
     639             : {
     640           0 :   Dwarf_Addr start = 0;
     641           0 :   Dwarf_Addr end = 0;
     642             : 
     643             :   #define report() \
     644             :     (INTUSE(dwfl_report_module) (dwfl, KERNEL_MODNAME, start, end))
     645             : 
     646             :   /* This is a bit of a kludge.  If we already reported the kernel,
     647             :      don't bother figuring it out again--it never changes.  */
     648           0 :   for (Dwfl_Module *m = dwfl->modulelist; m != NULL; m = m->next)
     649           0 :     if (!strcmp (m->name, KERNEL_MODNAME))
     650             :       {
     651           0 :         start = m->low_addr;
     652           0 :         end = m->high_addr;
     653           0 :         return report () == NULL ? -1 : 0;
     654             :       }
     655             : 
     656             :   /* Try to figure out the bounds of the kernel image without
     657             :      looking for any vmlinux file.  */
     658             :   Dwarf_Addr notes;
     659             :   /* The compiler cannot deduce that if intuit_kernel_bounds returns
     660             :      zero NOTES will be initialized.  Fake the initialization.  */
     661           0 :   asm ("" : "=m" (notes));
     662           0 :   int result = intuit_kernel_bounds (&start, &end, &notes);
     663           0 :   if (result == 0)
     664             :     {
     665           0 :       Dwfl_Module *mod = report ();
     666           0 :       return unlikely (mod == NULL) ? -1 : check_kernel_notes (mod, notes);
     667             :     }
     668           0 :   if (result != ENOENT)
     669             :     return result;
     670             : 
     671             :   /* Find the ELF file for the running kernel and dwfl_report_elf it.  */
     672           0 :   return report_kernel (dwfl, NULL, NULL);
     673             : }
     674             : INTDEF (dwfl_linux_kernel_report_kernel)
     675             : 
     676             : 
     677             : static inline bool
     678           0 : subst_name (char from, char to,
     679             :             const char * const module_name,
     680             :             char * const alternate_name,
     681             :             const size_t namelen)
     682             : {
     683           0 :   const char *n = memchr (module_name, from, namelen);
     684           0 :   if (n == NULL)
     685             :     return false;
     686           0 :   char *a = mempcpy (alternate_name, module_name, n - module_name);
     687           0 :   *a++ = to;
     688           0 :   ++n;
     689             :   const char *p;
     690           0 :   while ((p = memchr (n, from, namelen - (n - module_name))) != NULL)
     691             :     {
     692           0 :       a = mempcpy (a, n, p - n);
     693           0 :       *a++ = to;
     694           0 :       n = p + 1;
     695             :     }
     696           0 :   memcpy (a, n, namelen - (n - module_name) + 1);
     697           0 :   return true;
     698             : }
     699             : 
     700             : /* Dwfl_Callbacks.find_elf for the running Linux kernel and its modules.  */
     701             : 
     702             : int
     703           0 : dwfl_linux_kernel_find_elf (Dwfl_Module *mod,
     704             :                             void **userdata __attribute__ ((unused)),
     705             :                             const char *module_name,
     706             :                             Dwarf_Addr base __attribute__ ((unused)),
     707             :                             char **file_name, Elf **elfp)
     708             : {
     709           0 :   if (mod->build_id_len > 0)
     710             :     {
     711           0 :       int fd = INTUSE(dwfl_build_id_find_elf) (mod, NULL, NULL, 0,
     712             :                                                file_name, elfp);
     713           0 :       if (fd >= 0 || mod->main.elf != NULL || errno != 0)
     714             :         return fd;
     715             :     }
     716             : 
     717           0 :   const char *release = kernel_release ();
     718           0 :   if (release == NULL)
     719           0 :     return errno;
     720             : 
     721           0 :   if (!strcmp (module_name, KERNEL_MODNAME))
     722           0 :     return find_kernel_elf (mod->dwfl, release, file_name);
     723             : 
     724             :   /* Do "find /lib/modules/`uname -r` -name MODULE_NAME.ko".  */
     725             : 
     726           0 :   char *modulesdir[] = { NULL, NULL };
     727           0 :   if (asprintf (&modulesdir[0], MODULEDIRFMT, release) < 0)
     728             :     return -1;
     729             : 
     730           0 :   FTS *fts = fts_open (modulesdir, FTS_NOSTAT | FTS_LOGICAL, NULL);
     731           0 :   if (fts == NULL)
     732             :     {
     733           0 :       free (modulesdir[0]);
     734           0 :       return -1;
     735             :     }
     736             : 
     737           0 :   size_t namelen = strlen (module_name);
     738             : 
     739             :   /* This is a kludge.  There is no actual necessary relationship between
     740             :      the name of the .ko file installed and the module name the kernel
     741             :      knows it by when it's loaded.  The kernel's only idea of the module
     742             :      name comes from the name embedded in the object's magic
     743             :      .gnu.linkonce.this_module section.
     744             : 
     745             :      In practice, these module names match the .ko file names except for
     746             :      some using '_' and some using '-'.  So our cheap kludge is to look for
     747             :      two files when either a '_' or '-' appears in a module name, one using
     748             :      only '_' and one only using '-'.  */
     749             : 
     750           0 :   char *alternate_name = malloc (namelen + 1);
     751           0 :   if (unlikely (alternate_name == NULL))
     752             :     {
     753           0 :       free (modulesdir[0]);
     754           0 :       return ENOMEM;
     755             :     }
     756           0 :   if (!subst_name ('-', '_', module_name, alternate_name, namelen) &&
     757           0 :       !subst_name ('_', '-', module_name, alternate_name, namelen))
     758           0 :     alternate_name[0] = '\0';
     759             : 
     760             :   FTSENT *f;
     761             :   int error = ENOENT;
     762           0 :   while ((f = fts_read (fts)) != NULL)
     763             :     {
     764             :       /* Skip a "source" subtree, which tends to be large.
     765             :          This insane hard-coding of names is what depmod does too.  */
     766           0 :       if (f->fts_namelen == sizeof "source" - 1
     767           0 :           && !strcmp (f->fts_name, "source"))
     768             :         {
     769           0 :           fts_set (fts, f, FTS_SKIP);
     770           0 :           continue;
     771             :         }
     772             : 
     773           0 :       error = ENOENT;
     774           0 :       switch (f->fts_info)
     775             :         {
     776             :         case FTS_F:
     777             :         case FTS_SL:
     778             :         case FTS_NSOK:
     779             :           /* See if this file name is "MODULE_NAME.ko".  */
     780           0 :           if (check_suffix (f, namelen)
     781           0 :               && (!memcmp (f->fts_name, module_name, namelen)
     782           0 :                   || !memcmp (f->fts_name, alternate_name, namelen)))
     783             :             {
     784           0 :               int fd = open (f->fts_accpath, O_RDONLY);
     785           0 :               *file_name = strdup (f->fts_path);
     786           0 :               fts_close (fts);
     787           0 :               free (modulesdir[0]);
     788           0 :               free (alternate_name);
     789           0 :               if (fd < 0)
     790           0 :                 free (*file_name);
     791           0 :               else if (*file_name == NULL)
     792             :                 {
     793           0 :                   close (fd);
     794           0 :                   fd = -1;
     795             :                 }
     796             :               return fd;
     797             :             }
     798             :           break;
     799             : 
     800             :         case FTS_ERR:
     801             :         case FTS_DNR:
     802             :         case FTS_NS:
     803           0 :           error = f->fts_errno;
     804           0 :           break;
     805             : 
     806             :         case FTS_SLNONE:
     807             :         default:
     808             :           break;
     809             :         }
     810             :     }
     811             : 
     812           0 :   fts_close (fts);
     813           0 :   free (modulesdir[0]);
     814           0 :   free (alternate_name);
     815           0 :   errno = error;
     816           0 :   return -1;
     817             : }
     818             : INTDEF (dwfl_linux_kernel_find_elf)
     819             : 
     820             : 
     821             : /* Dwfl_Callbacks.section_address for kernel modules in the running Linux.
     822             :    We read the information from /sys/module directly.  */
     823             : 
     824             : int
     825           0 : dwfl_linux_kernel_module_section_address
     826             : (Dwfl_Module *mod __attribute__ ((unused)),
     827             :  void **userdata __attribute__ ((unused)),
     828             :  const char *modname, Dwarf_Addr base __attribute__ ((unused)),
     829             :  const char *secname, Elf32_Word shndx __attribute__ ((unused)),
     830             :  const GElf_Shdr *shdr __attribute__ ((unused)),
     831             :  Dwarf_Addr *addr)
     832             : {
     833             :   char *sysfile;
     834           0 :   if (asprintf (&sysfile, SECADDRDIRFMT "%s", modname, secname) < 0)
     835             :     return DWARF_CB_ABORT;
     836             : 
     837           0 :   FILE *f = fopen (sysfile, "r");
     838           0 :   free (sysfile);
     839             : 
     840           0 :   if (f == NULL)
     841             :     {
     842           0 :       if (errno == ENOENT)
     843             :         {
     844             :           /* The .modinfo and .data.percpu sections are never kept
     845             :              loaded in the kernel.  If the kernel was compiled without
     846             :              CONFIG_MODULE_UNLOAD, the .exit.* sections are not
     847             :              actually loaded at all.
     848             : 
     849             :              Setting *ADDR to -1 tells the caller this section is
     850             :              actually absent from memory.  */
     851             : 
     852           0 :           if (!strcmp (secname, ".modinfo")
     853           0 :               || !strcmp (secname, ".data.percpu")
     854           0 :               || !strncmp (secname, ".exit", 5))
     855             :             {
     856           0 :               *addr = (Dwarf_Addr) -1l;
     857           0 :               return DWARF_CB_OK;
     858             :             }
     859             : 
     860             :           /* The goofy PPC64 module_frob_arch_sections function tweaks
     861             :              the section names as a way to control other kernel code's
     862             :              behavior, and this cruft leaks out into the /sys information.
     863             :              The file name for ".init*" may actually look like "_init*".  */
     864             : 
     865           0 :           const bool is_init = !strncmp (secname, ".init", 5);
     866           0 :           if (is_init)
     867             :             {
     868           0 :               if (asprintf (&sysfile, SECADDRDIRFMT "_%s",
     869             :                             modname, &secname[1]) < 0)
     870             :                 return ENOMEM;
     871           0 :               f = fopen (sysfile, "r");
     872           0 :               free (sysfile);
     873           0 :               if (f != NULL)
     874             :                 goto ok;
     875             :             }
     876             : 
     877             :           /* The kernel truncates section names to MODULE_SECT_NAME_LEN - 1.
     878             :              In case that size increases in the future, look for longer
     879             :              truncated names first.  */
     880           0 :           size_t namelen = strlen (secname);
     881           0 :           if (namelen >= MODULE_SECT_NAME_LEN)
     882             :             {
     883           0 :               int len = asprintf (&sysfile, SECADDRDIRFMT "%s",
     884             :                                   modname, secname);
     885           0 :               if (len < 0)
     886             :                 return DWARF_CB_ABORT;
     887           0 :               char *end = sysfile + len;
     888             :               do
     889             :                 {
     890           0 :                   *--end = '\0';
     891           0 :                   f = fopen (sysfile, "r");
     892           0 :                   if (is_init && f == NULL && errno == ENOENT)
     893             :                     {
     894           0 :                       sysfile[len - namelen] = '_';
     895           0 :                       f = fopen (sysfile, "r");
     896           0 :                       sysfile[len - namelen] = '.';
     897             :                     }
     898             :                 }
     899           0 :               while (f == NULL && errno == ENOENT
     900           0 :                      && end - &sysfile[len - namelen] >= MODULE_SECT_NAME_LEN);
     901           0 :               free (sysfile);
     902             : 
     903           0 :               if (f != NULL)
     904             :                 goto ok;
     905             :             }
     906             :         }
     907             : 
     908             :       return DWARF_CB_ABORT;
     909             :     }
     910             : 
     911             :  ok:
     912           0 :   (void) __fsetlocking (f, FSETLOCKING_BYCALLER);
     913             : 
     914           0 :   int result = (fscanf (f, "%" PRIx64 "\n", addr) == 1 ? 0
     915           0 :                 : ferror_unlocked (f) ? errno : ENOEXEC);
     916           0 :   fclose (f);
     917             : 
     918           0 :   if (result == 0)
     919             :     return DWARF_CB_OK;
     920             : 
     921           0 :   errno = result;
     922           0 :   return DWARF_CB_ABORT;
     923             : }
     924             : INTDEF (dwfl_linux_kernel_module_section_address)
     925             : 
     926             : int
     927           0 : dwfl_linux_kernel_report_modules (Dwfl *dwfl)
     928             : {
     929           0 :   FILE *f = fopen (MODULELIST, "r");
     930           0 :   if (f == NULL)
     931           0 :     return errno;
     932             : 
     933           0 :   (void) __fsetlocking (f, FSETLOCKING_BYCALLER);
     934             : 
     935           0 :   int result = 0;
     936             :   Dwarf_Addr modaddr;
     937             :   unsigned long int modsz;
     938             :   char modname[128];
     939           0 :   char *line = NULL;
     940           0 :   size_t linesz = 0;
     941             :   /* We can't just use fscanf here because it's not easy to distinguish \n
     942             :      from other whitespace so as to take the optional word following the
     943             :      address but always stop at the end of the line.  */
     944           0 :   while (getline (&line, &linesz, f) > 0
     945           0 :          && sscanf (line, "%128s %lu %*s %*s %*s %" PRIx64 " %*s\n",
     946             :                     modname, &modsz, &modaddr) == 3)
     947             :     {
     948           0 :       Dwfl_Module *mod = INTUSE(dwfl_report_module) (dwfl, modname,
     949             :                                                      modaddr, modaddr + modsz);
     950           0 :       if (mod == NULL)
     951             :         {
     952             :           result = -1;
     953             :           break;
     954             :         }
     955             : 
     956           0 :       result = check_module_notes (mod);
     957             :     }
     958           0 :   free (line);
     959             : 
     960           0 :   if (result == 0)
     961           0 :     result = ferror_unlocked (f) ? errno : feof_unlocked (f) ? 0 : ENOEXEC;
     962             : 
     963           0 :   fclose (f);
     964             : 
     965           0 :   return result;
     966             : }
     967             : INTDEF (dwfl_linux_kernel_report_modules)

Generated by: LCOV version 1.12