1 /* Update RELA relocation information at 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
25 #include "libelfP.h"
26
27
28 int
gelf_update_rela(Elf_Data * dst,int ndx,GElf_Rela * src)29 gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
30 {
31 Elf_Data_Scn *data_scn = (Elf_Data_Scn *) dst;
32 Elf_Scn *scn;
33 int result = 0;
34
35 if (dst == NULL)
36 return 0;
37
38 if (unlikely (ndx < 0))
39 {
40 __libelf_seterrno (ELF_E_INVALID_INDEX);
41 return 0;
42 }
43
44 if (unlikely (data_scn->d.d_type != ELF_T_RELA))
45 {
46 /* The type of the data better should match. */
47 __libelf_seterrno (ELF_E_DATA_MISMATCH);
48 return 0;
49 }
50
51 scn = data_scn->s;
52 rwlock_wrlock (scn->elf->lock);
53
54 if (scn->elf->class == ELFCLASS32)
55 {
56 Elf32_Rela *rel;
57
58 /* There is the possibility that the values in the input are
59 too large. */
60 if (unlikely (src->r_offset > 0xffffffffull)
61 || unlikely (GELF_R_SYM (src->r_info) > 0xffffff)
62 || unlikely (GELF_R_TYPE (src->r_info) > 0xff)
63 || unlikely (src->r_addend < -0x80000000ll)
64 || unlikely (src->r_addend > 0x7fffffffll))
65 {
66 __libelf_seterrno (ELF_E_INVALID_DATA);
67 goto out;
68 }
69
70 /* Check whether we have to resize the data buffer. */
71 if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
72 {
73 __libelf_seterrno (ELF_E_INVALID_INDEX);
74 goto out;
75 }
76
77 rel = &((Elf32_Rela *) data_scn->d.d_buf)[ndx];
78
79 rel->r_offset = src->r_offset;
80 rel->r_info = ELF32_R_INFO (GELF_R_SYM (src->r_info),
81 GELF_R_TYPE (src->r_info));
82 rel->r_addend = src->r_addend;
83 }
84 else
85 {
86 /* Check whether we have to resize the data buffer. */
87 if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
88 {
89 __libelf_seterrno (ELF_E_INVALID_INDEX);
90 goto out;
91 }
92
93 ((Elf64_Rela *) data_scn->d.d_buf)[ndx] = *src;
94 }
95
96 result = 1;
97
98 /* Mark the section as modified. */
99 scn->flags |= ELF_F_DIRTY;
100
101 out:
102 rwlock_unlock (scn->elf->lock);
103
104 return result;
105 }
106