1 /* Relocate debug information.
2 Copyright (C) 2005 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
5 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
9 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18 In addition, as a special exception, Red Hat, Inc. gives You the
19 additional right to link the code of Red Hat elfutils with code licensed
20 under any Open Source Initiative certified open source license
21 (http://www.opensource.org/licenses/index.php) which requires the
22 distribution of source code with any binary distribution and to
23 distribute linked combinations of the two. Non-GPL Code permitted under
24 this exception must only link to the code of Red Hat elfutils through
25 those well defined interfaces identified in the file named EXCEPTION
26 found in the source code files (the "Approved Interfaces"). The files
27 of Non-GPL Code may instantiate templates or use macros or inline
28 functions from the Approved Interfaces without causing the resulting
29 work to be covered by the GNU General Public License. Only Red Hat,
30 Inc. may make changes or additions to the list of Approved Interfaces.
31 Red Hat's grant of this exception is conditioned upon your not adding
32 any new exceptions. If you wish to add a new Approved Interface or
33 exception, please contact Red Hat. You must obey the GNU General Public
34 License in all respects for all of the Red Hat elfutils code and other
35 code used in conjunction with Red Hat elfutils except the Non-GPL Code
36 covered by this exception. If you modify this file, you may extend this
37 exception to your version of the file, but you are not obligated to do
38 so. If you do not wish to provide this exception without modification,
39 you must delete this exception statement from your version and license
40 this file solely under the GPL without exception.
42 Red Hat elfutils is an included package of the Open Invention Network.
43 An included package of the Open Invention Network is a package for which
44 Open Invention Network licensees cross-license their patents. No patent
45 license is granted, either expressly or impliedly, by designation as an
46 included package. Should you wish to participate in the Open Invention
47 Network licensing program, please visit www.openinventionnetwork.com
48 <http://www.openinventionnetwork.com>. */
52 typedef uint8_t GElf_Byte;
54 /* Adjust *VALUE to add the load address of the SHNDX section.
55 We update the section header in place to cache the result. */
59 __libdwfl_relocate_value (Dwfl_Module *mod, size_t symshstrndx,
60 Elf32_Word shndx, GElf_Addr *value)
62 Elf_Scn *refscn = elf_getscn (mod->symfile->elf, shndx);
63 GElf_Shdr refshdr_mem, *refshdr = gelf_getshdr (refscn, &refshdr_mem);
67 if ((refshdr->sh_flags & SHF_ALLOC) && refshdr->sh_addr == 0)
69 /* This is a loaded section. Find its actual
70 address and update the section header. */
71 const char *name = elf_strptr (mod->symfile->elf, symshstrndx,
76 if ((*mod->dwfl->callbacks->section_address) (MODCB_ARGS (mod),
81 if (refshdr->sh_addr == 0)
82 /* The callback resolved this to zero, indicating it wasn't
83 really loaded but we don't really care. Mark it so we
84 don't check it again for the next relocation. */
85 refshdr->sh_flags &= ~SHF_ALLOC;
87 /* Update the in-core file's section header to show the final
88 load address (or unloadedness). This serves as a cache,
89 so we won't get here again for the same section. */
90 if (! gelf_update_shdr (refscn, refshdr))
94 /* Apply the adjustment. */
95 *value += refshdr->sh_addr;
96 return DWFL_E_NOERROR;
100 internal_function_def
101 __libdwfl_relocate (Dwfl_Module *mod, Elf *debugfile)
103 assert (mod->e_type == ET_REL);
106 const GElf_Ehdr *ehdr = gelf_getehdr (debugfile, &ehdr_mem);
108 return DWFL_E_LIBELF;
110 size_t symshstrndx, d_shstrndx;
111 if (elf_getshstrndx (mod->symfile->elf, &symshstrndx) < 0)
112 return DWFL_E_LIBELF;
113 if (mod->symfile->elf == debugfile)
114 d_shstrndx = symshstrndx;
115 else if (elf_getshstrndx (debugfile, &d_shstrndx) < 0)
116 return DWFL_E_LIBELF;
118 /* Look at each section in the debuginfo file, and process the
119 relocation sections for debugging sections. */
120 Dwfl_Error result = DWFL_E_NO_DWARF;
122 while ((scn = elf_nextscn (debugfile, scn)) != NULL)
125 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
127 if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
129 /* It's a relocation section. First, fetch the name of the
130 section these relocations apply to. */
132 Elf_Scn *tscn = elf_getscn (debugfile, shdr->sh_info);
134 return DWFL_E_LIBELF;
137 GElf_Shdr *tshdr = gelf_getshdr (tscn, &tshdr_mem);
138 const char *tname = elf_strptr (debugfile, d_shstrndx,
141 return DWFL_E_LIBELF;
143 if (! ebl_debugscn_p (mod->ebl, tname))
144 /* This relocation section is not for a debugging section.
145 Nothing to do here. */
148 /* Fetch the section data that needs the relocations applied. */
149 Elf_Data *tdata = elf_rawdata (tscn, NULL);
151 return DWFL_E_LIBELF;
153 /* Apply one relocation. Returns true for any invalid data. */
154 Dwfl_Error relocate (GElf_Addr offset, const GElf_Sxword *addend,
155 int rtype, int symndx)
157 /* First, resolve the symbol to an absolute value. */
159 inline Dwfl_Error adjust (GElf_Word shndx)
161 return __libdwfl_relocate_value (mod, symshstrndx,
165 if (symndx == STN_UNDEF)
166 /* When strip removes a section symbol referring to a
167 section moved into the debuginfo file, it replaces
168 that symbol index in relocs with STN_UNDEF. We
169 don't actually need the symbol, because those relocs
170 are always references relative to the nonallocated
171 debugging sections, which start at zero. */
177 GElf_Sym *sym = gelf_getsymshndx (mod->symdata,
182 return DWFL_E_LIBELF;
184 value = sym->st_value;
185 if (sym->st_shndx != SHN_XINDEX)
186 shndx = sym->st_shndx;
194 return DWFL_E_RELUNDEF;
198 Dwfl_Error error = adjust (shndx);
199 if (error != DWFL_E_NOERROR)
206 /* These are the types we can relocate. */
207 #define TYPES DO_TYPE (BYTE, Byte); DO_TYPE (HALF, Half); \
208 DO_TYPE (WORD, Word); DO_TYPE (SWORD, Sword); \
209 DO_TYPE (XWORD, Xword); DO_TYPE (SXWORD, Sxword)
211 Elf_Type type = ebl_reloc_simple_type (mod->ebl, rtype);
214 #define DO_TYPE(NAME, Name) \
216 size = sizeof (GElf_##Name); \
221 /* This might be because ebl_openbackend failed to find
222 any libebl_CPU.so library. Diagnose that clearly. */
223 if (ebl_get_elfmachine (mod->ebl) == EM_NONE)
224 return DWFL_E_UNKNOWN_MACHINE;
225 return DWFL_E_BADRELTYPE;
228 if (offset + size >= tdata->d_size)
229 return DWFL_E_BADRELOFF;
231 #define DO_TYPE(NAME, Name) GElf_##Name Name;
232 union { TYPES; } tmpbuf;
239 .d_version = EV_CURRENT,
244 .d_buf = tdata->d_buf + offset,
246 .d_version = EV_CURRENT,
249 /* XXX check for overflow? */
252 /* For the addend form, we have the value already. */
256 #define DO_TYPE(NAME, Name) \
258 tmpbuf.Name = value; \
268 /* Extract the original value and apply the reloc. */
269 Elf_Data *d = gelf_xlatetom (debugfile, &tmpdata, &rdata,
270 ehdr->e_ident[EI_DATA]);
272 return DWFL_E_LIBELF;
273 assert (d == &tmpdata);
276 #define DO_TYPE(NAME, Name) \
278 tmpbuf.Name += (GElf_##Name) value; \
287 /* Now convert the relocated datum back to the target
288 format. This will write into rdata.d_buf, which
289 points into the raw section data being relocated. */
290 Elf_Data *s = gelf_xlatetof (debugfile, &rdata, &tmpdata,
291 ehdr->e_ident[EI_DATA]);
293 return DWFL_E_LIBELF;
294 assert (s == &rdata);
296 /* We have applied this relocation! */
297 return DWFL_E_NOERROR;
300 /* Fetch the relocation section and apply each reloc in it. */
301 Elf_Data *reldata = elf_getdata (scn, NULL);
303 return DWFL_E_LIBELF;
305 result = DWFL_E_NOERROR;
306 size_t nrels = shdr->sh_size / shdr->sh_entsize;
307 if (shdr->sh_type == SHT_REL)
308 for (size_t relidx = 0; !result && relidx < nrels; ++relidx)
310 GElf_Rel rel_mem, *r = gelf_getrel (reldata, relidx, &rel_mem);
312 return DWFL_E_LIBELF;
313 result = relocate (r->r_offset, NULL,
314 GELF_R_TYPE (r->r_info),
315 GELF_R_SYM (r->r_info));
318 for (size_t relidx = 0; !result && relidx < nrels; ++relidx)
320 GElf_Rela rela_mem, *r = gelf_getrela (reldata, relidx,
323 return DWFL_E_LIBELF;
324 result = relocate (r->r_offset, &r->r_addend,
325 GELF_R_TYPE (r->r_info),
326 GELF_R_SYM (r->r_info));
328 if (result != DWFL_E_NOERROR)