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.
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
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.
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>. */
64
write_file (Elf *elf, off_t size, int change_bo, size_t shnum)
66
int class = elf->class;
68
/* Check the mode bits now, before modification might change them. */
70
if (unlikely (fstat (elf->fildes, &st) != 0))
72
__libelf_seterrno (ELF_E_WRITE_ERROR);
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))
86
__libelf_seterrno (ELF_E_WRITE_ERROR);
90
/* Try to map the file if this isn't done yet. */
91
if (elf->map_address == NULL && elf->cmd == ELF_C_WRITE_MMAP)
94
/* Mudflap doesn't grok that our mmap'd data is ok. */
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;
103
if (elf->map_address != NULL)
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. */
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. */
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))
128
__libelf_seterrno (ELF_E_WRITE_ERROR);
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. */
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))
140
__libelf_seterrno (ELF_E_WRITE_ERROR);
144
if (size != -1 && elf->parent == NULL)
145
elf->maximum_size = size;
152
elf_update (elf, cmd)
160
if (cmd != ELF_C_NULL
161
&& cmd != ELF_C_WRITE
162
&& unlikely (cmd != ELF_C_WRITE_MMAP))
164
__libelf_seterrno (ELF_E_INVALID_CMD);
171
if (elf->kind != ELF_K_ELF)
173
__libelf_seterrno (ELF_E_INVALID_HANDLE);
177
rwlock_wrlock (elf->lock);
179
/* Make sure we have an ELF header. */
180
if (elf->state.elf.ehdr == NULL)
182
__libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
187
/* Determine the number of sections. */
188
shnum = (elf->state.elf.scns_last->cnt == 0
190
: 1 + elf->state.elf.scns_last->data[elf->state.elf.scns_last->cnt - 1].index);
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 (elf, &change_bo, shnum)
197
: __elf64_updatenull (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))
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))
207
__libelf_seterrno (ELF_E_UPDATE_RO);
210
else if (unlikely (elf->fildes == -1))
212
/* We closed the file already. */
213
__libelf_seterrno (ELF_E_FD_DISABLED);
217
size = write_file (elf, size, change_bo, shnum);
221
rwlock_unlock (elf->lock);