1 /* Update data structures for changes.
2 Copyright (C) 2000-2010 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
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>. */
60 #include <sys/param.h>
63 #include "elf-knowledge.h"
66 # define LIBELFBITS 32
72 ELFW(default_ehdr,LIBELFBITS) (Elf *elf, ElfW2(LIBELFBITS,Ehdr) *ehdr,
73 size_t shnum, int *change_bop)
75 /* Always write the magic bytes. */
76 if (memcmp (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0)
78 memcpy (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG);
79 elf->state.ELFW(elf,LIBELFBITS).ehdr_flags |= ELF_F_DIRTY;
82 /* Always set the file class. */
83 update_if_changed (ehdr->e_ident[EI_CLASS], ELFW(ELFCLASS,LIBELFBITS),
84 elf->state.ELFW(elf,LIBELFBITS).ehdr_flags);
86 /* Set the data encoding if necessary. */
87 if (unlikely (ehdr->e_ident[EI_DATA] == ELFDATANONE))
89 ehdr->e_ident[EI_DATA] =
90 BYTE_ORDER == BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB;
91 elf->state.ELFW(elf,LIBELFBITS).ehdr_flags |= ELF_F_DIRTY;
93 else if (unlikely (ehdr->e_ident[EI_DATA] >= ELFDATANUM))
95 __libelf_seterrno (ELF_E_DATA_ENCODING);
99 *change_bop = ((BYTE_ORDER == LITTLE_ENDIAN
100 && ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
101 || (BYTE_ORDER == BIG_ENDIAN
102 && ehdr->e_ident[EI_DATA] != ELFDATA2MSB));
104 /* Unconditionally overwrite the ELF version. */
105 update_if_changed (ehdr->e_ident[EI_VERSION], EV_CURRENT,
106 elf->state.ELFW(elf,LIBELFBITS).ehdr_flags);
108 if (unlikely (ehdr->e_version == EV_NONE)
109 || unlikely (ehdr->e_version >= EV_NUM))
111 __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
115 if (unlikely (shnum >= SHN_LORESERVE))
117 update_if_changed (ehdr->e_shnum, 0,
118 elf->state.ELFW(elf,LIBELFBITS).ehdr_flags);
121 update_if_changed (ehdr->e_shnum, shnum,
122 elf->state.ELFW(elf,LIBELFBITS).ehdr_flags);
124 if (unlikely (ehdr->e_ehsize != elf_typesize (LIBELFBITS, ELF_T_EHDR, 1)))
126 ehdr->e_ehsize = elf_typesize (LIBELFBITS, ELF_T_EHDR, 1);
127 elf->state.ELFW(elf,LIBELFBITS).ehdr_flags |= ELF_F_DIRTY;
136 __elfw2(LIBELFBITS,updatenull_wrlock) (Elf *elf, int *change_bop, size_t shnum)
138 ElfW2(LIBELFBITS,Ehdr) *ehdr;
142 ehdr = __elfw2(LIBELFBITS,getehdr_wrlock) (elf);
144 /* Set the default values. */
145 if (ELFW(default_ehdr,LIBELFBITS) (elf, ehdr, shnum, change_bop) != 0)
148 /* At least the ELF header is there. */
149 off_t size = elf_typesize (LIBELFBITS, ELF_T_EHDR, 1);
151 /* Set the program header position. */
152 if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL
153 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN
154 || ehdr->e_type == ET_CORE))
155 (void) __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
156 if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
158 /* Only executables, shared objects, and core files have a program
160 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN
161 && unlikely (ehdr->e_type != ET_CORE))
163 __libelf_seterrno (ELF_E_INVALID_PHDR);
168 if (unlikely (__elf_getphdrnum_rdlock (elf, &phnum) != 0))
171 if (elf->flags & ELF_F_LAYOUT)
173 /* The user is supposed to fill out e_phoff. Use it and
174 e_phnum to determine the maximum extend. */
175 size = MAX ((size_t) size,
177 + elf_typesize (LIBELFBITS, ELF_T_PHDR, phnum));
181 update_if_changed (ehdr->e_phoff,
182 elf_typesize (LIBELFBITS, ELF_T_EHDR, 1),
185 /* We need no alignment here. */
186 size += elf_typesize (LIBELFBITS, ELF_T_PHDR, phnum);
195 assert (elf->state.ELFW(elf,LIBELFBITS).scns.cnt > 0);
197 if (shnum >= SHN_LORESERVE)
199 /* We have to fill in the number of sections in the header
200 of the zeroth section. */
201 Elf_Scn *scn0 = &elf->state.ELFW(elf,LIBELFBITS).scns.data[0];
203 update_if_changed (scn0->shdr.ELFW(e,LIBELFBITS)->sh_size,
204 shnum, scn0->shdr_flags);
207 /* Go over all sections and find out how large they are. */
208 list = &elf->state.ELFW(elf,LIBELFBITS).scns;
210 /* Load the section headers if necessary. This loads the
211 headers for all sections. */
212 if (list->data[1].shdr.ELFW(e,LIBELFBITS) == NULL)
213 (void) __elfw2(LIBELFBITS,getshdr_wrlock) (&list->data[1]);
217 for (size_t cnt = first == true; cnt < list->cnt; ++cnt)
219 Elf_Scn *scn = &list->data[cnt];
220 ElfW2(LIBELFBITS,Shdr) *shdr = scn->shdr.ELFW(e,LIBELFBITS);
223 assert (shdr != NULL);
224 ElfW2(LIBELFBITS,Word) sh_entsize = shdr->sh_entsize;
225 ElfW2(LIBELFBITS,Word) sh_align = shdr->sh_addralign ?: 1;
227 /* Set the sh_entsize value if we can reliably detect it. */
228 switch (shdr->sh_type)
231 sh_entsize = elf_typesize (LIBELFBITS, ELF_T_SYM, 1);
234 sh_entsize = elf_typesize (LIBELFBITS, ELF_T_RELA, 1);
237 /* Only relocatable files can contain section groups. */
238 if (ehdr->e_type != ET_REL)
240 __libelf_seterrno (ELF_E_GROUP_NOT_REL);
244 case SHT_SYMTAB_SHNDX:
245 sh_entsize = elf_typesize (32, ELF_T_WORD, 1);
248 sh_entsize = SH_ENTSIZE_HASH (ehdr);
251 sh_entsize = elf_typesize (LIBELFBITS, ELF_T_DYN, 1);
254 sh_entsize = elf_typesize (LIBELFBITS, ELF_T_REL, 1);
257 sh_entsize = elf_typesize (LIBELFBITS, ELF_T_SYM, 1);
260 sh_entsize = elf_typesize (LIBELFBITS, ELF_T_MOVE, 1);
262 case SHT_SUNW_syminfo:
263 sh_entsize = elf_typesize (LIBELFBITS, ELF_T_SYMINFO, 1);
269 /* If the section header contained the wrong entry size
270 correct it and mark the header as modified. */
271 update_if_changed (shdr->sh_entsize, sh_entsize,
274 if (scn->data_read == 0
275 && __libelf_set_rawdata_wrlock (scn) != 0)
276 /* Something went wrong. The error value is already set. */
279 /* Iterate over all data blocks. */
280 if (list->data[cnt].data_list_rear != NULL)
282 Elf_Data_List *dl = &scn->data_list;
286 Elf_Data *data = &dl->data.d;
287 if (dl == &scn->data_list && data->d_buf == NULL
288 && scn->rawdata.d.d_buf != NULL)
289 data = &scn->rawdata.d;
291 if (unlikely (data->d_version == EV_NONE)
292 || unlikely (data->d_version >= EV_NUM))
294 __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
298 if (unlikely (! powerof2 (data->d_align)))
300 __libelf_seterrno (ELF_E_INVALID_ALIGN);
304 sh_align = MAX (sh_align, data->d_align);
306 if (elf->flags & ELF_F_LAYOUT)
308 /* The user specified the offset and the size.
309 All we have to do is check whether this block
310 fits in the size specified for the section. */
311 if (unlikely ((GElf_Word) (data->d_off
315 __libelf_seterrno (ELF_E_SECTION_TOO_SMALL);
321 /* Determine the padding. */
322 offset = ((offset + data->d_align - 1)
323 & ~(data->d_align - 1));
325 update_if_changed (data->d_off, offset, changed);
327 offset += data->d_size;
330 /* Next data block. */
335 /* Get the size of the section from the raw data. If
336 none is available the value is zero. */
337 offset += scn->rawdata.d.d_size;
339 if (elf->flags & ELF_F_LAYOUT)
341 size = MAX ((GElf_Word) size,
343 + (shdr->sh_type != SHT_NOBITS
344 ? shdr->sh_size : 0));
346 /* The alignment must be a power of two. This is a
347 requirement from the ELF specification. Additionally
348 we test for the alignment of the section being large
349 enough for the largest alignment required by a data
351 if (unlikely (! powerof2 (shdr->sh_addralign))
352 || unlikely (shdr->sh_addralign < sh_align))
354 __libelf_seterrno (ELF_E_INVALID_ALIGN);
360 /* How much alignment do we need for this section. */
361 update_if_changed (shdr->sh_addralign, sh_align,
364 size = (size + sh_align - 1) & ~(sh_align - 1);
365 int offset_changed = 0;
366 update_if_changed (shdr->sh_offset, (GElf_Word) size,
368 changed |= offset_changed;
370 if (offset_changed && scn->data_list_rear == NULL)
372 /* The position of the section in the file
373 changed. Create the section data list. */
374 if (__elf_getdata_rdlock (scn, NULL) == NULL)
378 /* See whether the section size is correct. */
379 update_if_changed (shdr->sh_size, (GElf_Word) offset,
382 if (shdr->sh_type != SHT_NOBITS)
385 scn->flags |= changed;
388 /* Check that the section size is actually a multiple of
390 if (shdr->sh_entsize != 0
391 && unlikely (shdr->sh_size % shdr->sh_entsize != 0)
392 && (elf->flags & ELF_F_PERMISSIVE) == 0)
394 __libelf_seterrno (ELF_E_INVALID_SHENTSIZE);
399 assert (list->next == NULL || list->cnt == list->max);
403 while ((list = list->next) != NULL);
405 /* Store section information. */
406 if (elf->flags & ELF_F_LAYOUT)
408 /* The user is supposed to fill out e_shoff. Use it and
409 e_shnum (or sh_size of the dummy, first section header)
410 to determine the maximum extend. */
411 size = MAX ((GElf_Word) size,
413 + (elf_typesize (LIBELFBITS, ELF_T_SHDR, shnum))));
417 /* Align for section header table.
419 Yes, we use `sizeof' and not `__alignof__' since we do not
420 want to be surprised by architectures with less strict
422 #define SHDR_ALIGN sizeof (ElfW2(LIBELFBITS,Off))
423 size = (size + SHDR_ALIGN - 1) & ~(SHDR_ALIGN - 1);
425 update_if_changed (ehdr->e_shoff, (GElf_Word) size, elf->flags);
426 update_if_changed (ehdr->e_shentsize,
427 elf_typesize (LIBELFBITS, ELF_T_SHDR, 1),
430 /* Account for the section header size. */
431 size += elf_typesize (LIBELFBITS, ELF_T_SHDR, shnum);
435 elf->state.ELFW(elf,LIBELFBITS).ehdr_flags |= ehdr_flags;