1 /* Generate an index to speed access to archives.
2 Copyright (C) 2005-2012 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2005.
6 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
10 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
19 Red Hat elfutils is an included package of the Open Invention Network.
20 An included package of the Open Invention Network is a package for which
21 Open Invention Network licensees cross-license their patents. No patent
22 license is granted, either expressly or impliedly, by designation as an
23 included package. Should you wish to participate in the Open Invention
24 Network licensing program, please visit www.openinventionnetwork.com
25 <http://www.openinventionnetwork.com>. */
44 #include <stdio_ext.h>
47 #include <sys/param.h>
55 /* Prototypes for local functions. */
56 static int handle_file (const char *fname);
59 /* Name and version of program. */
60 static void print_version (FILE *stream, struct argp_state *state);
61 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
63 /* Bug report address. */
64 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
67 /* Definitions of arguments for argp functions. */
68 static const struct argp_option options[] =
70 { NULL, 0, NULL, 0, NULL, 0 }
73 /* Short description of program. */
74 static const char doc[] = N_("Generate an index to speed access to archives.");
76 /* Strings for arguments in help texts. */
77 static const char args_doc[] = N_("ARCHIVE");
79 /* Data structure to communicate with argp functions. */
80 static const struct argp argp =
82 options, NULL, args_doc, doc, arlib_argp_children, NULL, NULL
87 main (int argc, char *argv[])
89 /* Make memory leak detection possible. */
92 /* We use no threads here which can interfere with handling a stream. */
93 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
94 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
95 (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER);
98 (void) setlocale (LC_ALL, "");
100 /* Make sure the message catalog can be found. */
101 (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
103 /* Initialize the message catalog. */
104 (void) textdomain (PACKAGE_TARNAME);
106 /* Parse and process arguments. */
108 (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL);
110 /* Tell the library which version we are expecting. */
111 (void) elf_version (EV_CURRENT);
113 /* There must at least be one more parameter specifying the archive. */
114 if (remaining == argc)
116 error (0, 0, gettext ("Archive name required"));
117 argp_help (&argp, stderr, ARGP_HELP_SEE, "ranlib");
121 /* We accept the names of multiple archives. */
124 status |= handle_file (argv[remaining]);
125 while (++remaining < argc);
131 /* Print the version information. */
133 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
135 fprintf (stream, "ranlib (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
136 fprintf (stream, gettext ("\
137 Copyright (C) %s Red Hat, Inc.\n\
138 This is free software; see the source for copying conditions. There is NO\n\
139 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
141 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
146 copy_content (Elf *elf, int newfd, off_t off, size_t n)
149 char *rawfile = elf_rawfile (elf, &len);
151 assert (off + n <= len);
153 /* Tell the kernel we will read all the pages sequentially. */
154 size_t ps = sysconf (_SC_PAGESIZE);
156 posix_madvise (rawfile + (off & ~(ps - 1)), n, POSIX_MADV_SEQUENTIAL);
158 return write_retry (newfd, rawfile + off, n) != (ssize_t) n;
162 /* Handle a file given on the command line. */
164 handle_file (const char *fname)
166 int fd = open (fname, O_RDONLY);
169 error (0, errno, gettext ("cannot open '%s'"), fname);
174 if (fstat (fd, &st) != 0)
176 error (0, errno, gettext ("cannot stat '%s'"), fname);
181 /* First we walk through the file, looking for all ELF files to
182 collect symbols from. */
183 Elf *arelf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
186 error (0, 0, gettext ("cannot create ELF descriptor for '%s': %s"),
187 fname, elf_errmsg (-1));
192 if (elf_kind (arelf) != ELF_K_AR)
194 error (0, 0, gettext ("'%s' is no archive"), fname);
202 /* Iterate over the content of the archive. */
203 off_t index_off = -1;
204 size_t index_size = 0;
205 off_t cur_off = SARMAG;
207 Elf_Cmd cmd = ELF_C_READ_MMAP;
208 while ((elf = elf_begin (fd, cmd, arelf)) != NULL)
210 Elf_Arhdr *arhdr = elf_getarhdr (elf);
211 assert (arhdr != NULL);
213 /* If this is the index, remember the location. */
214 if (strcmp (arhdr->ar_name, "/") == 0)
216 index_off = elf_getaroff (elf);
217 index_size = arhdr->ar_size;
221 arlib_add_symbols (elf, fname, arhdr->ar_name, cur_off);
222 cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
223 + sizeof (struct ar_hdr));
226 /* Get next archive element. */
227 cmd = elf_next (elf);
228 if (elf_end (elf) != 0)
229 error (0, 0, gettext ("error while freeing sub-ELF descriptor: %s"),
235 /* If the file contains no symbols we need not do anything. */
237 if (symtab.symsnamelen != 0
238 /* We have to rewrite the file also if it initially had an index
239 but now does not need one anymore. */
240 || (symtab.symsnamelen == 0 && index_size != 0))
242 /* Create a new, temporary file in the same directory as the
244 char tmpfname[strlen (fname) + 7];
245 strcpy (stpcpy (tmpfname, fname), "XXXXXX");
246 int newfd = mkstemp (tmpfname);
247 if (unlikely (newfd == -1))
250 error (0, errno, gettext ("cannot create new file"));
255 /* Create the header. */
256 if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
258 // XXX Use /prof/self/fd/%d ???
266 /* Create the new file. There are three parts as far we are
267 concerned: 1. original context before the index, 2. the
268 new index, 3. everything after the new index. */
271 rest_off = (index_off + sizeof (struct ar_hdr)
272 + ((index_size + 1) & ~1ul));
276 if ((symtab.symsnamelen != 0
277 && ((write_retry (newfd, symtab.symsoff,
279 != (ssize_t) symtab.symsofflen)
280 || (write_retry (newfd, symtab.symsname,
282 != (ssize_t) symtab.symsnamelen)))
283 /* Even if the original file had content before the
284 symbol table, we write it in the correct order. */
285 || (index_off > SARMAG
286 && copy_content (arelf, newfd, SARMAG, index_off - SARMAG))
287 || copy_content (arelf, newfd, rest_off, st.st_size - rest_off)
288 /* Set the mode of the new file to the same values the
289 original file has. */
290 || fchmod (newfd, st.st_mode & ALLPERMS) != 0
291 /* Never complain about fchown failing. */
292 || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
294 || (newfd = -1, rename (tmpfname, fname) != 0))
309 #include "debugpred.h"