gelf_update_phdr.c revision cc6695e2684ce93cdf8bd2da63d55d2cf49ff076
1441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project/* Update program header program header table entry. 2441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project Copyright (C) 2000, 2001, 2002 Red Hat, Inc. 3cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng This file is part of Red Hat elfutils. 4441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project Written by Ulrich Drepper <drepper@redhat.com>, 2000. 5441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 6cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Red Hat elfutils is free software; you can redistribute it and/or modify 7cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng it under the terms of the GNU General Public License as published by the 8cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Free Software Foundation; version 2 of the License. 9cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng 10cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Red Hat elfutils is distributed in the hope that it will be useful, but 11cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng WITHOUT ANY WARRANTY; without even the implied warranty of 12cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng General Public License for more details. 14cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng 15cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng You should have received a copy of the GNU General Public License along 16cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng with Red Hat elfutils; if not, write to the Free Software Foundation, 17cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 18cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng 19cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng In addition, as a special exception, Red Hat, Inc. gives You the 20cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng additional right to link the code of Red Hat elfutils with code licensed 21cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng under any Open Source Initiative certified open source license 22cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng (http://www.opensource.org/licenses/index.php) which requires the 23cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng distribution of source code with any binary distribution and to 24cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng distribute linked combinations of the two. Non-GPL Code permitted under 25cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng this exception must only link to the code of Red Hat elfutils through 26cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng those well defined interfaces identified in the file named EXCEPTION 27cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng found in the source code files (the "Approved Interfaces"). The files 28cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng of Non-GPL Code may instantiate templates or use macros or inline 29cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng functions from the Approved Interfaces without causing the resulting 30cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng work to be covered by the GNU General Public License. Only Red Hat, 31cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Inc. may make changes or additions to the list of Approved Interfaces. 32cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Red Hat's grant of this exception is conditioned upon your not adding 33cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng any new exceptions. If you wish to add a new Approved Interface or 34cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng exception, please contact Red Hat. You must obey the GNU General Public 35cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng License in all respects for all of the Red Hat elfutils code and other 36cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng code used in conjunction with Red Hat elfutils except the Non-GPL Code 37cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng covered by this exception. If you modify this file, you may extend this 38cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng exception to your version of the file, but you are not obligated to do 39cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng so. If you do not wish to provide this exception without modification, 40cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng you must delete this exception statement from your version and license 41cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng this file solely under the GPL without exception. 42cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng 43cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Red Hat elfutils is an included package of the Open Invention Network. 44cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng An included package of the Open Invention Network is a package for which 45cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Open Invention Network licensees cross-license their patents. No patent 46cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng license is granted, either expressly or impliedly, by designation as an 47cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng included package. Should you wish to participate in the Open Invention 48cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Network licensing program, please visit www.openinventionnetwork.com 49cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng <http://www.openinventionnetwork.com>. */ 50441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 51441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#ifdef HAVE_CONFIG_H 52441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project# include <config.h> 53441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#endif 54441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 55441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#include <gelf.h> 56441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#include <string.h> 57441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 58441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#include "libelfP.h" 59441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 60441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 61441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Projectint 62441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Projectgelf_update_phdr (Elf *elf, int ndx, GElf_Phdr *src) 63441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project{ 64441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project int result = 0; 65441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 66441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (elf == NULL) 67441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project return 0; 68441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 69441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (unlikely (elf->kind != ELF_K_ELF)) 70441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 71441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_INVALID_HANDLE); 72441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project return 0; 73441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 74441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 75441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project rwlock_wrlock (elf->lock); 76441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 77441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (elf->class == ELFCLASS32) 78441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 79441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project Elf32_Phdr *phdr = elf->state.elf32.phdr; 80441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 81441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* We have to convert the data to the 32 bit format. This might 82441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project overflow some fields so we have to test for this case before 83441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project copying. */ 84441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (unlikely (src->p_offset > 0xffffffffull) 85441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project || unlikely (src->p_vaddr > 0xffffffffull) 86441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project || unlikely (src->p_paddr > 0xffffffffull) 87441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project || unlikely (src->p_filesz > 0xffffffffull) 88441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project || unlikely (src->p_memsz > 0xffffffffull) 89441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project || unlikely (src->p_align > 0xffffffffull)) 90441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 91441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_INVALID_DATA); 92441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 93441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 94441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 95441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (phdr == NULL) 96441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 97cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng phdr = __elf32_getphdr_wrlock (elf); 98441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (phdr == NULL) 99441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* The error number is already set. */ 100441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 101441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 102441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 103441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* Test whether the index is ok. */ 104441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (unlikely (ndx >= elf->state.elf32.ehdr->e_phnum)) 105441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 106441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_INVALID_INDEX); 107441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 108441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 109441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 110441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* Now correct the pointer to point to the correct element. */ 111441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project phdr += ndx; 112441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 113441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#define COPY(name) \ 114441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project phdr->name = src->name 115441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project COPY (p_type); 116441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project COPY (p_offset); 117441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project COPY (p_vaddr); 118441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project COPY (p_paddr); 119441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project COPY (p_filesz); 120441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project COPY (p_memsz); 121441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project COPY (p_flags); 122441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project COPY (p_align); 123441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 124441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project else 125441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 126441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project Elf64_Phdr *phdr = elf->state.elf64.phdr; 127441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 128441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (phdr == NULL) 129441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 130cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng phdr = __elf64_getphdr_wrlock (elf); 131441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (phdr == NULL) 132441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* The error number is already set. */ 133441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 134441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 135441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 136441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* Test whether the index is ok. */ 137441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (unlikely (ndx >= elf->state.elf64.ehdr->e_phnum)) 138441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 139441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_INVALID_INDEX); 140441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 141441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 142441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 143441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* Just copy the data. */ 144441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project memcpy (phdr + ndx, src, sizeof (Elf64_Phdr)); 145441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 146441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 147441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project result = 1; 148441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 149441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project out: 150441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project rwlock_unlock (elf->lock); 151441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 152441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project return result; 153441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project} 154