1441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project/* Return string pointer from string section. 2cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2008 Red Hat, Inc. 3cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng This file is part of Red Hat elfutils. 4441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project Contributed by Ulrich Drepper <drepper@redhat.com>, 1998. 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 <libelf.h> 56441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#include <stddef.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 Projectchar * 62441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Projectelf_strptr (elf, idx, offset) 63441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project Elf *elf; 64441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project size_t idx; 65441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project size_t offset; 66441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project{ 67441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (elf == NULL) 68441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project return NULL; 69441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 70441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (elf->kind != ELF_K_ELF) 71441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 72441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_INVALID_HANDLE); 73441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project return NULL; 74441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 75441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 76441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project rwlock_rdlock (elf->lock); 77441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 78441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project char *result = NULL; 79441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project Elf_Scn *strscn; 80441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 81441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* Find the section in the list. */ 82441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project Elf_ScnList *runp = (elf->class == ELFCLASS32 83441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project || (offsetof (struct Elf, state.elf32.scns) 84441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project == offsetof (struct Elf, state.elf64.scns)) 85441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project ? &elf->state.elf32.scns : &elf->state.elf64.scns); 86441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project while (1) 87441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 88441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (idx < runp->max) 89441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 90441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (idx < runp->cnt) 91441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project strscn = &runp->data[idx]; 92441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project else 93441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 94441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_INVALID_INDEX); 95441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 96441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 97441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project break; 98441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 99441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 100441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project idx -= runp->max; 101441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 102441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project runp = runp->next; 103441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (runp == NULL) 104441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 105441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_INVALID_INDEX); 106441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 107441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 108441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 109441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 110441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (elf->class == ELFCLASS32) 111441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 112441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (unlikely (strscn->shdr.e32->sh_type != SHT_STRTAB)) 113441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 114441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* This is no string section. */ 115441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_INVALID_SECTION); 116441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 117441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 118441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 119441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (unlikely (offset >= strscn->shdr.e32->sh_size)) 120441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 121441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* The given offset is too big, it is beyond this section. */ 122441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_OFFSET_RANGE); 123441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 124441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 125441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 126441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project else 127441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 128441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (unlikely (strscn->shdr.e64->sh_type != SHT_STRTAB)) 129441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 130441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* This is no string section. */ 131441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_INVALID_SECTION); 132441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 133441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 134441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 135441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project if (unlikely (offset >= strscn->shdr.e64->sh_size)) 136441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project { 137441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project /* The given offset is too big, it is beyond this section. */ 138441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project __libelf_seterrno (ELF_E_OFFSET_RANGE); 139441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project goto out; 140441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 141441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project } 142441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 143cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng if (strscn->rawdata_base == NULL && ! strscn->data_read) 144cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng { 145cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng rwlock_unlock (elf->lock); 146cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng rwlock_wrlock (elf->lock); 147cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng if (strscn->rawdata_base == NULL && ! strscn->data_read 148cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng /* Read the section data. */ 149cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng && __libelf_set_rawdata_wrlock (strscn) != 0) 150cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng goto out; 151cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng } 152cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng 153cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng if (likely (strscn->rawdata_base != NULL)) 154cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng result = &strscn->rawdata_base[offset]; 155cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng else 156cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng { 157cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng /* This is a file which is currently created. Use the list of 158cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng data blocks. */ 159cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng struct Elf_Data_List *dl = &strscn->data_list; 160cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng while (dl != NULL) 161cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng { 162cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng if (offset >= (size_t) dl->data.d.d_off 163cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng && offset < dl->data.d.d_off + dl->data.d.d_size) 164cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng { 165cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng result = (char *) dl->data.d.d_buf + (offset - dl->data.d.d_off); 166cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng break; 167cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng } 168441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 169cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng dl = dl->next; 170cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng } 171cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng } 172441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 173441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project out: 174441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project rwlock_unlock (elf->lock); 175441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project 176441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project return result; 177441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project} 178441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source ProjectINTDEF(elf_strptr) 179