Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Update program header program header table entry. |
Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 2 | Copyright (C) 2000-2010 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2000. |
| 5 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 6 | This file is free software; you can redistribute it and/or modify |
| 7 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 8 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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 |
| 12 | |
| 13 | or |
| 14 | |
| 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 |
| 18 | |
| 19 | or both in parallel, as here. |
| 20 | |
| 21 | elfutils is distributed in the hope that it will be useful, but |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 25 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | # include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <gelf.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include "libelfP.h" |
| 38 | |
| 39 | |
| 40 | int |
| 41 | gelf_update_phdr (Elf *elf, int ndx, GElf_Phdr *src) |
| 42 | { |
| 43 | int result = 0; |
| 44 | |
| 45 | if (elf == NULL) |
| 46 | return 0; |
| 47 | |
| 48 | if (unlikely (elf->kind != ELF_K_ELF)) |
| 49 | { |
| 50 | __libelf_seterrno (ELF_E_INVALID_HANDLE); |
| 51 | return 0; |
| 52 | } |
| 53 | |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 54 | rwlock_wrlock (elf->lock); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 55 | |
| 56 | if (elf->class == ELFCLASS32) |
| 57 | { |
| 58 | Elf32_Phdr *phdr = elf->state.elf32.phdr; |
| 59 | |
| 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 |
| 62 | copying. */ |
| 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)) |
| 69 | { |
| 70 | __libelf_seterrno (ELF_E_INVALID_DATA); |
| 71 | goto out; |
| 72 | } |
| 73 | |
| 74 | if (phdr == NULL) |
| 75 | { |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 76 | phdr = __elf32_getphdr_wrlock (elf); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 77 | if (phdr == NULL) |
| 78 | /* The error number is already set. */ |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | /* Test whether the index is ok. */ |
Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 83 | size_t phnum; |
| 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)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 88 | { |
| 89 | __libelf_seterrno (ELF_E_INVALID_INDEX); |
| 90 | goto out; |
| 91 | } |
| 92 | |
| 93 | /* Now correct the pointer to point to the correct element. */ |
| 94 | phdr += ndx; |
| 95 | |
| 96 | #define COPY(name) \ |
| 97 | phdr->name = src->name |
| 98 | COPY (p_type); |
| 99 | COPY (p_offset); |
| 100 | COPY (p_vaddr); |
| 101 | COPY (p_paddr); |
| 102 | COPY (p_filesz); |
| 103 | COPY (p_memsz); |
| 104 | COPY (p_flags); |
| 105 | COPY (p_align); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | Elf64_Phdr *phdr = elf->state.elf64.phdr; |
| 110 | |
| 111 | if (phdr == NULL) |
| 112 | { |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 113 | phdr = __elf64_getphdr_wrlock (elf); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 114 | if (phdr == NULL) |
| 115 | /* The error number is already set. */ |
| 116 | goto out; |
| 117 | } |
| 118 | |
| 119 | /* Test whether the index is ok. */ |
Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 120 | size_t phnum; |
| 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)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 125 | { |
| 126 | __libelf_seterrno (ELF_E_INVALID_INDEX); |
| 127 | goto out; |
| 128 | } |
| 129 | |
| 130 | /* Just copy the data. */ |
| 131 | memcpy (phdr + ndx, src, sizeof (Elf64_Phdr)); |
| 132 | } |
| 133 | |
Ulrich Drepper | d763578 | 2010-06-14 14:18:23 -0700 | [diff] [blame] | 134 | /* Mark the program header as modified. */ |
| 135 | elf->state.elf.phdr_flags |= ELF_F_DIRTY; |
| 136 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 137 | result = 1; |
| 138 | |
| 139 | out: |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 140 | rwlock_unlock (elf->lock); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 141 | |
| 142 | return result; |
| 143 | } |