1/* Update data structures for changes and write them out.
2   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006 Red Hat, Inc.
3   This file is part of Red Hat elfutils.
4   Contributed by Ulrich Drepper <drepper@redhat.com>, 1999.
5
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.
9
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.
14
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.
18
19   In addition, as a special exception, Red Hat, Inc. gives You the
20   additional right to link the code of Red Hat elfutils with code licensed
21   under any Open Source Initiative certified open source license
22   (http://www.opensource.org/licenses/index.php) which requires the
23   distribution of source code with any binary distribution and to
24   distribute linked combinations of the two.  Non-GPL Code permitted under
25   this exception must only link to the code of Red Hat elfutils through
26   those well defined interfaces identified in the file named EXCEPTION
27   found in the source code files (the "Approved Interfaces").  The files
28   of Non-GPL Code may instantiate templates or use macros or inline
29   functions from the Approved Interfaces without causing the resulting
30   work to be covered by the GNU General Public License.  Only Red Hat,
31   Inc. may make changes or additions to the list of Approved Interfaces.
32   Red Hat's grant of this exception is conditioned upon your not adding
33   any new exceptions.  If you wish to add a new Approved Interface or
34   exception, please contact Red Hat.  You must obey the GNU General Public
35   License in all respects for all of the Red Hat elfutils code and other
36   code used in conjunction with Red Hat elfutils except the Non-GPL Code
37   covered by this exception.  If you modify this file, you may extend this
38   exception to your version of the file, but you are not obligated to do
39   so.  If you do not wish to provide this exception without modification,
40   you must delete this exception statement from your version and license
41   this file solely under the GPL without exception.
42
43   Red Hat elfutils is an included package of the Open Invention Network.
44   An included package of the Open Invention Network is a package for which
45   Open Invention Network licensees cross-license their patents.  No patent
46   license is granted, either expressly or impliedly, by designation as an
47   included package.  Should you wish to participate in the Open Invention
48   Network licensing program, please visit www.openinventionnetwork.com
49   <http://www.openinventionnetwork.com>.  */
50
51#ifdef HAVE_CONFIG_H
52# include <config.h>
53#endif
54
55#include <libelf.h>
56#include <unistd.h>
57#include <sys/mman.h>
58#include <sys/stat.h>
59
60#include "libelfP.h"
61
62
63static off_t
64write_file (Elf *elf, off_t size, int change_bo, size_t shnum)
65{
66  int class = elf->class;
67
68  /* Check the mode bits now, before modification might change them.  */
69  struct stat st;
70  if (unlikely (fstat (elf->fildes, &st) != 0))
71    {
72      __libelf_seterrno (ELF_E_WRITE_ERROR);
73      return -1;
74    }
75
76  /* Adjust the size in any case.  We do this even if we use `write'.
77     We cannot do this if this file is in an archive.  We also don't
78     do it *now* if we are shortening the file since this would
79     prevent programs to use the data of the file in generating the
80     new file.  We truncate the file later in this case.  */
81  if (elf->parent == NULL
82      && (elf->maximum_size == ~((size_t) 0)
83	  || (size_t) size > elf->maximum_size)
84      && unlikely (ftruncate (elf->fildes, size) != 0))
85    {
86      __libelf_seterrno (ELF_E_WRITE_ERROR);
87      return -1;
88    }
89
90  /* Try to map the file if this isn't done yet.  */
91  if (elf->map_address == NULL && elf->cmd == ELF_C_WRITE_MMAP)
92    {
93#if _MUDFLAP
94      /* Mudflap doesn't grok that our mmap'd data is ok.  */
95#else
96      elf->map_address = mmap (NULL, size, PROT_READ | PROT_WRITE,
97			       MAP_SHARED, elf->fildes, 0);
98      if (unlikely (elf->map_address == MAP_FAILED))
99	elf->map_address = NULL;
100#endif
101    }
102
103  if (elf->map_address != NULL)
104    {
105      /* The file is mmaped.  */
106      if ((class == ELFCLASS32
107	   ? __elf32_updatemmap (elf, change_bo, shnum)
108	   : __elf64_updatemmap (elf, change_bo, shnum)) != 0)
109	/* Some problem while writing.  */
110	size = -1;
111    }
112  else
113    {
114      /* The file is not mmaped.  */
115      if ((class == ELFCLASS32
116	   ? __elf32_updatefile (elf, change_bo, shnum)
117	   : __elf64_updatefile (elf, change_bo, shnum)) != 0)
118	/* Some problem while writing.  */
119	size = -1;
120    }
121
122  if (size != -1
123      && elf->parent == NULL
124      && elf->maximum_size != ~((size_t) 0)
125      && (size_t) size < elf->maximum_size
126      && unlikely (ftruncate (elf->fildes, size) != 0))
127    {
128      __libelf_seterrno (ELF_E_WRITE_ERROR);
129      size = -1;
130    }
131
132  /* POSIX says that ftruncate and write may clear the S_ISUID and S_ISGID
133     mode bits.  So make sure we restore them afterwards if they were set.
134     This is not atomic if someone else chmod's the file while we operate.  */
135  if (size != -1
136      && unlikely (st.st_mode & (S_ISUID | S_ISGID))
137      /* fchmod ignores the bits we cannot change.  */
138      && unlikely (fchmod (elf->fildes, st.st_mode) != 0))
139    {
140      __libelf_seterrno (ELF_E_WRITE_ERROR);
141      size = -1;
142    }
143
144  if (size != -1 && elf->parent == NULL)
145    elf->maximum_size = size;
146
147  return size;
148}
149
150
151off_t
152elf_update (elf, cmd)
153     Elf *elf;
154     Elf_Cmd cmd;
155{
156  size_t shnum;
157  off_t size;
158  int change_bo = 0;
159
160  if (cmd != ELF_C_NULL
161      && cmd != ELF_C_WRITE
162      && unlikely (cmd != ELF_C_WRITE_MMAP))
163    {
164      __libelf_seterrno (ELF_E_INVALID_CMD);
165      return -1;
166    }
167
168  if (elf == NULL)
169    return -1;
170
171  if (elf->kind != ELF_K_ELF)
172    {
173      __libelf_seterrno (ELF_E_INVALID_HANDLE);
174      return -1;
175    }
176
177  rwlock_wrlock (elf->lock);
178
179  /* Make sure we have an ELF header.  */
180  if (elf->state.elf.ehdr == NULL)
181    {
182      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
183      size = -1;
184      goto out;
185    }
186
187  /* Determine the number of sections.  */
188  shnum = (elf->state.elf.scns_last->cnt == 0
189	   ? 0
190	   : 1 + elf->state.elf.scns_last->data[elf->state.elf.scns_last->cnt - 1].index);
191
192  /* Update the ELF descriptor.  First, place the program header.  It
193     will come right after the ELF header.  The count the size of all
194     sections and finally place the section table.  */
195  size = (elf->class == ELFCLASS32
196	  ? __elf32_updatenull_wrlock (elf, &change_bo, shnum)
197	  : __elf64_updatenull_wrlock (elf, &change_bo, shnum));
198  if (likely (size != -1)
199      /* See whether we actually have to write out the data.  */
200      && (cmd == ELF_C_WRITE || cmd == ELF_C_WRITE_MMAP))
201    {
202      if (elf->cmd != ELF_C_RDWR
203	  && elf->cmd != ELF_C_RDWR_MMAP
204	  && elf->cmd != ELF_C_WRITE
205	  && unlikely (elf->cmd != ELF_C_WRITE_MMAP))
206	{
207	  __libelf_seterrno (ELF_E_UPDATE_RO);
208	  size = -1;
209	}
210      else if (unlikely (elf->fildes == -1))
211	{
212	  /* We closed the file already.  */
213	  __libelf_seterrno (ELF_E_FD_DISABLED);
214	  size = -1;
215	}
216      else
217	size = write_file (elf, size, change_bo, shnum);
218    }
219
220 out:
221  rwlock_unlock (elf->lock);
222
223  return size;
224}
225