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.
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.
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.
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/>. */
36 #include <stdio_ext.h>
39 #include <sys/param.h>
47 /* Prototypes for local functions. */
48 static int handle_file (const char *fname);
51 /* Name and version of program. */
52 static void print_version (FILE *stream, struct argp_state *state);
53 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
55 /* Bug report address. */
56 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
59 /* Definitions of arguments for argp functions. */
60 static const struct argp_option options[] =
62 { NULL, 0, NULL, 0, NULL, 0 }
65 /* Short description of program. */
66 static const char doc[] = N_("Generate an index to speed access to archives.");
68 /* Strings for arguments in help texts. */
69 static const char args_doc[] = N_("ARCHIVE");
71 /* Data structure to communicate with argp functions. */
72 static const struct argp argp =
74 options, NULL, args_doc, doc, arlib_argp_children, NULL, NULL
79 main (int argc, char *argv[])
81 /* Make memory leak detection possible. */
84 /* We use no threads here which can interfere with handling a stream. */
85 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
86 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
87 (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER);
90 (void) setlocale (LC_ALL, "");
92 /* Make sure the message catalog can be found. */
93 (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
95 /* Initialize the message catalog. */
96 (void) textdomain (PACKAGE_TARNAME);
98 /* Parse and process arguments. */
100 (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL);
102 /* Tell the library which version we are expecting. */
103 (void) elf_version (EV_CURRENT);
105 /* There must at least be one more parameter specifying the archive. */
106 if (remaining == argc)
108 error (0, 0, gettext ("Archive name required"));
109 argp_help (&argp, stderr, ARGP_HELP_SEE, "ranlib");
113 /* We accept the names of multiple archives. */
116 status |= handle_file (argv[remaining]);
117 while (++remaining < argc);
123 /* Print the version information. */
125 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
127 fprintf (stream, "ranlib (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
128 fprintf (stream, gettext ("\
129 Copyright (C) %s Red Hat, Inc.\n\
130 This is free software; see the source for copying conditions. There is NO\n\
131 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
133 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
138 copy_content (Elf *elf, int newfd, off_t off, size_t n)
141 char *rawfile = elf_rawfile (elf, &len);
143 assert (off + n <= len);
145 /* Tell the kernel we will read all the pages sequentially. */
146 size_t ps = sysconf (_SC_PAGESIZE);
148 posix_madvise (rawfile + (off & ~(ps - 1)), n, POSIX_MADV_SEQUENTIAL);
150 return write_retry (newfd, rawfile + off, n) != (ssize_t) n;
154 /* Handle a file given on the command line. */
156 handle_file (const char *fname)
158 int fd = open (fname, O_RDONLY);
161 error (0, errno, gettext ("cannot open '%s'"), fname);
166 if (fstat (fd, &st) != 0)
168 error (0, errno, gettext ("cannot stat '%s'"), fname);
173 /* First we walk through the file, looking for all ELF files to
174 collect symbols from. */
175 Elf *arelf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
178 error (0, 0, gettext ("cannot create ELF descriptor for '%s': %s"),
179 fname, elf_errmsg (-1));
184 if (elf_kind (arelf) != ELF_K_AR)
186 error (0, 0, gettext ("'%s' is no archive"), fname);
194 /* Iterate over the content of the archive. */
195 off_t index_off = -1;
196 size_t index_size = 0;
197 off_t cur_off = SARMAG;
199 Elf_Cmd cmd = ELF_C_READ_MMAP;
200 while ((elf = elf_begin (fd, cmd, arelf)) != NULL)
202 Elf_Arhdr *arhdr = elf_getarhdr (elf);
203 assert (arhdr != NULL);
205 /* If this is the index, remember the location. */
206 if (strcmp (arhdr->ar_name, "/") == 0)
208 index_off = elf_getaroff (elf);
209 index_size = arhdr->ar_size;
213 arlib_add_symbols (elf, fname, arhdr->ar_name, cur_off);
214 cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
215 + sizeof (struct ar_hdr));
218 /* Get next archive element. */
219 cmd = elf_next (elf);
220 if (elf_end (elf) != 0)
221 error (0, 0, gettext ("error while freeing sub-ELF descriptor: %s"),
227 /* If the file contains no symbols we need not do anything. */
229 if (symtab.symsnamelen != 0
230 /* We have to rewrite the file also if it initially had an index
231 but now does not need one anymore. */
232 || (symtab.symsnamelen == 0 && index_size != 0))
234 /* Create a new, temporary file in the same directory as the
236 char tmpfname[strlen (fname) + 7];
237 strcpy (stpcpy (tmpfname, fname), "XXXXXX");
238 int newfd = mkstemp (tmpfname);
239 if (unlikely (newfd == -1))
242 error (0, errno, gettext ("cannot create new file"));
247 /* Create the header. */
248 if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
250 // XXX Use /prof/self/fd/%d ???
258 /* Create the new file. There are three parts as far we are
259 concerned: 1. original context before the index, 2. the
260 new index, 3. everything after the new index. */
263 rest_off = (index_off + sizeof (struct ar_hdr)
264 + ((index_size + 1) & ~1ul));
268 if ((symtab.symsnamelen != 0
269 && ((write_retry (newfd, symtab.symsoff,
271 != (ssize_t) symtab.symsofflen)
272 || (write_retry (newfd, symtab.symsname,
274 != (ssize_t) symtab.symsnamelen)))
275 /* Even if the original file had content before the
276 symbol table, we write it in the correct order. */
277 || (index_off > SARMAG
278 && copy_content (arelf, newfd, SARMAG, index_off - SARMAG))
279 || copy_content (arelf, newfd, rest_off, st.st_size - rest_off)
280 /* Set the mode of the new file to the same values the
281 original file has. */
282 || fchmod (newfd, st.st_mode & ALLPERMS) != 0
283 /* Never complain about fchown failing. */
284 || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
286 || (newfd = -1, rename (tmpfname, fname) != 0))
301 #include "debugpred.h"