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