Fix FSF address. No exception for libdwarf.
[platform/upstream/elfutils.git] / libdwfl / relocate.c
1 /* Relocate debug information.
2    Copyright (C) 2005 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               inline Dwfl_Error adjust (GElf_Word shndx)
160                 {
161                   return __libdwfl_relocate_value (mod, symshstrndx,
162                                                    shndx, &value);
163                 }
164
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.  */
172                 value = 0;
173               else
174                 {
175                   GElf_Sym sym_mem;
176                   GElf_Word shndx;
177                   GElf_Sym *sym = gelf_getsymshndx (mod->symdata,
178                                                     mod->symxndxdata,
179                                                     symndx, &sym_mem,
180                                                     &shndx);
181                   if (sym == NULL)
182                     return DWFL_E_LIBELF;
183
184                   value = sym->st_value;
185                   if (sym->st_shndx != SHN_XINDEX)
186                     shndx = sym->st_shndx;
187                   switch (shndx)
188                     {
189                     case SHN_ABS:
190                       break;
191
192                     case SHN_UNDEF:
193                     case SHN_COMMON:
194                       return DWFL_E_RELUNDEF;
195
196                     default:
197                       {
198                         Dwfl_Error error = adjust (shndx);
199                         if (error != DWFL_E_NOERROR)
200                           return error;
201                         break;
202                       }
203                     }
204                 }
205
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)
210               size_t size;
211               Elf_Type type = ebl_reloc_simple_type (mod->ebl, rtype);
212               switch (type)
213                 {
214 #define DO_TYPE(NAME, Name)                             \
215                   case ELF_T_##NAME:                    \
216                     size = sizeof (GElf_##Name);        \
217                   break
218                   TYPES;
219 #undef DO_TYPE
220                 default:
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;
226                 }
227
228               if (offset + size >= tdata->d_size)
229                 return DWFL_E_BADRELOFF;
230
231 #define DO_TYPE(NAME, Name) GElf_##Name Name;
232               union { TYPES; } tmpbuf;
233 #undef DO_TYPE
234               Elf_Data tmpdata =
235                 {
236                   .d_type = type,
237                   .d_buf = &tmpbuf,
238                   .d_size = size,
239                   .d_version = EV_CURRENT,
240                 };
241               Elf_Data rdata =
242                 {
243                   .d_type = type,
244                   .d_buf = tdata->d_buf + offset,
245                   .d_size = size,
246                   .d_version = EV_CURRENT,
247                 };
248
249               /* XXX check for overflow? */
250               if (addend)
251                 {
252                   /* For the addend form, we have the value already.  */
253                   value += *addend;
254                   switch (type)
255                     {
256 #define DO_TYPE(NAME, Name)                     \
257                       case ELF_T_##NAME:        \
258                         tmpbuf.Name = value;    \
259                       break
260                       TYPES;
261 #undef DO_TYPE
262                     default:
263                       abort ();
264                     }
265                 }
266               else
267                 {
268                   /* Extract the original value and apply the reloc.  */
269                   Elf_Data *d = gelf_xlatetom (debugfile, &tmpdata, &rdata,
270                                                ehdr->e_ident[EI_DATA]);
271                   if (d == NULL)
272                     return DWFL_E_LIBELF;
273                   assert (d == &tmpdata);
274                   switch (type)
275                     {
276 #define DO_TYPE(NAME, Name)                                     \
277                       case ELF_T_##NAME:                        \
278                         tmpbuf.Name += (GElf_##Name) value;     \
279                       break
280                       TYPES;
281 #undef DO_TYPE
282                     default:
283                       abort ();
284                     }
285                 }
286
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]);
292               if (s == NULL)
293                 return DWFL_E_LIBELF;
294               assert (s == &rdata);
295
296               /* We have applied this relocation!  */
297               return DWFL_E_NOERROR;
298             }
299
300           /* Fetch the relocation section and apply each reloc in it.  */
301           Elf_Data *reldata = elf_getdata (scn, NULL);
302           if (reldata == NULL)
303             return DWFL_E_LIBELF;
304
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)
309               {
310                 GElf_Rel rel_mem, *r = gelf_getrel (reldata, relidx, &rel_mem);
311                 if (r == NULL)
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));
316               }
317           else
318             for (size_t relidx = 0; !result && relidx < nrels; ++relidx)
319               {
320                 GElf_Rela rela_mem, *r = gelf_getrela (reldata, relidx,
321                                                        &rela_mem);
322                 if (r == NULL)
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));
327               }
328           if (result != DWFL_E_NOERROR)
329             break;
330         }
331     }
332
333   return result;
334 }