1 /* Update symbol information in symbol table at the given index.
2 Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 2.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <gelf.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "libelfP.h"
27
28
29 int
gelf_update_sym(data,ndx,src)30 gelf_update_sym (data, ndx, src)
31 Elf_Data *data;
32 int ndx;
33 GElf_Sym *src;
34 {
35 Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
36 Elf_Scn *scn;
37 int result = 0;
38
39 if (data == NULL)
40 return 0;
41
42 if (unlikely (ndx < 0))
43 {
44 __libelf_seterrno (ELF_E_INVALID_INDEX);
45 return 0;
46 }
47
48 if (unlikely (data_scn->d.d_type != ELF_T_SYM))
49 {
50 /* The type of the data better should match. */
51 __libelf_seterrno (ELF_E_DATA_MISMATCH);
52 return 0;
53 }
54
55 scn = data_scn->s;
56 rwlock_wrlock (scn->elf->lock);
57
58 if (scn->elf->class == ELFCLASS32)
59 {
60 Elf32_Sym *sym;
61
62 /* There is the possibility that the values in the input are
63 too large. */
64 if (unlikely (src->st_value > 0xffffffffull)
65 || unlikely (src->st_size > 0xffffffffull))
66 {
67 __libelf_seterrno (ELF_E_INVALID_DATA);
68 goto out;
69 }
70
71 /* Check whether we have to resize the data buffer. */
72 if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
73 {
74 __libelf_seterrno (ELF_E_INVALID_INDEX);
75 goto out;
76 }
77
78 sym = &((Elf32_Sym *) data_scn->d.d_buf)[ndx];
79
80 #define COPY(name) \
81 sym->name = src->name
82 COPY (st_name);
83 COPY (st_value);
84 COPY (st_size);
85 /* Please note that we can simply copy the `st_info' element since
86 the definitions of ELFxx_ST_BIND and ELFxx_ST_TYPE are the same
87 for the 64 bit variant. */
88 COPY (st_info);
89 COPY (st_other);
90 COPY (st_shndx);
91 }
92 else
93 {
94 /* Check whether we have to resize the data buffer. */
95 if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
96 {
97 __libelf_seterrno (ELF_E_INVALID_INDEX);
98 goto out;
99 }
100
101 ((Elf64_Sym *) data_scn->d.d_buf)[ndx] = *src;
102 }
103
104 result = 1;
105
106 /* Mark the section as modified. */
107 scn->flags |= ELF_F_DIRTY;
108
109 out:
110 rwlock_unlock (scn->elf->lock);
111
112 return result;
113 }
114