1*7304104dSAndroid Build Coastguard Worker /* Update symbol information and section index in symbol table at the
2*7304104dSAndroid Build Coastguard Worker given index.
3*7304104dSAndroid Build Coastguard Worker Copyright (C) 2000, 2001, 2002, 2005, 2009, 2014, 2015 Red Hat, Inc.
4*7304104dSAndroid Build Coastguard Worker This file is part of elfutils.
5*7304104dSAndroid Build Coastguard Worker Written by Ulrich Drepper <[email protected]>, 2000.
6*7304104dSAndroid Build Coastguard Worker
7*7304104dSAndroid Build Coastguard Worker This file is free software; you can redistribute it and/or modify
8*7304104dSAndroid Build Coastguard Worker it under the terms of either
9*7304104dSAndroid Build Coastguard Worker
10*7304104dSAndroid Build Coastguard Worker * the GNU Lesser General Public License as published by the Free
11*7304104dSAndroid Build Coastguard Worker Software Foundation; either version 3 of the License, or (at
12*7304104dSAndroid Build Coastguard Worker your option) any later version
13*7304104dSAndroid Build Coastguard Worker
14*7304104dSAndroid Build Coastguard Worker or
15*7304104dSAndroid Build Coastguard Worker
16*7304104dSAndroid Build Coastguard Worker * the GNU General Public License as published by the Free
17*7304104dSAndroid Build Coastguard Worker Software Foundation; either version 2 of the License, or (at
18*7304104dSAndroid Build Coastguard Worker your option) any later version
19*7304104dSAndroid Build Coastguard Worker
20*7304104dSAndroid Build Coastguard Worker or both in parallel, as here.
21*7304104dSAndroid Build Coastguard Worker
22*7304104dSAndroid Build Coastguard Worker elfutils is distributed in the hope that it will be useful, but
23*7304104dSAndroid Build Coastguard Worker WITHOUT ANY WARRANTY; without even the implied warranty of
24*7304104dSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25*7304104dSAndroid Build Coastguard Worker General Public License for more details.
26*7304104dSAndroid Build Coastguard Worker
27*7304104dSAndroid Build Coastguard Worker You should have received copies of the GNU General Public License and
28*7304104dSAndroid Build Coastguard Worker the GNU Lesser General Public License along with this program. If
29*7304104dSAndroid Build Coastguard Worker not, see <http://www.gnu.org/licenses/>. */
30*7304104dSAndroid Build Coastguard Worker
31*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
32*7304104dSAndroid Build Coastguard Worker # include <config.h>
33*7304104dSAndroid Build Coastguard Worker #endif
34*7304104dSAndroid Build Coastguard Worker
35*7304104dSAndroid Build Coastguard Worker #include <gelf.h>
36*7304104dSAndroid Build Coastguard Worker #include <stdlib.h>
37*7304104dSAndroid Build Coastguard Worker #include <string.h>
38*7304104dSAndroid Build Coastguard Worker
39*7304104dSAndroid Build Coastguard Worker #include "libelfP.h"
40*7304104dSAndroid Build Coastguard Worker
41*7304104dSAndroid Build Coastguard Worker
42*7304104dSAndroid Build Coastguard Worker int
gelf_update_symshndx(Elf_Data * symdata,Elf_Data * shndxdata,int ndx,GElf_Sym * src,Elf32_Word srcshndx)43*7304104dSAndroid Build Coastguard Worker gelf_update_symshndx (Elf_Data *symdata, Elf_Data *shndxdata, int ndx,
44*7304104dSAndroid Build Coastguard Worker GElf_Sym *src, Elf32_Word srcshndx)
45*7304104dSAndroid Build Coastguard Worker {
46*7304104dSAndroid Build Coastguard Worker Elf_Data_Scn *symdata_scn = (Elf_Data_Scn *) symdata;
47*7304104dSAndroid Build Coastguard Worker Elf_Data_Scn *shndxdata_scn = (Elf_Data_Scn *) shndxdata;
48*7304104dSAndroid Build Coastguard Worker Elf_Scn *scn;
49*7304104dSAndroid Build Coastguard Worker Elf32_Word *shndx = NULL;
50*7304104dSAndroid Build Coastguard Worker int result = 0;
51*7304104dSAndroid Build Coastguard Worker
52*7304104dSAndroid Build Coastguard Worker if (symdata == NULL)
53*7304104dSAndroid Build Coastguard Worker return 0;
54*7304104dSAndroid Build Coastguard Worker
55*7304104dSAndroid Build Coastguard Worker if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
56*7304104dSAndroid Build Coastguard Worker {
57*7304104dSAndroid Build Coastguard Worker /* The type of the data better should match. */
58*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_DATA_MISMATCH);
59*7304104dSAndroid Build Coastguard Worker return 0;
60*7304104dSAndroid Build Coastguard Worker }
61*7304104dSAndroid Build Coastguard Worker
62*7304104dSAndroid Build Coastguard Worker scn = symdata_scn->s;
63*7304104dSAndroid Build Coastguard Worker /* We simply have to believe the user that the two sections belong to
64*7304104dSAndroid Build Coastguard Worker the same ELF file. */
65*7304104dSAndroid Build Coastguard Worker rwlock_wrlock (scn->elf->lock);
66*7304104dSAndroid Build Coastguard Worker
67*7304104dSAndroid Build Coastguard Worker /* The user is not required to pass a data descriptor for an extended
68*7304104dSAndroid Build Coastguard Worker section index table. */
69*7304104dSAndroid Build Coastguard Worker if (shndxdata_scn != NULL)
70*7304104dSAndroid Build Coastguard Worker {
71*7304104dSAndroid Build Coastguard Worker if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
72*7304104dSAndroid Build Coastguard Worker {
73*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
74*7304104dSAndroid Build Coastguard Worker goto out;
75*7304104dSAndroid Build Coastguard Worker }
76*7304104dSAndroid Build Coastguard Worker
77*7304104dSAndroid Build Coastguard Worker shndx = &((Elf32_Word *) shndxdata_scn->d.d_buf)[ndx];
78*7304104dSAndroid Build Coastguard Worker }
79*7304104dSAndroid Build Coastguard Worker /* But if s/he does not the extended sectio index must be zero. */
80*7304104dSAndroid Build Coastguard Worker else if (unlikely (srcshndx != 0))
81*7304104dSAndroid Build Coastguard Worker {
82*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
83*7304104dSAndroid Build Coastguard Worker goto out;
84*7304104dSAndroid Build Coastguard Worker }
85*7304104dSAndroid Build Coastguard Worker
86*7304104dSAndroid Build Coastguard Worker if (scn->elf->class == ELFCLASS32)
87*7304104dSAndroid Build Coastguard Worker {
88*7304104dSAndroid Build Coastguard Worker Elf32_Sym *sym;
89*7304104dSAndroid Build Coastguard Worker
90*7304104dSAndroid Build Coastguard Worker /* There is the possibility that the values in the input are
91*7304104dSAndroid Build Coastguard Worker too large. */
92*7304104dSAndroid Build Coastguard Worker if (unlikely (src->st_value > 0xffffffffull)
93*7304104dSAndroid Build Coastguard Worker || unlikely (src->st_size > 0xffffffffull))
94*7304104dSAndroid Build Coastguard Worker {
95*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_DATA);
96*7304104dSAndroid Build Coastguard Worker goto out;
97*7304104dSAndroid Build Coastguard Worker }
98*7304104dSAndroid Build Coastguard Worker
99*7304104dSAndroid Build Coastguard Worker /* Check whether we have to resize the data buffer. */
100*7304104dSAndroid Build Coastguard Worker if (INVALID_NDX (ndx, Elf32_Sym, &symdata_scn->d))
101*7304104dSAndroid Build Coastguard Worker {
102*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
103*7304104dSAndroid Build Coastguard Worker goto out;
104*7304104dSAndroid Build Coastguard Worker }
105*7304104dSAndroid Build Coastguard Worker
106*7304104dSAndroid Build Coastguard Worker sym = &((Elf32_Sym *) symdata_scn->d.d_buf)[ndx];
107*7304104dSAndroid Build Coastguard Worker
108*7304104dSAndroid Build Coastguard Worker #define COPY(name) \
109*7304104dSAndroid Build Coastguard Worker sym->name = src->name
110*7304104dSAndroid Build Coastguard Worker COPY (st_name);
111*7304104dSAndroid Build Coastguard Worker COPY (st_value);
112*7304104dSAndroid Build Coastguard Worker COPY (st_size);
113*7304104dSAndroid Build Coastguard Worker /* Please note that we can simply copy the `st_info' element since
114*7304104dSAndroid Build Coastguard Worker the definitions of ELFxx_ST_BIND and ELFxx_ST_TYPE are the same
115*7304104dSAndroid Build Coastguard Worker for the 64 bit variant. */
116*7304104dSAndroid Build Coastguard Worker COPY (st_info);
117*7304104dSAndroid Build Coastguard Worker COPY (st_other);
118*7304104dSAndroid Build Coastguard Worker COPY (st_shndx);
119*7304104dSAndroid Build Coastguard Worker }
120*7304104dSAndroid Build Coastguard Worker else
121*7304104dSAndroid Build Coastguard Worker {
122*7304104dSAndroid Build Coastguard Worker /* Check whether we have to resize the data buffer. */
123*7304104dSAndroid Build Coastguard Worker if (INVALID_NDX (ndx, Elf64_Sym, &symdata_scn->d))
124*7304104dSAndroid Build Coastguard Worker {
125*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
126*7304104dSAndroid Build Coastguard Worker goto out;
127*7304104dSAndroid Build Coastguard Worker }
128*7304104dSAndroid Build Coastguard Worker
129*7304104dSAndroid Build Coastguard Worker ((Elf64_Sym *) symdata_scn->d.d_buf)[ndx] = *src;
130*7304104dSAndroid Build Coastguard Worker }
131*7304104dSAndroid Build Coastguard Worker
132*7304104dSAndroid Build Coastguard Worker /* Now we can store the section index. */
133*7304104dSAndroid Build Coastguard Worker if (shndx != NULL)
134*7304104dSAndroid Build Coastguard Worker *shndx = srcshndx;
135*7304104dSAndroid Build Coastguard Worker
136*7304104dSAndroid Build Coastguard Worker result = 1;
137*7304104dSAndroid Build Coastguard Worker
138*7304104dSAndroid Build Coastguard Worker /* Mark the section as modified. */
139*7304104dSAndroid Build Coastguard Worker scn->flags |= ELF_F_DIRTY;
140*7304104dSAndroid Build Coastguard Worker
141*7304104dSAndroid Build Coastguard Worker out:
142*7304104dSAndroid Build Coastguard Worker rwlock_unlock (scn->elf->lock);
143*7304104dSAndroid Build Coastguard Worker
144*7304104dSAndroid Build Coastguard Worker return result;
145*7304104dSAndroid Build Coastguard Worker }
146