propagate from branch 'com.redhat.elfutils.roland.pending' (head e5cfdd13aa39dfae16b9...
[platform/upstream/elfutils.git] / libdwfl / relocate.c
1 /* Relocate debug information.
2    Copyright (C) 2005, 2006 Red Hat, Inc.
3    This file is part of Red Hat elfutils.
4
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.
8
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.
13
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.
17
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.
41
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>.  */
49
50 #include "libdwflP.h"
51
52 typedef uint8_t GElf_Byte;
53
54 /* Adjust *VALUE to add the load address of the SHNDX section.
55    We update the section header in place to cache the result.  */
56
57 Dwfl_Error
58 internal_function_def
59 __libdwfl_relocate_value (Dwfl_Module *mod, size_t symshstrndx,
60                           Elf32_Word shndx, GElf_Addr *value)
61 {
62   Elf_Scn *refscn = elf_getscn (mod->symfile->elf, shndx);
63   GElf_Shdr refshdr_mem, *refshdr = gelf_getshdr (refscn, &refshdr_mem);
64   if (refshdr == NULL)
65     return DWFL_E_LIBELF;
66
67   if ((refshdr->sh_flags & SHF_ALLOC) && refshdr->sh_addr == 0)
68     {
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,
72                                      refshdr->sh_name);
73       if (name == NULL)
74         return DWFL_E_LIBELF;
75
76       if ((*mod->dwfl->callbacks->section_address) (MODCB_ARGS (mod),
77                                                     name, shndx, refshdr,
78                                                     &refshdr->sh_addr))
79         return CBFAIL;
80
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;
86
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))
91         return DWFL_E_LIBELF;
92     }
93
94   /* Apply the adjustment.  */
95   *value += refshdr->sh_addr;
96   return DWFL_E_NOERROR;
97 }
98
99 Dwfl_Error
100 internal_function_def
101 __libdwfl_relocate (Dwfl_Module *mod, Elf *debugfile)
102 {
103   assert (mod->e_type == ET_REL);
104
105   GElf_Ehdr ehdr_mem;
106   const GElf_Ehdr *ehdr = gelf_getehdr (debugfile, &ehdr_mem);
107   if (ehdr == NULL)
108     return DWFL_E_LIBELF;
109
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;
117
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;
121   Elf_Scn *scn = NULL;
122   while ((scn = elf_nextscn (debugfile, scn)) != NULL)
123     {
124       GElf_Shdr shdr_mem;
125       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
126
127       if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
128         {
129           /* It's a relocation section.  First, fetch the name of the
130              section these relocations apply to.  */
131
132           Elf_Scn *tscn = elf_getscn (debugfile, shdr->sh_info);
133           if (tscn == NULL)
134             return DWFL_E_LIBELF;
135
136           GElf_Shdr tshdr_mem;
137           GElf_Shdr *tshdr = gelf_getshdr (tscn, &tshdr_mem);
138           const char *tname = elf_strptr (debugfile, d_shstrndx,
139                                           tshdr->sh_name);
140           if (tname == NULL)
141             return DWFL_E_LIBELF;
142
143           if (! ebl_debugscn_p (mod->ebl, tname))
144             /* This relocation section is not for a debugging section.
145                Nothing to do here.  */
146             continue;
147
148           /* Fetch the section data that needs the relocations applied.  */
149           Elf_Data *tdata = elf_rawdata (tscn, NULL);
150           if (tdata == NULL)
151             return DWFL_E_LIBELF;
152
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)
156             {
157               /* First, resolve the symbol to an absolute value.  */
158               GElf_Addr value;
159
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.  */
167                 value = 0;
168               else
169                 {
170                   GElf_Sym sym;
171                   GElf_Word shndx;
172
173                   if (INTUSE(dwfl_module_getsym) (mod, symndx,
174                                                   &sym, &shndx) == NULL)
175                     return dwfl_errno ();
176
177                   if (shndx == SHN_UNDEF || shndx == SHN_COMMON)
178                     return DWFL_E_RELUNDEF;
179
180                   value = sym.st_value;
181                 }
182
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)
187               size_t size;
188               Elf_Type type = ebl_reloc_simple_type (mod->ebl, rtype);
189               switch (type)
190                 {
191 #define DO_TYPE(NAME, Name)                             \
192                   case ELF_T_##NAME:                    \
193                     size = sizeof (GElf_##Name);        \
194                   break
195                   TYPES;
196 #undef DO_TYPE
197                 default:
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;
203                 }
204
205               if (offset + size >= tdata->d_size)
206                 return DWFL_E_BADRELOFF;
207
208 #define DO_TYPE(NAME, Name) GElf_##Name Name;
209               union { TYPES; } tmpbuf;
210 #undef DO_TYPE
211               Elf_Data tmpdata =
212                 {
213                   .d_type = type,
214                   .d_buf = &tmpbuf,
215                   .d_size = size,
216                   .d_version = EV_CURRENT,
217                 };
218               Elf_Data rdata =
219                 {
220                   .d_type = type,
221                   .d_buf = tdata->d_buf + offset,
222                   .d_size = size,
223                   .d_version = EV_CURRENT,
224                 };
225
226               /* XXX check for overflow? */
227               if (addend)
228                 {
229                   /* For the addend form, we have the value already.  */
230                   value += *addend;
231                   switch (type)
232                     {
233 #define DO_TYPE(NAME, Name)                     \
234                       case ELF_T_##NAME:        \
235                         tmpbuf.Name = value;    \
236                       break
237                       TYPES;
238 #undef DO_TYPE
239                     default:
240                       abort ();
241                     }
242                 }
243               else
244                 {
245                   /* Extract the original value and apply the reloc.  */
246                   Elf_Data *d = gelf_xlatetom (debugfile, &tmpdata, &rdata,
247                                                ehdr->e_ident[EI_DATA]);
248                   if (d == NULL)
249                     return DWFL_E_LIBELF;
250                   assert (d == &tmpdata);
251                   switch (type)
252                     {
253 #define DO_TYPE(NAME, Name)                                     \
254                       case ELF_T_##NAME:                        \
255                         tmpbuf.Name += (GElf_##Name) value;     \
256                       break
257                       TYPES;
258 #undef DO_TYPE
259                     default:
260                       abort ();
261                     }
262                 }
263
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]);
269               if (s == NULL)
270                 return DWFL_E_LIBELF;
271               assert (s == &rdata);
272
273               /* We have applied this relocation!  */
274               return DWFL_E_NOERROR;
275             }
276
277           /* Fetch the relocation section and apply each reloc in it.  */
278           Elf_Data *reldata = elf_getdata (scn, NULL);
279           if (reldata == NULL)
280             return DWFL_E_LIBELF;
281
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)
286               {
287                 GElf_Rel rel_mem, *r = gelf_getrel (reldata, relidx, &rel_mem);
288                 if (r == NULL)
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));
293               }
294           else
295             for (size_t relidx = 0; !result && relidx < nrels; ++relidx)
296               {
297                 GElf_Rela rela_mem, *r = gelf_getrela (reldata, relidx,
298                                                        &rela_mem);
299                 if (r == NULL)
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));
304               }
305           if (result != DWFL_E_NOERROR)
306             break;
307         }
308     }
309
310   return result;
311 }