LCOV - code coverage report
Current view: top level - libelf - elf_compress_gnu.c (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 66 76 86.8 %
Date: 2024-07-23 19:52:03 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 31 46 67.4 %

           Branch data     Line data    Source code
       1                 :            : /* Compress or decompress a section.
       2                 :            :    Copyright (C) 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                 :            : #ifdef HAVE_CONFIG_H
      30                 :            : # include <config.h>
      31                 :            : #endif
      32                 :            : 
      33                 :            : #include <libelf.h>
      34                 :            : #include "libelfP.h"
      35                 :            : #include "common.h"
      36                 :            : 
      37                 :            : int
      38                 :       1450 : elf_compress_gnu (Elf_Scn *scn, int inflate, unsigned int flags)
      39                 :            : {
      40         [ -  + ]:       1450 :   if (scn == NULL)
      41                 :            :     return -1;
      42                 :            : 
      43         [ -  + ]:       1450 :   if ((flags & ~ELF_CHF_FORCE) != 0)
      44                 :            :     {
      45                 :          0 :       __libelf_seterrno (ELF_E_INVALID_OPERAND);
      46                 :          0 :       return -1;
      47                 :            :     }
      48                 :            : 
      49                 :       1450 :   bool force = (flags & ELF_CHF_FORCE) != 0;
      50                 :            : 
      51                 :       1450 :   Elf *elf = scn->elf;
      52                 :       1450 :   GElf_Ehdr ehdr;
      53         [ -  + ]:       1450 :   if (gelf_getehdr (elf, &ehdr) == NULL)
      54                 :            :     return -1;
      55                 :            : 
      56                 :       1450 :   int elfclass = elf->class;
      57                 :       1450 :   int elfdata = ehdr.e_ident[EI_DATA];
      58                 :            : 
      59                 :       1450 :   Elf64_Xword sh_flags;
      60                 :       1450 :   Elf64_Word sh_type;
      61                 :       1450 :   Elf64_Xword sh_addralign;
      62         [ +  + ]:       1450 :   if (elfclass == ELFCLASS32)
      63                 :            :     {
      64                 :        572 :       Elf32_Shdr *shdr = elf32_getshdr (scn);
      65         [ -  + ]:        572 :       if (shdr == NULL)
      66                 :            :         return -1;
      67                 :            : 
      68                 :        572 :       sh_flags = shdr->sh_flags;
      69                 :        572 :       sh_type = shdr->sh_type;
      70                 :        572 :       sh_addralign = shdr->sh_addralign;
      71                 :            :     }
      72                 :            :   else
      73                 :            :     {
      74                 :        878 :       Elf64_Shdr *shdr = elf64_getshdr (scn);
      75         [ -  + ]:        878 :       if (shdr == NULL)
      76                 :            :         return -1;
      77                 :            : 
      78                 :        878 :       sh_flags = shdr->sh_flags;
      79                 :        878 :       sh_type = shdr->sh_type;
      80                 :        878 :       sh_addralign = shdr->sh_addralign;
      81                 :            :     }
      82                 :            : 
      83                 :            :   /* Allocated sections, or sections that are already are compressed
      84                 :            :      cannot (also) be GNU compressed.  */
      85         [ -  + ]:       1450 :   if ((sh_flags & SHF_ALLOC) != 0 || (sh_flags & SHF_COMPRESSED))
      86                 :            :     {
      87                 :          0 :       __libelf_seterrno (ELF_E_INVALID_SECTION_FLAGS);
      88                 :          0 :       return -1;
      89                 :            :     }
      90                 :            : 
      91         [ -  + ]:       1450 :   if (sh_type == SHT_NULL || sh_type == SHT_NOBITS)
      92                 :            :     {
      93                 :          0 :       __libelf_seterrno (ELF_E_INVALID_SECTION_TYPE);
      94                 :          0 :       return -1;
      95                 :            :     }
      96                 :            : 
      97                 :            :   /* For GNU compression we cannot really know whether the section is
      98                 :            :      already compressed or not.  Just try and see what happens...  */
      99                 :            :   // int compressed = (sh_flags & SHF_COMPRESSED);
     100         [ +  + ]:       1450 :   if (inflate == 1)
     101                 :            :     {
     102                 :        834 :       size_t hsize = 4 + 8; /* GNU "ZLIB" + 8 byte size.  */
     103                 :        834 :       size_t orig_size, new_size, orig_addralign;
     104                 :        834 :       void *out_buf = __libelf_compress (scn, hsize, elfdata,
     105                 :            :                                          &orig_size, &orig_addralign,
     106                 :            :                                          &new_size, force,
     107                 :            :                                          /* use_zstd */ false);
     108                 :            : 
     109                 :            :       /* Compression would make section larger, don't change anything.  */
     110         [ +  + ]:        834 :       if (out_buf == (void *) -1)
     111                 :            :         return 0;
     112                 :            : 
     113                 :            :       /* Compression failed, return error.  */
     114         [ +  - ]:        728 :       if (out_buf == NULL)
     115                 :            :         return -1;
     116                 :            : 
     117         [ +  + ]:        728 :       uint64_t be64_size = htobe64 (orig_size);
     118         [ +  + ]:        728 :       memmove (out_buf, "ZLIB", 4);
     119                 :        728 :       memmove (out_buf + 4, &be64_size, sizeof (be64_size));
     120                 :            : 
     121                 :            :       /* We don't know anything about sh_entsize, sh_addralign and
     122                 :            :          sh_flags won't have a SHF_COMPRESSED hint in the GNU format.
     123                 :            :          Just adjust the sh_size.  */
     124         [ +  + ]:        728 :       if (elfclass == ELFCLASS32)
     125                 :            :         {
     126                 :        320 :           Elf32_Shdr *shdr = elf32_getshdr (scn);
     127                 :        320 :           shdr->sh_size = new_size;
     128                 :            :         }
     129                 :            :       else
     130                 :            :         {
     131                 :        408 :           Elf64_Shdr *shdr = elf64_getshdr (scn);
     132                 :        408 :           shdr->sh_size = new_size;
     133                 :            :         }
     134                 :            : 
     135                 :        728 :       __libelf_reset_rawdata (scn, out_buf, new_size, 1, ELF_T_BYTE);
     136                 :            : 
     137                 :            :       /* The section is now compressed, we could keep the uncompressed
     138                 :            :          data around, but since that might have been multiple Elf_Data
     139                 :            :          buffers let the user uncompress it explicitly again if they
     140                 :            :          want it to simplify bookkeeping.  */
     141                 :        728 :       scn->zdata_base = NULL;
     142                 :            : 
     143                 :        728 :       return 1;
     144                 :            :     }
     145         [ +  - ]:        616 :   else if (inflate == 0)
     146                 :            :     {
     147                 :            :       /* In theory the user could have constructed a compressed section
     148                 :            :          by hand.  And in practice they do. For example when copying
     149                 :            :          a section from one file to another using elf_newdata. So we
     150                 :            :          have to use elf_getdata (not elf_rawdata).  */
     151                 :        616 :       Elf_Data *data = elf_getdata (scn, NULL);
     152         [ -  + ]:        616 :       if (data == NULL)
     153                 :            :         return -1;
     154                 :            : 
     155                 :        616 :       size_t hsize = 4 + 8; /* GNU "ZLIB" + 8 byte size.  */
     156   [ +  -  +  + ]:        616 :       if (data->d_size < hsize || memcmp (data->d_buf, "ZLIB", 4) != 0)
     157                 :            :         {
     158                 :         24 :           __libelf_seterrno (ELF_E_NOT_COMPRESSED);
     159                 :         24 :           return -1;
     160                 :            :         }
     161                 :            : 
     162                 :            :       /* There is a 12-byte header of "ZLIB" followed by
     163                 :            :          an 8-byte big-endian size.  There is only one type and
     164                 :            :          Alignment isn't preserved separately.  */
     165                 :        592 :       uint64_t gsize;
     166         [ -  + ]:        592 :       memcpy (&gsize, data->d_buf + 4, sizeof gsize);
     167         [ -  + ]:        592 :       gsize = be64toh (gsize);
     168                 :            : 
     169                 :            :       /* One more sanity check, size should be bigger than original
     170                 :            :          data size plus some overhead (4 chars ZLIB + 8 bytes size + 6
     171                 :            :          bytes zlib stream overhead + 5 bytes overhead max for one 16K
     172                 :            :          block) and should fit into a size_t.  */
     173         [ -  + ]:        592 :       if (gsize + 4 + 8 + 6 + 5 < data->d_size || gsize > SIZE_MAX)
     174                 :            :         {
     175                 :          0 :           __libelf_seterrno (ELF_E_NOT_COMPRESSED);
     176                 :          0 :           return -1;
     177                 :            :         }
     178                 :            : 
     179                 :        592 :       size_t size = gsize;
     180                 :        592 :       size_t size_in = data->d_size - hsize;
     181                 :        592 :       void *buf_in = data->d_buf + hsize;
     182                 :        592 :       void *buf_out = __libelf_decompress (ELFCOMPRESS_ZLIB, buf_in, size_in, size);
     183         [ -  + ]:        592 :       if (buf_out == NULL)
     184                 :            :         return -1;
     185                 :            : 
     186                 :            :       /* We don't know anything about sh_entsize, sh_addralign and
     187                 :            :          sh_flags won't have a SHF_COMPRESSED hint in the GNU format.
     188                 :            :          Just adjust the sh_size.  */
     189         [ +  + ]:        592 :       if (elfclass == ELFCLASS32)
     190                 :            :         {
     191                 :        208 :           Elf32_Shdr *shdr = elf32_getshdr (scn);
     192                 :        208 :           shdr->sh_size = size;
     193                 :            :         }
     194                 :            :       else
     195                 :            :         {
     196                 :        384 :           Elf64_Shdr *shdr = elf64_getshdr (scn);
     197                 :        384 :           shdr->sh_size = size;
     198                 :            :         }
     199                 :            : 
     200                 :        592 :       __libelf_reset_rawdata (scn, buf_out, size, sh_addralign,
     201                 :            :                               __libelf_data_type (&ehdr, sh_type,
     202                 :            :                                                   sh_addralign));
     203                 :            : 
     204                 :        592 :       scn->zdata_base = buf_out;
     205                 :            : 
     206                 :        592 :       return 1;
     207                 :            :     }
     208                 :            :   else
     209                 :            :     {
     210                 :          0 :       __libelf_seterrno (ELF_E_UNKNOWN_COMPRESSION_TYPE);
     211                 :          0 :       return -1;
     212                 :            :     }
     213                 :            : }

Generated by: LCOV version 1.16