1 /* Relocate debug information.
2 Copyright (C) 2005, 2006 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;
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. */
160 if (symndx == STN_UNDEF)
161 /* When strip removes a section symbol referring to a
162 section moved into the debuginfo file, it replaces
163 that symbol index in relocs with STN_UNDEF. We
164 don't actually need the symbol, because those relocs
165 are always references relative to the nonallocated
166 debugging sections, which start at zero. */
173 if (INTUSE(dwfl_module_getsym) (mod, symndx,
174 &sym, &shndx) == NULL)
175 return dwfl_errno ();
177 if (shndx == SHN_UNDEF || shndx == SHN_COMMON)
178 return DWFL_E_RELUNDEF;
180 value = sym.st_value;
183 /* These are the types we can relocate. */
184 #define TYPES DO_TYPE (BYTE, Byte); DO_TYPE (HALF, Half); \
185 DO_TYPE (WORD, Word); DO_TYPE (SWORD, Sword); \
186 DO_TYPE (XWORD, Xword); DO_TYPE (SXWORD, Sxword)
188 Elf_Type type = ebl_reloc_simple_type (mod->ebl, rtype);
191 #define DO_TYPE(NAME, Name) \
193 size = sizeof (GElf_##Name); \
198 /* This might be because ebl_openbackend failed to find
199 any libebl_CPU.so library. Diagnose that clearly. */
200 if (ebl_get_elfmachine (mod->ebl) == EM_NONE)
201 return DWFL_E_UNKNOWN_MACHINE;
202 return DWFL_E_BADRELTYPE;
205 if (offset + size >= tdata->d_size)
206 return DWFL_E_BADRELOFF;
208 #define DO_TYPE(NAME, Name) GElf_##Name Name;
209 union { TYPES; } tmpbuf;
216 .d_version = EV_CURRENT,
221 .d_buf = tdata->d_buf + offset,
223 .d_version = EV_CURRENT,
226 /* XXX check for overflow? */
229 /* For the addend form, we have the value already. */
233 #define DO_TYPE(NAME, Name) \
235 tmpbuf.Name = value; \
245 /* Extract the original value and apply the reloc. */
246 Elf_Data *d = gelf_xlatetom (debugfile, &tmpdata, &rdata,
247 ehdr->e_ident[EI_DATA]);
249 return DWFL_E_LIBELF;
250 assert (d == &tmpdata);
253 #define DO_TYPE(NAME, Name) \
255 tmpbuf.Name += (GElf_##Name) value; \
264 /* Now convert the relocated datum back to the target
265 format. This will write into rdata.d_buf, which
266 points into the raw section data being relocated. */
267 Elf_Data *s = gelf_xlatetof (debugfile, &rdata, &tmpdata,
268 ehdr->e_ident[EI_DATA]);
270 return DWFL_E_LIBELF;
271 assert (s == &rdata);
273 /* We have applied this relocation! */
274 return DWFL_E_NOERROR;
277 /* Fetch the relocation section and apply each reloc in it. */
278 Elf_Data *reldata = elf_getdata (scn, NULL);
280 return DWFL_E_LIBELF;
282 result = DWFL_E_NOERROR;
283 size_t nrels = shdr->sh_size / shdr->sh_entsize;
284 if (shdr->sh_type == SHT_REL)
285 for (size_t relidx = 0; !result && relidx < nrels; ++relidx)
287 GElf_Rel rel_mem, *r = gelf_getrel (reldata, relidx, &rel_mem);
289 return DWFL_E_LIBELF;
290 result = relocate (r->r_offset, NULL,
291 GELF_R_TYPE (r->r_info),
292 GELF_R_SYM (r->r_info));
295 for (size_t relidx = 0; !result && relidx < nrels; ++relidx)
297 GElf_Rela rela_mem, *r = gelf_getrela (reldata, relidx,
300 return DWFL_E_LIBELF;
301 result = relocate (r->r_offset, &r->r_addend,
302 GELF_R_TYPE (r->r_info),
303 GELF_R_SYM (r->r_info));
305 if (result != DWFL_E_NOERROR)