LCOV - code coverage report
Current view: top level - libdwfl - dwfl_report_elf.c (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 103 148 69.6 %
Date: 2024-03-01 16:42:08 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 71 115 61.7 %

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

Generated by: LCOV version 1.14