125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Update symbol information and section index in symbol table at the 225b3c049e70834cf33790a28643ab058b507b35cBen Cheng given index. 325b3c049e70834cf33790a28643ab058b507b35cBen Cheng Copyright (C) 2000, 2001, 2002 Red Hat, Inc. 425b3c049e70834cf33790a28643ab058b507b35cBen Cheng This file is part of Red Hat elfutils. 525b3c049e70834cf33790a28643ab058b507b35cBen Cheng Written by Ulrich Drepper <drepper@redhat.com>, 2000. 625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 725b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat elfutils is free software; you can redistribute it and/or modify 825b3c049e70834cf33790a28643ab058b507b35cBen Cheng it under the terms of the GNU General Public License as published by the 925b3c049e70834cf33790a28643ab058b507b35cBen Cheng Free Software Foundation; version 2 of the License. 1025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 1125b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat elfutils is distributed in the hope that it will be useful, but 1225b3c049e70834cf33790a28643ab058b507b35cBen Cheng WITHOUT ANY WARRANTY; without even the implied warranty of 1325b3c049e70834cf33790a28643ab058b507b35cBen Cheng MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1425b3c049e70834cf33790a28643ab058b507b35cBen Cheng General Public License for more details. 1525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 1625b3c049e70834cf33790a28643ab058b507b35cBen Cheng You should have received a copy of the GNU General Public License along 1725b3c049e70834cf33790a28643ab058b507b35cBen Cheng with Red Hat elfutils; if not, write to the Free Software Foundation, 1825b3c049e70834cf33790a28643ab058b507b35cBen Cheng Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 1925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 2025b3c049e70834cf33790a28643ab058b507b35cBen Cheng In addition, as a special exception, Red Hat, Inc. gives You the 2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng additional right to link the code of Red Hat elfutils with code licensed 2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng under any Open Source Initiative certified open source license 2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng (http://www.opensource.org/licenses/index.php) which requires the 2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng distribution of source code with any binary distribution and to 2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng distribute linked combinations of the two. Non-GPL Code permitted under 2625b3c049e70834cf33790a28643ab058b507b35cBen Cheng this exception must only link to the code of Red Hat elfutils through 2725b3c049e70834cf33790a28643ab058b507b35cBen Cheng those well defined interfaces identified in the file named EXCEPTION 2825b3c049e70834cf33790a28643ab058b507b35cBen Cheng found in the source code files (the "Approved Interfaces"). The files 2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng of Non-GPL Code may instantiate templates or use macros or inline 3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng functions from the Approved Interfaces without causing the resulting 3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng work to be covered by the GNU General Public License. Only Red Hat, 3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng Inc. may make changes or additions to the list of Approved Interfaces. 3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat's grant of this exception is conditioned upon your not adding 3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng any new exceptions. If you wish to add a new Approved Interface or 3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng exception, please contact Red Hat. You must obey the GNU General Public 3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng License in all respects for all of the Red Hat elfutils code and other 3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng code used in conjunction with Red Hat elfutils except the Non-GPL Code 3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng covered by this exception. If you modify this file, you may extend this 3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng exception to your version of the file, but you are not obligated to do 4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng so. If you do not wish to provide this exception without modification, 4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng you must delete this exception statement from your version and license 4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng this file solely under the GPL without exception. 4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat elfutils is an included package of the Open Invention Network. 4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng An included package of the Open Invention Network is a package for which 4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng Open Invention Network licensees cross-license their patents. No patent 4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng license is granted, either expressly or impliedly, by designation as an 4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng included package. Should you wish to participate in the Open Invention 4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng Network licensing program, please visit www.openinventionnetwork.com 5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng <http://www.openinventionnetwork.com>. */ 5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifdef HAVE_CONFIG_H 5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng# include <config.h> 5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif 5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <gelf.h> 5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <stdlib.h> 5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <string.h> 5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libelfP.h" 6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6325b3c049e70834cf33790a28643ab058b507b35cBen Chengint 6425b3c049e70834cf33790a28643ab058b507b35cBen Chenggelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx) 6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng Elf_Data *symdata; 6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng Elf_Data *shndxdata; 6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng int ndx; 6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng GElf_Sym *src; 6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng Elf32_Word srcshndx; 7025b3c049e70834cf33790a28643ab058b507b35cBen Cheng{ 7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng Elf_Data_Scn *symdata_scn = (Elf_Data_Scn *) symdata; 7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng Elf_Data_Scn *shndxdata_scn = (Elf_Data_Scn *) shndxdata; 7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng Elf_Scn *scn; 7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng Elf32_Word *shndx = NULL; 7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng int result = 0; 7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (symdata == NULL) 7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng return 0; 7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (unlikely (ndx < 0)) 8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libelf_seterrno (ELF_E_INVALID_INDEX); 8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng return 0; 8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (unlikely (symdata_scn->d.d_type != ELF_T_SYM)) 8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* The type of the data better should match. */ 8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libelf_seterrno (ELF_E_DATA_MISMATCH); 9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng return 0; 9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng 9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng scn = symdata_scn->s; 9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* We simply have to believe the user that the two sections belong to 9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng the same ELF file. */ 9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng rwlock_wrlock (scn->elf->lock); 9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* The user is not required to pass a data descriptor for an extended 9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng section index table. */ 10025b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (shndxdata_scn != NULL) 10125b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 10225b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size)) 10325b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 10425b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libelf_seterrno (ELF_E_INVALID_INDEX); 10525b3c049e70834cf33790a28643ab058b507b35cBen Cheng goto out; 10625b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 10825b3c049e70834cf33790a28643ab058b507b35cBen Cheng shndx = &((Elf32_Word *) shndxdata_scn->d.d_buf)[ndx]; 10925b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* But if s/he does not the extended sectio index must be zero. */ 11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng else if (unlikely (srcshndx != 0)) 11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 11325b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libelf_seterrno (ELF_E_INVALID_INDEX); 11425b3c049e70834cf33790a28643ab058b507b35cBen Cheng goto out; 11525b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 11625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11725b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (scn->elf->class == ELFCLASS32) 11825b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 11925b3c049e70834cf33790a28643ab058b507b35cBen Cheng Elf32_Sym *sym; 12025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 12125b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* There is the possibility that the values in the input are 12225b3c049e70834cf33790a28643ab058b507b35cBen Cheng too large. */ 12325b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (unlikely (src->st_value > 0xffffffffull) 12425b3c049e70834cf33790a28643ab058b507b35cBen Cheng || unlikely (src->st_size > 0xffffffffull)) 12525b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 12625b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libelf_seterrno (ELF_E_INVALID_DATA); 12725b3c049e70834cf33790a28643ab058b507b35cBen Cheng goto out; 12825b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 12925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 13025b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Check whether we have to resize the data buffer. */ 13125b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size)) 13225b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 13325b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libelf_seterrno (ELF_E_INVALID_INDEX); 13425b3c049e70834cf33790a28643ab058b507b35cBen Cheng goto out; 13525b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 13625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 13725b3c049e70834cf33790a28643ab058b507b35cBen Cheng sym = &((Elf32_Sym *) symdata_scn->d.d_buf)[ndx]; 13825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 13925b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define COPY(name) \ 14025b3c049e70834cf33790a28643ab058b507b35cBen Cheng sym->name = src->name 14125b3c049e70834cf33790a28643ab058b507b35cBen Cheng COPY (st_name); 14225b3c049e70834cf33790a28643ab058b507b35cBen Cheng COPY (st_value); 14325b3c049e70834cf33790a28643ab058b507b35cBen Cheng COPY (st_size); 14425b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Please note that we can simply copy the `st_info' element since 14525b3c049e70834cf33790a28643ab058b507b35cBen Cheng the definitions of ELFxx_ST_BIND and ELFxx_ST_TYPE are the same 14625b3c049e70834cf33790a28643ab058b507b35cBen Cheng for the 64 bit variant. */ 14725b3c049e70834cf33790a28643ab058b507b35cBen Cheng COPY (st_info); 14825b3c049e70834cf33790a28643ab058b507b35cBen Cheng COPY (st_other); 14925b3c049e70834cf33790a28643ab058b507b35cBen Cheng COPY (st_shndx); 15025b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 15125b3c049e70834cf33790a28643ab058b507b35cBen Cheng else 15225b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 15325b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Check whether we have to resize the data buffer. */ 15425b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size)) 15525b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 15625b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libelf_seterrno (ELF_E_INVALID_INDEX); 15725b3c049e70834cf33790a28643ab058b507b35cBen Cheng goto out; 15825b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 15925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 16025b3c049e70834cf33790a28643ab058b507b35cBen Cheng ((Elf64_Sym *) symdata_scn->d.d_buf)[ndx] = *src; 16125b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 16225b3c049e70834cf33790a28643ab058b507b35cBen Cheng 16325b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Now we can store the section index. */ 16425b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (shndx != NULL) 16525b3c049e70834cf33790a28643ab058b507b35cBen Cheng *shndx = srcshndx; 16625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 16725b3c049e70834cf33790a28643ab058b507b35cBen Cheng result = 1; 16825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 16925b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Mark the section as modified. */ 17025b3c049e70834cf33790a28643ab058b507b35cBen Cheng scn->flags |= ELF_F_DIRTY; 17125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 17225b3c049e70834cf33790a28643ab058b507b35cBen Cheng out: 17325b3c049e70834cf33790a28643ab058b507b35cBen Cheng rwlock_unlock (scn->elf->lock); 17425b3c049e70834cf33790a28643ab058b507b35cBen Cheng 17525b3c049e70834cf33790a28643ab058b507b35cBen Cheng return result; 17625b3c049e70834cf33790a28643ab058b507b35cBen Cheng} 177