Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame] | 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 | main (int argc, char *argv[]) |
| 78 | { |
| 79 | /* We use no threads here which can interfere with handling a stream. */ |
| 80 | (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER); |
| 81 | (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER); |
| 82 | (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER); |
| 83 | |
| 84 | /* Set locale. */ |
| 85 | (void) setlocale (LC_ALL, ""); |
| 86 | |
| 87 | /* Make sure the message catalog can be found. */ |
| 88 | (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR); |
| 89 | |
| 90 | /* Initialize the message catalog. */ |
| 91 | (void) textdomain (PACKAGE_TARNAME); |
| 92 | |
| 93 | /* Parse and process arguments. */ |
| 94 | int remaining; |
| 95 | (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL); |
| 96 | |
| 97 | /* Tell the library which version we are expecting. */ |
| 98 | (void) elf_version (EV_CURRENT); |
| 99 | |
| 100 | /* There must at least be one more parameter specifying the archive. */ |
| 101 | if (remaining == argc) |
| 102 | { |
| 103 | error (0, 0, gettext ("Archive name required")); |
| 104 | argp_help (&argp, stderr, ARGP_HELP_SEE, "ranlib"); |
| 105 | exit (EXIT_FAILURE); |
| 106 | } |
| 107 | |
| 108 | /* We accept the names of multiple archives. */ |
| 109 | int status = 0; |
| 110 | do |
| 111 | status |= handle_file (argv[remaining]); |
| 112 | while (++remaining < argc); |
| 113 | |
| 114 | return status; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | static int |
| 119 | copy_content (Elf *elf, int newfd, off_t off, size_t n) |
| 120 | { |
| 121 | size_t len; |
| 122 | char *rawfile = elf_rawfile (elf, &len); |
| 123 | |
| 124 | assert (off + n <= len); |
| 125 | |
| 126 | /* Tell the kernel we will read all the pages sequentially. */ |
| 127 | size_t ps = sysconf (_SC_PAGESIZE); |
| 128 | if (n > 2 * ps) |
| 129 | posix_madvise (rawfile + (off & ~(ps - 1)), n, POSIX_MADV_SEQUENTIAL); |
| 130 | |
| 131 | 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 | handle_file (const char *fname) |
| 138 | { |
| 139 | int fd = open (fname, O_RDONLY); |
| 140 | if (fd == -1) |
| 141 | { |
| 142 | error (0, errno, gettext ("cannot open '%s'"), fname); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | struct stat st; |
| 147 | if (fstat (fd, &st) != 0) |
| 148 | { |
| 149 | error (0, errno, gettext ("cannot stat '%s'"), fname); |
| 150 | close (fd); |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | /* First we walk through the file, looking for all ELF files to |
| 155 | collect symbols from. */ |
| 156 | Elf *arelf = elf_begin (fd, ELF_C_READ_MMAP, NULL); |
| 157 | if (arelf == NULL) |
| 158 | { |
| 159 | error (0, 0, gettext ("cannot create ELF descriptor for '%s': %s"), |
| 160 | fname, elf_errmsg (-1)); |
| 161 | close (fd); |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | if (elf_kind (arelf) != ELF_K_AR) |
| 166 | { |
| 167 | error (0, 0, gettext ("'%s' is no archive"), fname); |
| 168 | elf_end (arelf); |
| 169 | close (fd); |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | arlib_init (); |
| 174 | |
| 175 | /* Iterate over the content of the archive. */ |
| 176 | off_t index_off = -1; |
| 177 | size_t index_size = 0; |
| 178 | off_t cur_off = SARMAG; |
| 179 | Elf *elf; |
| 180 | Elf_Cmd cmd = ELF_C_READ_MMAP; |
| 181 | while ((elf = elf_begin (fd, cmd, arelf)) != NULL) |
| 182 | { |
| 183 | Elf_Arhdr *arhdr = elf_getarhdr (elf); |
| 184 | assert (arhdr != NULL); |
| 185 | |
| 186 | /* If this is the index, remember the location. */ |
| 187 | if (strcmp (arhdr->ar_name, "/") == 0) |
| 188 | { |
| 189 | index_off = elf_getaroff (elf); |
| 190 | index_size = arhdr->ar_size; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | arlib_add_symbols (elf, fname, arhdr->ar_name, cur_off); |
| 195 | cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1)) |
| 196 | + sizeof (struct ar_hdr)); |
| 197 | } |
| 198 | |
| 199 | /* Get next archive element. */ |
| 200 | cmd = elf_next (elf); |
| 201 | if (elf_end (elf) != 0) |
| 202 | error (0, 0, gettext ("error while freeing sub-ELF descriptor: %s"), |
| 203 | elf_errmsg (-1)); |
| 204 | } |
| 205 | |
| 206 | arlib_finalize (); |
| 207 | |
| 208 | /* If the file contains no symbols we need not do anything. */ |
| 209 | int status = 0; |
| 210 | 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 | || (symtab.symsnamelen == 0 && index_size != 0)) |
| 214 | { |
| 215 | /* Create a new, temporary file in the same directory as the |
| 216 | original file. */ |
| 217 | char tmpfname[strlen (fname) + 7]; |
| 218 | strcpy (stpcpy (tmpfname, fname), "XXXXXX"); |
| 219 | int newfd = mkstemp (tmpfname); |
| 220 | if (unlikely (newfd == -1)) |
| 221 | { |
| 222 | nonew: |
| 223 | error (0, errno, gettext ("cannot create new file")); |
| 224 | status = 1; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | /* Create the header. */ |
| 229 | if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG)) |
| 230 | { |
| 231 | // XXX Use /prof/self/fd/%d ??? |
| 232 | nonew_unlink: |
| 233 | unlink (tmpfname); |
| 234 | if (newfd != -1) |
| 235 | 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 | if (index_off != -1) |
| 244 | rest_off = (index_off + sizeof (struct ar_hdr) |
| 245 | + ((index_size + 1) & ~1ul)); |
| 246 | else |
| 247 | rest_off = SARMAG; |
| 248 | |
| 249 | if ((symtab.symsnamelen != 0 |
| 250 | && ((write_retry (newfd, symtab.symsoff, |
| 251 | symtab.symsofflen) |
| 252 | != (ssize_t) symtab.symsofflen) |
| 253 | || (write_retry (newfd, symtab.symsname, |
| 254 | symtab.symsnamelen) |
| 255 | != (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 | || (index_off > SARMAG |
| 259 | && copy_content (arelf, newfd, SARMAG, index_off - SARMAG)) |
| 260 | || 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 | || fchmod (newfd, st.st_mode & ALLPERMS) != 0 |
| 264 | /* Never complain about fchown failing. */ |
| 265 | || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }), |
| 266 | close (newfd) != 0) |
| 267 | || (newfd = -1, rename (tmpfname, fname) != 0)) |
| 268 | goto nonew_unlink; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | elf_end (arelf); |
| 273 | |
| 274 | arlib_fini (); |
| 275 | |
| 276 | close (fd); |
| 277 | |
| 278 | return status; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | #include "debugpred.h" |