1 /* Update symbol information and section index in symbol table at the
2 given index.
3 Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
4 This file is part of Red Hat elfutils.
5 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
6
7 Red Hat elfutils is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by the
9 Free Software Foundation; version 2 of the License.
10
11 Red Hat elfutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with Red Hat elfutils; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
19
20 In addition, as a special exception, Red Hat, Inc. gives You the
21 additional right to link the code of Red Hat elfutils with code licensed
22 under any Open Source Initiative certified open source license
23 (http://www.opensource.org/licenses/index.php) which requires the
24 distribution of source code with any binary distribution and to
25 distribute linked combinations of the two. Non-GPL Code permitted under
26 this exception must only link to the code of Red Hat elfutils through
27 those well defined interfaces identified in the file named EXCEPTION
28 found in the source code files (the "Approved Interfaces"). The files
29 of Non-GPL Code may instantiate templates or use macros or inline
30 functions from the Approved Interfaces without causing the resulting
31 work to be covered by the GNU General Public License. Only Red Hat,
32 Inc. may make changes or additions to the list of Approved Interfaces.
33 Red Hat's grant of this exception is conditioned upon your not adding
34 any new exceptions. If you wish to add a new Approved Interface or
35 exception, please contact Red Hat. You must obey the GNU General Public
36 License in all respects for all of the Red Hat elfutils code and other
37 code used in conjunction with Red Hat elfutils except the Non-GPL Code
38 covered by this exception. If you modify this file, you may extend this
39 exception to your version of the file, but you are not obligated to do
40 so. If you do not wish to provide this exception without modification,
41 you must delete this exception statement from your version and license
42 this file solely under the GPL without exception.
43
44 Red Hat elfutils is an included package of the Open Invention Network.
45 An included package of the Open Invention Network is a package for which
46 Open Invention Network licensees cross-license their patents. No patent
47 license is granted, either expressly or impliedly, by designation as an
48 included package. Should you wish to participate in the Open Invention
49 Network licensing program, please visit www.openinventionnetwork.com
50 <http://www.openinventionnetwork.com>. */
51
52 #ifdef HAVE_CONFIG_H
53 # include <config.h>
54 #endif
55
56 #include <gelf.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 #include "libelfP.h"
61
62
63 int
gelf_update_symshndx(symdata,shndxdata,ndx,src,srcshndx)64 gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx)
65 Elf_Data *symdata;
66 Elf_Data *shndxdata;
67 int ndx;
68 GElf_Sym *src;
69 Elf32_Word srcshndx;
70 {
71 Elf_Data_Scn *symdata_scn = (Elf_Data_Scn *) symdata;
72 Elf_Data_Scn *shndxdata_scn = (Elf_Data_Scn *) shndxdata;
73 Elf_Scn *scn;
74 Elf32_Word *shndx = NULL;
75 int result = 0;
76
77 if (symdata == NULL)
78 return 0;
79
80 if (unlikely (ndx < 0))
81 {
82 __libelf_seterrno (ELF_E_INVALID_INDEX);
83 return 0;
84 }
85
86 if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
87 {
88 /* The type of the data better should match. */
89 __libelf_seterrno (ELF_E_DATA_MISMATCH);
90 return 0;
91 }
92
93 scn = symdata_scn->s;
94 /* We simply have to believe the user that the two sections belong to
95 the same ELF file. */
96 rwlock_wrlock (scn->elf->lock);
97
98 /* The user is not required to pass a data descriptor for an extended
99 section index table. */
100 if (shndxdata_scn != NULL)
101 {
102 if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
103 {
104 __libelf_seterrno (ELF_E_INVALID_INDEX);
105 goto out;
106 }
107
108 shndx = &((Elf32_Word *) shndxdata_scn->d.d_buf)[ndx];
109 }
110 /* But if s/he does not the extended sectio index must be zero. */
111 else if (unlikely (srcshndx != 0))
112 {
113 __libelf_seterrno (ELF_E_INVALID_INDEX);
114 goto out;
115 }
116
117 if (scn->elf->class == ELFCLASS32)
118 {
119 Elf32_Sym *sym;
120
121 /* There is the possibility that the values in the input are
122 too large. */
123 if (unlikely (src->st_value > 0xffffffffull)
124 || unlikely (src->st_size > 0xffffffffull))
125 {
126 __libelf_seterrno (ELF_E_INVALID_DATA);
127 goto out;
128 }
129
130 /* Check whether we have to resize the data buffer. */
131 if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
132 {
133 __libelf_seterrno (ELF_E_INVALID_INDEX);
134 goto out;
135 }
136
137 sym = &((Elf32_Sym *) symdata_scn->d.d_buf)[ndx];
138
139 #define COPY(name) \
140 sym->name = src->name
141 COPY (st_name);
142 COPY (st_value);
143 COPY (st_size);
144 /* Please note that we can simply copy the `st_info' element since
145 the definitions of ELFxx_ST_BIND and ELFxx_ST_TYPE are the same
146 for the 64 bit variant. */
147 COPY (st_info);
148 COPY (st_other);
149 COPY (st_shndx);
150 }
151 else
152 {
153 /* Check whether we have to resize the data buffer. */
154 if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
155 {
156 __libelf_seterrno (ELF_E_INVALID_INDEX);
157 goto out;
158 }
159
160 ((Elf64_Sym *) symdata_scn->d.d_buf)[ndx] = *src;
161 }
162
163 /* Now we can store the section index. */
164 if (shndx != NULL)
165 *shndx = srcshndx;
166
167 result = 1;
168
169 /* Mark the section as modified. */
170 scn->flags |= ELF_F_DIRTY;
171
172 out:
173 rwlock_unlock (scn->elf->lock);
174
175 return result;
176 }
177