1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/* Relocate debug information.
Copyright (C) 2005 Red Hat, Inc.
This program is Open Source software; you can redistribute it and/or
modify it under the terms of the Open Software License version 1.0 as
published by the Open Source Initiative.
You should have received a copy of the Open Software License along
with this program; if not, you may obtain a copy of the Open Software
License version 1.0 from http://www.opensource.org/licenses/osl.php or
by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
3001 King Ranch Road, Ukiah, CA 95482. */
#include "libdwflP.h"
typedef uint8_t GElf_Byte;
/* Adjust *VALUE to add the load address of the SHNDX section.
We update the section header in place to cache the result. */
Dwfl_Error
internal_function_def
__libdwfl_relocate_value (Dwfl_Module *mod, size_t symshstrndx,
Elf32_Word shndx, GElf_Addr *value)
{
Elf_Scn *refscn = elf_getscn (mod->symfile->elf, shndx);
GElf_Shdr refshdr_mem, *refshdr = gelf_getshdr (refscn, &refshdr_mem);
if (refshdr == NULL)
return DWFL_E_LIBELF;
if ((refshdr->sh_flags & SHF_ALLOC) && refshdr->sh_addr == 0)
{
/* This is a loaded section. Find its actual
address and update the section header. */
const char *name = elf_strptr (mod->symfile->elf, symshstrndx,
refshdr->sh_name);
if (name == NULL)
return DWFL_E_LIBELF;
if ((*mod->dwfl->callbacks->section_address) (MODCB_ARGS (mod), name,
&refshdr->sh_addr))
return CBFAIL;
if (refshdr->sh_addr == 0)
/* The callback resolved this to zero, indicating it wasn't
really loaded but we don't really care. Mark it so we
don't check it again for the next relocation. */
refshdr->sh_flags &= ~SHF_ALLOC;
/* Update the in-core file's section header to show the final
load address (or unloadedness). This serves as a cache,
so we won't get here again for the same section. */
if (! gelf_update_shdr (refscn, refshdr))
return DWFL_E_LIBELF;
}
/* Apply the adjustment. */
*value += refshdr->sh_addr;
return DWFL_E_NOERROR;
}
Dwfl_Error
internal_function_def
__libdwfl_relocate (Dwfl_Module *mod)
{
assert (mod->isrel);
GElf_Ehdr ehdr_mem;
const GElf_Ehdr *ehdr = gelf_getehdr (mod->debug.elf, &ehdr_mem);
if (ehdr == NULL)
return DWFL_E_LIBELF;
size_t symshstrndx, d_shstrndx;
if (elf_getshstrndx (mod->symfile->elf, &symshstrndx) < 0)
return DWFL_E_LIBELF;
if (mod->symfile == &mod->debug)
d_shstrndx = symshstrndx;
else if (elf_getshstrndx (mod->debug.elf, &d_shstrndx) < 0)
return DWFL_E_LIBELF;
/* Look at each section in the debuginfo file, and process the
relocation sections for debugging sections. */
Dwfl_Error result = DWFL_E_NO_DWARF;
Elf_Scn *scn = NULL;
while ((scn = elf_nextscn (mod->debug.elf, scn)) != NULL)
{
GElf_Shdr shdr_mem;
GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
{
/* It's a relocation section. First, fetch the name of the
section these relocations apply to. */
Elf_Scn *tscn = elf_getscn (mod->debug.elf, shdr->sh_info);
if (tscn == NULL)
return DWFL_E_LIBELF;
GElf_Shdr tshdr_mem;
GElf_Shdr *tshdr = gelf_getshdr (tscn, &tshdr_mem);
const char *tname = elf_strptr (mod->debug.elf, d_shstrndx,
tshdr->sh_name);
if (tname == NULL)
return DWFL_E_LIBELF;
if (! ebl_debugscn_p (mod->ebl, tname))
/* This relocation section is not for a debugging section.
Nothing to do here. */
continue;
/* Fetch the section data that needs the relocations applied. */
Elf_Data *tdata = elf_rawdata (tscn, NULL);
if (tdata == NULL)
return DWFL_E_LIBELF;
/* Apply one relocation. Returns true for any invalid data. */
Dwfl_Error relocate (GElf_Addr offset, const GElf_Sxword *addend,
int rtype, int symndx)
{
/* First, resolve the symbol to an absolute value. */
GElf_Addr value;
inline Dwfl_Error adjust (GElf_Word shndx)
{
return __libdwfl_relocate_value (mod, symshstrndx,
shndx, &value);
}
if (symndx == STN_UNDEF)
/* When strip removes a section symbol referring to a
section moved into the debuginfo file, it replaces
that symbol index in relocs with STN_UNDEF. We
don't actually need the symbol, because those relocs
are always references relative to the nonallocated
debugging sections, which start at zero. */
value = 0;
else
{
GElf_Sym sym_mem;
GElf_Word shndx;
GElf_Sym *sym = gelf_getsymshndx (mod->symdata,
mod->symxndxdata,
symndx, &sym_mem,
&shndx);
if (sym == NULL)
return DWFL_E_LIBELF;
value = sym->st_value;
if (sym->st_shndx != SHN_XINDEX)
shndx = sym->st_shndx;
switch (shndx)
{
case SHN_ABS:
break;
case SHN_UNDEF:
case SHN_COMMON:
return DWFL_E_RELUNDEF;
default:
{
Dwfl_Error error = adjust (shndx);
if (error != DWFL_E_NOERROR)
return error;
break;
}
}
}
/* These are the types we can relocate. */
#define TYPES DO_TYPE (BYTE, Byte) DO_TYPE (HALF, Half) \
DO_TYPE (WORD, Word) DO_TYPE (SWORD, Sword) \
DO_TYPE (XWORD, Xword) DO_TYPE (SXWORD, Sxword)
size_t size;
Elf_Type type = ebl_reloc_simple_type (mod->ebl, rtype);
switch (type)
{
#define DO_TYPE(NAME, Name) \
case ELF_T_##NAME: \
size = sizeof (GElf_##Name); \
break;
TYPES
#undef DO_TYPE
default:
return DWFL_E_BADRELTYPE;
}
if (offset + size >= tdata->d_size)
return DWFL_E_BADRELOFF;
#define DO_TYPE(NAME, Name) GElf_##Name Name;
union { TYPES } tmpbuf;
#undef DO_TYPE
Elf_Data tmpdata =
{
.d_type = type,
.d_buf = &tmpbuf,
.d_size = size,
.d_version = EV_CURRENT,
};
Elf_Data rdata =
{
.d_type = type,
.d_buf = tdata->d_buf + offset,
.d_size = size,
.d_version = EV_CURRENT,
};
/* XXX check for overflow? */
if (addend)
{
/* For the addend form, we have the value already. */
value += *addend;
switch (type)
{
#define DO_TYPE(NAME, Name) \
case ELF_T_##NAME: \
tmpbuf.Name = value; \
break;
TYPES
#undef DO_TYPE
default:
abort ();
}
}
else
{
/* Extract the original value and apply the reloc. */
Elf_Data *d = gelf_xlatetom (mod->main.elf, &tmpdata, &rdata,
ehdr->e_ident[EI_DATA]);
if (d == NULL)
return DWFL_E_LIBELF;
assert (d == &tmpdata);
switch (type)
{
#define DO_TYPE(NAME, Name) \
case ELF_T_##NAME: \
tmpbuf.Name += (GElf_##Name) value; \
break;
TYPES
#undef DO_TYPE
default:
abort ();
}
}
/* Now convert the relocated datum back to the target
format. This will write into rdata.d_buf, which
points into the raw section data being relocated. */
Elf_Data *s = gelf_xlatetof (mod->main.elf, &rdata, &tmpdata,
ehdr->e_ident[EI_DATA]);
if (s == NULL)
return DWFL_E_LIBELF;
assert (s == &rdata);
/* We have applied this relocation! */
return DWFL_E_NOERROR;
}
/* Fetch the relocation section and apply each reloc in it. */
Elf_Data *reldata = elf_getdata (scn, NULL);
if (reldata == NULL)
return DWFL_E_LIBELF;
result = DWFL_E_NOERROR;
size_t nrels = shdr->sh_size / shdr->sh_entsize;
if (shdr->sh_type == SHT_REL)
for (size_t relidx = 0; !result && relidx < nrels; ++relidx)
{
GElf_Rel rel_mem, *r = gelf_getrel (reldata, relidx, &rel_mem);
if (r == NULL)
return DWFL_E_LIBELF;
result = relocate (r->r_offset, NULL,
GELF_R_TYPE (r->r_info),
GELF_R_SYM (r->r_info));
}
else
for (size_t relidx = 0; !result && relidx < nrels; ++relidx)
{
GElf_Rela rela_mem, *r = gelf_getrela (reldata, relidx,
&rela_mem);
if (r == NULL)
return DWFL_E_LIBELF;
result = relocate (r->r_offset, &r->r_addend,
GELF_R_TYPE (r->r_info),
GELF_R_SYM (r->r_info));
}
if (result != DWFL_E_NOERROR)
break;
}
}
return result;
}
|