1 /* Update program header program header table entry.
2 Copyright (C) 2000-2010 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
41 gelf_update_phdr (Elf *elf, int ndx, GElf_Phdr *src)
48 if (unlikely (elf->kind != ELF_K_ELF))
50 __libelf_seterrno (ELF_E_INVALID_HANDLE);
54 rwlock_wrlock (elf->lock);
56 if (elf->class == ELFCLASS32)
58 Elf32_Phdr *phdr = elf->state.elf32.phdr;
60 /* We have to convert the data to the 32 bit format. This might
61 overflow some fields so we have to test for this case before
63 if (unlikely (src->p_offset > 0xffffffffull)
64 || unlikely (src->p_vaddr > 0xffffffffull)
65 || unlikely (src->p_paddr > 0xffffffffull)
66 || unlikely (src->p_filesz > 0xffffffffull)
67 || unlikely (src->p_memsz > 0xffffffffull)
68 || unlikely (src->p_align > 0xffffffffull))
70 __libelf_seterrno (ELF_E_INVALID_DATA);
76 phdr = __elf32_getphdr_wrlock (elf);
78 /* The error number is already set. */
82 /* Test whether the index is ok. */
84 if (ndx >= elf->state.elf32.ehdr->e_phnum
85 && (elf->state.elf32.ehdr->e_phnum != PN_XNUM
86 || __elf_getphdrnum_rdlock (elf, &phnum) != 0
87 || (size_t) ndx >= phnum))
89 __libelf_seterrno (ELF_E_INVALID_INDEX);
93 /* Now correct the pointer to point to the correct element. */
97 phdr->name = src->name
109 Elf64_Phdr *phdr = elf->state.elf64.phdr;
113 phdr = __elf64_getphdr_wrlock (elf);
115 /* The error number is already set. */
119 /* Test whether the index is ok. */
121 if (ndx >= elf->state.elf64.ehdr->e_phnum
122 && (elf->state.elf64.ehdr->e_phnum != PN_XNUM
123 || __elf_getphdrnum_rdlock (elf, &phnum) != 0
124 || (size_t) ndx >= phnum))
126 __libelf_seterrno (ELF_E_INVALID_INDEX);
130 /* Just copy the data. */
131 memcpy (phdr + ndx, src, sizeof (Elf64_Phdr));
134 /* Mark the program header as modified. */
135 elf->state.elf.phdr_flags |= ELF_F_DIRTY;
140 rwlock_unlock (elf->lock);