1 /* Copyright (C) 2001, 2003, 2004, 2005, 2007 Red Hat, Inc.
2 Written by Jakub Jelinek <jakub@redhat.com>, 2001.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "prelinktab.h"
31 struct prelink_entry **ents;
36 find_ents (void **p, void *info)
38 struct collect_ents *l = (struct collect_ents *) info;
39 struct prelink_entry *e = * (struct prelink_entry **) p;
42 if ((e->type == ET_DYN && e->done == 1)
43 || (e->type == ET_EXEC && e->done == 0 && ! libs_only))
44 l->ents[l->nents++] = e;
50 prelink_ent (struct prelink_entry *ent)
55 struct prelink_link *hardlink;
59 for (i = 0; i < ent->ndepends; ++i)
60 if (ent->depends[i]->done == 1)
61 prelink_ent (ent->depends[i]);
63 for (i = 0; i < ent->ndepends; ++i)
64 if (ent->depends[i]->done != 2)
68 ent->type = ET_UNPRELINKABLE;
70 error (0, 0, "Could not prelink %s because its dependency %s could not be prelinked",
71 ent->filename, ent->depends[i]->filename);
76 for (i = 0; i < ent->ndepends; ++i)
77 ent->depends[i]->u.tmp = 1;
78 for (i = 0; i < ent->ndepends; ++i)
80 struct prelink_entry *dent = ent->depends[i];
81 for (j = 0; j < dent->ndepends; ++j)
82 if (dent->depends[j]->u.tmp == 0)
86 ent->type = ET_UNPRELINKABLE;
88 error (0, 0, "Could not prelink %s because it doesn't use %s, but one of its dependencies has been prelinked against it",
89 ent->filename, dent->depends[j]->filename);
91 for (i = 0; i < ent->ndepends; ++i)
92 ent->depends[i]->u.tmp = 0;
97 for (i = 0; i < ent->ndepends; ++i)
98 ent->depends[i]->u.tmp = 0;
103 printf ("Would prelink %s\n", ent->canon_filename);
105 printf ("Prelinking %s\n", ent->canon_filename);
108 dso = open_dso (ent->canon_filename);
112 if (fstat64 (dso->fd, &st) < 0)
114 error (0, errno, "%s changed during prelinking", ent->filename);
118 if (st.st_dev != ent->dev || st.st_ino != ent->ino)
120 error (0, 0, "%s changed during prelinking", ent->filename);
128 if (prelink_prepare (dso))
129 goto make_unprelinkable;
130 if (ent->type == ET_DYN && relocate_dso (dso, ent->base))
131 goto make_unprelinkable;
132 if (prelink (dso, ent))
133 goto make_unprelinkable;
134 if (update_dso (dso, NULL))
141 ent->flags |= PCF_PRELINKED;
143 /* Redo hardlinks. */
144 for (hardlink = ent->hardlink; hardlink; hardlink = hardlink->next)
148 if (lstat64 (hardlink->canon_filename, &st) < 0)
150 error (0, 0, "Could not stat %s (former hardlink to %s)",
151 hardlink->canon_filename, ent->canon_filename);
155 if (st.st_dev != ent->dev || st.st_ino != ent->ino)
157 error (0, 0, "%s is no longer hardlink to %s",
158 hardlink->canon_filename, ent->canon_filename);
165 printf ("Would link %s to %s\n", hardlink->canon_filename,
166 ent->canon_filename);
168 printf ("Linking %s to %s\n", hardlink->canon_filename,
169 ent->canon_filename);
175 len = strlen (hardlink->canon_filename);
176 if (len + sizeof (".#prelink#") > movelen)
178 movelen = len + sizeof (".#prelink#");
179 move = realloc (move, movelen);
182 error (0, ENOMEM, "Could not hardlink %s to %s",
183 hardlink->canon_filename, ent->canon_filename);
189 memcpy (mempcpy (move, hardlink->canon_filename, len), ".#prelink#",
190 sizeof (".#prelink#"));
191 if (rename (hardlink->canon_filename, move) < 0)
193 error (0, errno, "Could not hardlink %s to %s",
194 hardlink->canon_filename, ent->canon_filename);
198 if (link (ent->canon_filename, hardlink->canon_filename) < 0)
200 error (0, errno, "Could not hardlink %s to %s",
201 hardlink->canon_filename, ent->canon_filename);
203 if (rename (move, hardlink->canon_filename) < 0)
205 error (0, errno, "Could not rename %s back to %s",
206 move, hardlink->canon_filename);
211 if (unlink (move) < 0)
213 error (0, errno, "Could not unlink %s", move);
219 if (! dry_run && stat64 (ent->canon_filename, &st) >= 0)
221 ent->dev = st.st_dev;
222 ent->ino = st.st_ino;
223 ent->ctime = st.st_ctime;
224 ent->mtime = st.st_mtime;
230 ent->type = ET_UNPRELINKABLE;
241 struct collect_ents l;
245 (struct prelink_entry **) alloca (prelink_entry_count
246 * sizeof (struct prelink_entry *));
248 htab_traverse (prelink_filename_htab, find_ents, &l);
250 for (i = 0; i < l.nents; ++i)
251 if (l.ents[i]->done == 1
252 || (l.ents[i]->done == 0 && l.ents[i]->type == ET_EXEC))
253 prelink_ent (l.ents[i]);