LCOV - code coverage report
Current view: top level - src - ranlib.c (source / functions) Hit Total Coverage
Test: elfutils-0.173 Lines: 67 90 74.4 %
Date: 2018-06-29 23:49:12 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Generate an index to speed access to archives.
       2             :    Copyright (C) 2005-2012 Red Hat, Inc.
       3             :    This file is part of elfutils.
       4             :    Written by Ulrich Drepper <drepper@redhat.com>, 2005.
       5             : 
       6             :    This file is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    elfutils is distributed in the hope that it will be useful, but
      12             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      18             : 
      19             : #ifdef HAVE_CONFIG_H
      20             : # include <config.h>
      21             : #endif
      22             : 
      23             : #include <ar.h>
      24             : #include <argp.h>
      25             : #include <assert.h>
      26             : #include <errno.h>
      27             : #include <error.h>
      28             : #include <fcntl.h>
      29             : #include <gelf.h>
      30             : #include <libintl.h>
      31             : #include <locale.h>
      32             : #include <obstack.h>
      33             : #include <stdlib.h>
      34             : #include <stdio.h>
      35             : #include <stdio_ext.h>
      36             : #include <unistd.h>
      37             : #include <sys/mman.h>
      38             : #include <sys/stat.h>
      39             : 
      40             : #include <system.h>
      41             : #include <printversion.h>
      42             : 
      43             : #include "arlib.h"
      44             : 
      45             : 
      46             : /* Prototypes for local functions.  */
      47             : static int handle_file (const char *fname);
      48             : 
      49             : 
      50             : /* Name and version of program.  */
      51             : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
      52             : 
      53             : /* Bug report address.  */
      54             : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
      55             : 
      56             : 
      57             : /* Definitions of arguments for argp functions.  */
      58             : static const struct argp_option options[] =
      59             : {
      60             :   { NULL, 0, NULL, 0, NULL, 0 }
      61             : };
      62             : 
      63             : /* Short description of program.  */
      64             : static const char doc[] = N_("Generate an index to speed access to archives.");
      65             : 
      66             : /* Strings for arguments in help texts.  */
      67             : static const char args_doc[] = N_("ARCHIVE");
      68             : 
      69             : /* Data structure to communicate with argp functions.  */
      70             : static const struct argp argp =
      71             : {
      72             :   options, NULL, args_doc, doc, arlib_argp_children, NULL, NULL
      73             : };
      74             : 
      75             : 
      76             : int
      77           4 : main (int argc, char *argv[])
      78             : {
      79             :   /* We use no threads here which can interfere with handling a stream.  */
      80           4 :   (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
      81           4 :   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
      82           4 :   (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER);
      83             : 
      84             :   /* Set locale.  */
      85           4 :   (void) setlocale (LC_ALL, "");
      86             : 
      87             :   /* Make sure the message catalog can be found.  */
      88           4 :   (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
      89             : 
      90             :   /* Initialize the message catalog.  */
      91           4 :   (void) textdomain (PACKAGE_TARNAME);
      92             : 
      93             :   /* Parse and process arguments.  */
      94             :   int remaining;
      95           4 :   (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL);
      96             : 
      97             :   /* Tell the library which version we are expecting.  */
      98           4 :   (void) elf_version (EV_CURRENT);
      99             : 
     100             :   /* There must at least be one more parameter specifying the archive.   */
     101           4 :   if (remaining == argc)
     102             :     {
     103           0 :       error (0, 0, gettext ("Archive name required"));
     104           0 :       argp_help (&argp, stderr, ARGP_HELP_SEE, "ranlib");
     105           0 :       exit (EXIT_FAILURE);
     106             :     }
     107             : 
     108             :   /* We accept the names of multiple archives.  */
     109             :   int status = 0;
     110             :   do
     111           4 :     status |= handle_file (argv[remaining]);
     112           4 :   while (++remaining < argc);
     113             : 
     114             :   return status;
     115             : }
     116             : 
     117             : 
     118             : static int
     119           3 : copy_content (Elf *elf, int newfd, off_t off, size_t n)
     120             : {
     121             :   size_t len;
     122           3 :   char *rawfile = elf_rawfile (elf, &len);
     123             : 
     124           3 :   assert (off + n <= len);
     125             : 
     126             :   /* Tell the kernel we will read all the pages sequentially.  */
     127           3 :   size_t ps = sysconf (_SC_PAGESIZE);
     128           3 :   if (n > 2 * ps)
     129           0 :     posix_madvise (rawfile + (off & ~(ps - 1)), n, POSIX_MADV_SEQUENTIAL);
     130             : 
     131           3 :   return write_retry (newfd, rawfile + off, n) != (ssize_t) n;
     132             : }
     133             : 
     134             : 
     135             : /* Handle a file given on the command line.  */
     136             : static int
     137           4 : handle_file (const char *fname)
     138             : {
     139           4 :   int fd = open (fname, O_RDONLY);
     140           4 :   if (fd == -1)
     141             :     {
     142           0 :       error (0, errno, gettext ("cannot open '%s'"), fname);
     143           0 :       return 1;
     144             :     }
     145             : 
     146             :   struct stat st;
     147           4 :   if (fstat (fd, &st) != 0)
     148             :     {
     149           0 :       error (0, errno, gettext ("cannot stat '%s'"), fname);
     150           0 :       close (fd);
     151           0 :       return 1;
     152             :     }
     153             : 
     154             :   /* First we walk through the file, looking for all ELF files to
     155             :      collect symbols from.  */
     156           4 :   Elf *arelf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
     157           4 :   if (arelf == NULL)
     158             :     {
     159           0 :       error (0, 0, gettext ("cannot create ELF descriptor for '%s': %s"),
     160             :              fname, elf_errmsg (-1));
     161           0 :       close (fd);
     162           0 :       return 1;
     163             :     }
     164             : 
     165           4 :   if (elf_kind (arelf) != ELF_K_AR)
     166             :     {
     167           0 :       error (0, 0, gettext ("'%s' is no archive"), fname);
     168           0 :       elf_end (arelf);
     169           0 :       close (fd);
     170           0 :       return 1;
     171             :     }
     172             : 
     173           4 :   arlib_init ();
     174             : 
     175             :   /* Iterate over the content of the archive.  */
     176           4 :   off_t index_off = -1;
     177           4 :   size_t index_size = 0;
     178           4 :   off_t cur_off = SARMAG;
     179             :   Elf *elf;
     180           4 :   Elf_Cmd cmd = ELF_C_READ_MMAP;
     181          21 :   while ((elf = elf_begin (fd, cmd, arelf)) != NULL)
     182             :     {
     183          13 :       Elf_Arhdr *arhdr = elf_getarhdr (elf);
     184          13 :       assert (arhdr != NULL);
     185             : 
     186             :       /* If this is the index, remember the location.  */
     187          13 :       if (strcmp (arhdr->ar_name, "/") == 0)
     188             :         {
     189           2 :           index_off = elf_getaroff (elf);
     190           2 :           index_size = arhdr->ar_size;
     191             :         }
     192             :       else
     193             :         {
     194          11 :           arlib_add_symbols (elf, fname, arhdr->ar_name, cur_off);
     195          11 :           cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
     196             :                       + sizeof (struct ar_hdr));
     197             :         }
     198             : 
     199             :       /* Get next archive element.  */
     200          13 :       cmd = elf_next (elf);
     201          13 :       if (elf_end (elf) != 0)
     202           0 :         error (0, 0, gettext ("error while freeing sub-ELF descriptor: %s"),
     203             :                elf_errmsg (-1));
     204             :     }
     205             : 
     206           4 :   arlib_finalize ();
     207             : 
     208             :   /* If the file contains no symbols we need not do anything.  */
     209           4 :   int status = 0;
     210           8 :   if (symtab.symsnamelen != 0
     211             :       /* We have to rewrite the file also if it initially had an index
     212             :          but now does not need one anymore.  */
     213           4 :       || (symtab.symsnamelen == 0 && index_size != 0))
     214             :     {
     215             :       /* Create a new, temporary file in the same directory as the
     216             :          original file.  */
     217           3 :       char tmpfname[strlen (fname) + 7];
     218           6 :       strcpy (stpcpy (tmpfname, fname), "XXXXXX");
     219           3 :       int newfd = mkstemp (tmpfname);
     220           3 :       if (unlikely (newfd == -1))
     221             :         {
     222             :         nonew:
     223           0 :           error (0, errno, gettext ("cannot create new file"));
     224           0 :           status = 1;
     225             :         }
     226             :       else
     227             :         {
     228             :           /* Create the header.  */
     229           3 :           if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
     230             :             {
     231             :               // XXX Use /prof/self/fd/%d ???
     232             :             nonew_unlink:
     233           0 :               unlink (tmpfname);
     234           0 :               if (newfd != -1)
     235           0 :                 close (newfd);
     236             :               goto nonew;
     237             :             }
     238             : 
     239             :           /* Create the new file.  There are three parts as far we are
     240             :              concerned: 1. original context before the index, 2. the
     241             :              new index, 3. everything after the new index.  */
     242             :           off_t rest_off;
     243           3 :           if (index_off != -1)
     244           2 :             rest_off = (index_off + sizeof (struct ar_hdr)
     245           2 :                         + ((index_size + 1) & ~1ul));
     246             :           else
     247             :             rest_off = SARMAG;
     248             : 
     249           3 :           if ((symtab.symsnamelen != 0
     250           4 :                && ((write_retry (newfd, symtab.symsoff,
     251             :                                  symtab.symsofflen)
     252           2 :                     != (ssize_t) symtab.symsofflen)
     253           4 :                    || (write_retry (newfd, symtab.symsname,
     254             :                                     symtab.symsnamelen)
     255           2 :                        != (ssize_t) symtab.symsnamelen)))
     256             :               /* Even if the original file had content before the
     257             :                  symbol table, we write it in the correct order.  */
     258           3 :               || (index_off > SARMAG
     259           0 :                   && copy_content (arelf, newfd, SARMAG, index_off - SARMAG))
     260           3 :               || copy_content (arelf, newfd, rest_off, st.st_size - rest_off)
     261             :               /* Set the mode of the new file to the same values the
     262             :                  original file has.  */
     263           3 :               || fchmod (newfd, st.st_mode & ALLPERMS) != 0
     264             :               /* Never complain about fchown failing.  */
     265           6 :               || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
     266           3 :                   close (newfd) != 0)
     267           3 :               || (newfd = -1, rename (tmpfname, fname) != 0))
     268             :             goto nonew_unlink;
     269             :         }
     270             :     }
     271             : 
     272           4 :   elf_end (arelf);
     273             : 
     274           4 :   arlib_fini ();
     275             : 
     276           4 :   close (fd);
     277             : 
     278           4 :   return status;
     279             : }
     280             : 
     281             : 
     282             : #include "debugpred.h"

Generated by: LCOV version 1.13