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