LCOV - code coverage report
Current view: top level - libdwfl - dwfl_report_elf.c (source / functions) Hit Total Coverage
Test: elfutils- Lines: 99 145 68.3 %
Date: 2020-06-11 18:20:19 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Report a module to libdwfl based on ELF program headers.
       2             :    Copyright (C) 2005-2010 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             : #ifdef HAVE_CONFIG_H
      30             : # include <config.h>
      31             : #endif
      32             : 
      33             : #include "libdwflP.h"
      34             : #include <fcntl.h>
      35             : #include <unistd.h>
      36             : 
      37             : 
      38             : /* We start every ET_REL module at a moderately aligned boundary.
      39             :    This keeps the low addresses easy to read compared to a layout
      40             :    starting at 0 (as when using -e).  It also makes it unlikely
      41             :    that a middle section will have a larger alignment and require
      42             :    rejiggering (see below).  */
      43             : #define REL_MIN_ALIGN   ((GElf_Xword) 0x100)
      44             : 
      45             : bool
      46             : internal_function
      47         604 : __libdwfl_elf_address_range (Elf *elf, GElf_Addr base, bool add_p_vaddr,
      48             :                              bool sanity, GElf_Addr *vaddrp,
      49             :                              GElf_Addr *address_syncp, GElf_Addr *startp,
      50             :                              GElf_Addr *endp, GElf_Addr *biasp,
      51             :                              GElf_Half *e_typep)
      52             : {
      53         604 :   GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem);
      54         604 :   if (ehdr == NULL)
      55             :     {
      56           0 :     elf_error:
      57           0 :       __libdwfl_seterrno (DWFL_E_LIBELF);
      58           0 :       return false;
      59             :     }
      60             : 
      61         604 :   GElf_Addr vaddr = 0;
      62         604 :   GElf_Addr address_sync = 0;
      63         604 :   GElf_Addr start = 0, end = 0, bias = 0;
      64         604 :   switch (ehdr->e_type)
      65             :     {
      66         169 :     case ET_REL:
      67             :       /* For a relocatable object, we do an arbitrary section layout.
      68             :          By updating the section header in place, we leave the layout
      69             :          information to be found by relocation.  */
      70             : 
      71         169 :       start = end = base = (base + REL_MIN_ALIGN - 1) & -REL_MIN_ALIGN;
      72             : 
      73         169 :       bool first = true;
      74         169 :       Elf_Scn *scn = NULL;
      75      790301 :       while ((scn = elf_nextscn (elf, scn)) != NULL)
      76      790132 :         {
      77      790132 :           GElf_Shdr shdr_mem;
      78      790132 :           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
      79      790132 :           if (unlikely (shdr == NULL))
      80             :             goto elf_error;
      81             : 
      82      790132 :           if (shdr->sh_flags & SHF_ALLOC)
      83             :             {
      84        1236 :               const GElf_Xword align = shdr->sh_addralign ?: 1;
      85        1236 :               const GElf_Addr next = (end + align - 1) & -align;
      86        1236 :               if (shdr->sh_addr == 0
      87             :                   /* Once we've started doing layout we have to do it all,
      88             :                      unless we just layed out the first section at 0 when
      89             :                      it already was at 0.  */
      90          15 :                   || (bias == 0 && end > start && end != next))
      91             :                 {
      92        1221 :                   shdr->sh_addr = next;
      93        1221 :                   if (end == base)
      94             :                     /* This is the first section assigned a location.
      95             :                        Use its aligned address as the module's base.  */
      96             :                     start = base = shdr->sh_addr;
      97         981 :                   else if (unlikely (base & (align - 1)))
      98             :                     {
      99             :                       /* If BASE has less than the maximum alignment of
     100             :                          any section, we eat more than the optimal amount
     101             :                          of padding and so make the module's apparent
     102             :                          size come out larger than it would when placed
     103             :                          at zero.  So reset the layout with a better base.  */
     104             : 
     105           0 :                       start = end = base = (base + align - 1) & -align;
     106           0 :                       Elf_Scn *prev_scn = NULL;
     107           0 :                       do
     108             :                         {
     109           0 :                           prev_scn = elf_nextscn (elf, prev_scn);
     110           0 :                           GElf_Shdr prev_shdr_mem;
     111           0 :                           GElf_Shdr *prev_shdr = gelf_getshdr (prev_scn,
     112             :                                                                &prev_shdr_mem);
     113           0 :                           if (unlikely (prev_shdr == NULL))
     114           0 :                             goto elf_error;
     115           0 :                           if (prev_shdr->sh_flags & SHF_ALLOC)
     116             :                             {
     117           0 :                               const GElf_Xword prev_align
     118           0 :                                 = prev_shdr->sh_addralign ?: 1;
     119             : 
     120           0 :                               prev_shdr->sh_addr
     121           0 :                                 = (end + prev_align - 1) & -prev_align;
     122           0 :                               end = prev_shdr->sh_addr + prev_shdr->sh_size;
     123             : 
     124           0 :                               if (unlikely (! gelf_update_shdr (prev_scn,
     125             :                                                                 prev_shdr)))
     126             :                                 goto elf_error;
     127             :                             }
     128             :                         }
     129           0 :                       while (prev_scn != scn);
     130           0 :                       continue;
     131             :                     }
     132             : 
     133        1221 :                   end = shdr->sh_addr + shdr->sh_size;
     134        1221 :                   if (likely (shdr->sh_addr != 0)
     135         981 :                       && unlikely (! gelf_update_shdr (scn, shdr)))
     136             :                     goto elf_error;
     137             :                 }
     138             :               else
     139             :                 {
     140             :                   /* The address is already assigned.  Just track it.  */
     141          15 :                   if (first || end < shdr->sh_addr + shdr->sh_size)
     142           3 :                     end = shdr->sh_addr + shdr->sh_size;
     143          15 :                   if (first || bias > shdr->sh_addr)
     144             :                     /* This is the lowest address in the module.  */
     145           0 :                     bias = shdr->sh_addr;
     146             : 
     147          15 :                   if ((shdr->sh_addr - bias + base) & (align - 1))
     148             :                     /* This section winds up misaligned using BASE.
     149             :                        Adjust BASE upwards to make it congruent to
     150             :                        the lowest section address in the file modulo ALIGN.  */
     151           0 :                     base = (((base + align - 1) & -align)
     152           0 :                             + (bias & (align - 1)));
     153             :                 }
     154             : 
     155             :               first = false;
     156             :             }
     157             :         }
     158             : 
     159         169 :       if (bias != 0)
     160             :         {
     161             :           /* The section headers had nonzero sh_addr values.  The layout
     162             :              was already done.  We've just collected the total span.
     163             :              Now just compute the bias from the requested base.  */
     164           0 :           start = base;
     165           0 :           end = end - bias + start;
     166           0 :           bias = start - bias;
     167             :         }
     168         604 :       break;
     169             : 
     170             :       /* Everything else has to have program headers.  */
     171             : 
     172         270 :     case ET_EXEC:
     173             :     case ET_CORE:
     174             :       /* An assigned base address is meaningless for these.  */
     175         270 :       base = 0;
     176         270 :       add_p_vaddr = true;
     177         435 :       FALLTHROUGH;
     178         435 :     case ET_DYN:
     179         435 :     default:;
     180         435 :       size_t phnum;
     181         435 :       if (unlikely (elf_getphdrnum (elf, &phnum) != 0))
     182             :         goto elf_error;
     183        1105 :       for (size_t i = 0; i < phnum; ++i)
     184             :         {
     185        1104 :           GElf_Phdr phdr_mem, *ph = gelf_getphdr (elf, i, &phdr_mem);
     186        1104 :           if (unlikely (ph == NULL))
     187           0 :             goto elf_error;
     188        1104 :           if (ph->p_type == PT_LOAD)
     189             :             {
     190         434 :               vaddr = ph->p_vaddr & -ph->p_align;
     191         434 :               address_sync = ph->p_vaddr + ph->p_memsz;
     192         434 :               break;
     193             :             }
     194             :         }
     195         435 :       if (add_p_vaddr)
     196             :         {
     197         431 :           start = base + vaddr;
     198         431 :           bias = base;
     199             :         }
     200             :       else
     201             :         {
     202           4 :           start = base;
     203           4 :           bias = base - vaddr;
     204             :         }
     205             : 
     206        2197 :       for (size_t i = phnum; i-- > 0;)
     207             :         {
     208        2196 :           GElf_Phdr phdr_mem, *ph = gelf_getphdr (elf, i, &phdr_mem);
     209        2196 :           if (unlikely (ph == NULL))
     210           0 :             goto elf_error;
     211        2196 :           if (ph->p_type == PT_LOAD
     212         434 :               && ph->p_vaddr + ph->p_memsz > 0)
     213             :             {
     214         434 :               end = bias + (ph->p_vaddr + ph->p_memsz);
     215         434 :               break;
     216             :             }
     217             :         }
     218             : 
     219         435 :       if (end == 0 && sanity)
     220             :         {
     221           0 :           __libdwfl_seterrno (DWFL_E_NO_PHDR);
     222           0 :           return false;
     223             :         }
     224             :       break;
     225             :     }
     226         604 :   if (vaddrp)
     227         566 :     *vaddrp = vaddr;
     228         604 :   if (address_syncp)
     229         566 :     *address_syncp = address_sync;
     230         604 :   if (startp)
     231         604 :     *startp = start;
     232         604 :   if (endp)
     233         604 :     *endp = end;
     234         604 :   if (biasp)
     235         566 :     *biasp = bias;
     236         604 :   if (e_typep)
     237         566 :     *e_typep = ehdr->e_type;
     238             :   return true;
     239             : }
     240             : 
     241             : Dwfl_Module *
     242             : internal_function
     243         566 : __libdwfl_report_elf (Dwfl *dwfl, const char *name, const char *file_name,
     244             :                       int fd, Elf *elf, GElf_Addr base, bool add_p_vaddr,
     245             :                       bool sanity)
     246             : {
     247         566 :   GElf_Addr vaddr, address_sync, start, end, bias;
     248         566 :   GElf_Half e_type;
     249         566 :   if (! __libdwfl_elf_address_range (elf, base, add_p_vaddr, sanity, &vaddr,
     250             :                                      &address_sync, &start, &end, &bias,
     251             :                                      &e_type))
     252             :     return NULL;
     253         566 :   Dwfl_Module *m = INTUSE(dwfl_report_module) (dwfl, name, start, end);
     254         566 :   if (m != NULL)
     255             :     {
     256         566 :       if (m->main.name == NULL)
     257             :         {
     258         566 :           m->main.name = strdup (file_name);
     259         566 :           m->main.fd = fd;
     260             :         }
     261           0 :       else if ((fd >= 0 && m->main.fd != fd)
     262           0 :                || strcmp (m->main.name, file_name))
     263             :         {
     264           0 :         overlap:
     265           0 :           m->gc = true;
     266           0 :           __libdwfl_seterrno (DWFL_E_OVERLAP);
     267           0 :           return NULL;
     268             :         }
     269             : 
     270             :       /* Preinstall the open ELF handle for the module.  */
     271         566 :       if (m->main.elf == NULL)
     272             :         {
     273         566 :           m->main.elf = elf;
     274         566 :           m->main.vaddr = vaddr;
     275         566 :           m->main.address_sync = address_sync;
     276         566 :           m->main_bias = bias;
     277         566 :           m->e_type = e_type;
     278             :         }
     279             :       else
     280             :         {
     281           0 :           elf_end (elf);
     282           0 :           if (m->main_bias != bias
     283           0 :               || m->main.vaddr != vaddr || m->main.address_sync != address_sync)
     284             :             goto overlap;
     285             :         }
     286             :     }
     287             :   return m;
     288             : }
     289             : 
     290             : Dwfl_Module *
     291           4 : dwfl_report_elf (Dwfl *dwfl, const char *name, const char *file_name, int fd,
     292             :                  GElf_Addr base, bool add_p_vaddr)
     293             : {
     294           4 :   bool closefd = false;
     295           4 :   if (fd < 0)
     296             :     {
     297           4 :       closefd = true;
     298           4 :       fd = open (file_name, O_RDONLY);
     299           4 :       if (fd < 0)
     300             :         {
     301           0 :           __libdwfl_seterrno (DWFL_E_ERRNO);
     302           0 :           return NULL;
     303             :         }
     304             :     }
     305             : 
     306           4 :   Elf *elf;
     307           4 :   Dwfl_Error error = __libdw_open_file (&fd, &elf, closefd, false);
     308           4 :   if (error != DWFL_E_NOERROR)
     309             :     {
     310           0 :       __libdwfl_seterrno (error);
     311           0 :       return NULL;
     312             :     }
     313             : 
     314           4 :   Dwfl_Module *mod = __libdwfl_report_elf (dwfl, name, file_name,
     315             :                                            fd, elf, base, add_p_vaddr, true);
     316           4 :   if (mod == NULL)
     317             :     {
     318           0 :       elf_end (elf);
     319           0 :       if (closefd)
     320           0 :         close (fd);
     321             :     }
     322             : 
     323             :   return mod;
     324             : }
     325             : INTDEF (dwfl_report_elf)
     326             : NEW_VERSION (dwfl_report_elf, ELFUTILS_0.156)
     327             : 
     328             : #ifdef SYMBOL_VERSIONING
     329             : Dwfl_Module *
     330             :   _compat_without_add_p_vaddr_dwfl_report_elf (Dwfl *dwfl, const char *name,
     331             :                                                const char *file_name, int fd,
     332             :                                                GElf_Addr base);
     333             : COMPAT_VERSION_NEWPROTO (dwfl_report_elf, ELFUTILS_0.122, without_add_p_vaddr)
     334             : 
     335             : Dwfl_Module *
     336             : _compat_without_add_p_vaddr_dwfl_report_elf (Dwfl *dwfl, const char *name,
     337             :                                              const char *file_name, int fd,
     338             :                                              GElf_Addr base)
     339             : {
     340             :   return dwfl_report_elf (dwfl, name, file_name, fd, base, true);
     341             : }
     342             : #endif

Generated by: LCOV version 1.13