OSDN Git Service

78434167be83db19e52840d0c12422c005fda924
[uclinux-h8/uClibc.git] / ldso / ldso / metag / elfinterp.c
1 /*
2  * Meta ELF shared library loader support.
3  *
4  * Program to load an elf binary on a linux system, and run it.
5  * References to symbols in sharable libraries can be resolved
6  * by either an ELF sharable library or a linux style of shared
7  * library.
8  *
9  * Copyright (C) 2013, Imagination Technologies Ltd.
10  *
11  * Licensed under LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
12  */
13
14 #include "ldso.h"
15
16 /* Defined in resolve.S. */
17 extern int _dl_linux_resolve(void);
18
19 static inline unsigned long __get_unaligned_reloc(unsigned long *addr)
20 {
21         char *rel_addr = (char *)addr;
22         unsigned long val;
23
24         val = *rel_addr++ & 0xff;
25         val |= (*rel_addr++ << 8) & 0x0000ff00;
26         val |= (*rel_addr++ << 16) & 0x00ff0000;
27         val |= (*rel_addr++ << 24) & 0xff000000;
28
29         return val;
30 }
31
32 static inline void __put_unaligned_reloc(unsigned long *addr,
33                                          unsigned long val)
34 {
35         char *rel_addr = (char *)addr;
36
37         *rel_addr++ = (val & 0x000000ff);
38         *rel_addr++ = ((val & 0x0000ff00) >> 8);
39         *rel_addr++ = ((val & 0x00ff0000) >> 16);
40         *rel_addr++ = ((val & 0xff000000) >> 24);
41 }
42
43 unsigned long
44 _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
45 {
46         int reloc_type;
47         int symtab_index;
48         char *strtab;
49         char *symname;
50         char *new_addr;
51         char *rel_addr;
52         char **got_addr;
53         Elf32_Sym *symtab;
54         ELF_RELOC *this_reloc;
55         unsigned long instr_addr;
56
57         rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
58
59         this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
60         reloc_type = ELF32_R_TYPE(this_reloc->r_info);
61         symtab_index = ELF32_R_SYM(this_reloc->r_info);
62
63         symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
64         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
65         symname = strtab + symtab[symtab_index].st_name;
66
67         if (unlikely(reloc_type != R_METAG_JMP_SLOT)) {
68                 _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
69                             _dl_progname);
70                 _dl_exit(1);
71         }
72
73         /* Address of the jump instruction to fix up. */
74         instr_addr = ((unsigned long)this_reloc->r_offset +
75                       (unsigned long)tpnt->loadaddr);
76         got_addr = (char **)instr_addr;
77
78         /* Get the address of the GOT entry. */
79         new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt,
80                         ELF_RTYPE_CLASS_PLT, NULL);
81         if (unlikely(!new_addr)) {
82                 _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
83                 _dl_exit(1);
84         }
85
86 #if defined (__SUPPORT_LD_DEBUG__)
87         if (_dl_debug_bindings) {
88                 _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
89                 if (_dl_debug_detail)
90                         _dl_dprintf(_dl_debug_file,
91                                     "\n\tpatched: %x ==> %x @ %x\n",
92                                     *got_addr, new_addr, got_addr);
93         }
94         if (!_dl_debug_nofixups) {
95                 *got_addr = new_addr;
96         }
97 #else
98         *got_addr = new_addr;
99 #endif
100
101         return (unsigned long)new_addr;
102 }
103
104 static int
105 _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
106           unsigned long rel_addr, unsigned long rel_size,
107           int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
108                            ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
109 {
110         int symtab_index;
111         unsigned int i;
112         char *strtab;
113         Elf32_Sym *symtab;
114         ELF_RELOC *rpnt;
115
116         /* Parse the relocation information. */
117         rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
118         rel_size /= sizeof(ELF_RELOC);
119
120         symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
121         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
122
123         for (i = 0; i < rel_size; i++, rpnt++) {
124                 int res;
125
126                 symtab_index = ELF32_R_SYM(rpnt->r_info);
127
128                 debug_sym(symtab, strtab, symtab_index);
129                 debug_reloc(symtab, strtab, rpnt);
130
131                 /* Pass over to actual relocation function. */
132                 res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
133
134                 if (res == 0)
135                         continue;
136
137                 _dl_dprintf(2, "\n%s: ", _dl_progname);
138
139                 if (symtab_index)
140                         _dl_dprintf(2, "symbol '%s': ",
141                                     strtab + symtab[symtab_index].st_name);
142
143                 if (unlikely(res < 0)) {
144                         int reloc_type = ELF32_R_TYPE(rpnt->r_info);
145
146 #if defined (__SUPPORT_LD_DEBUG__)
147                         _dl_dprintf(2, "can't handle reloc type %s\n",
148                                     _dl_reltypes(reloc_type));
149 #else
150                         _dl_dprintf(2, "can't handle reloc type %x\n",
151                                     reloc_type);
152 #endif
153                         _dl_exit(-res);
154                 } else if (unlikely(res > 0)) {
155                         _dl_dprintf(2, "can't resolve symbol\n");
156                         return res;
157                 }
158         }
159
160         return 0;
161 }
162
163 static int
164 _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
165              ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
166 {
167         int reloc_type;
168         int symtab_index;
169         char *symname = NULL;
170         unsigned long *reloc_addr;
171         unsigned long symbol_addr;
172 #if defined (__SUPPORT_LD_DEBUG__)
173         unsigned long old_val;
174 #endif
175
176         reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
177         reloc_type = ELF32_R_TYPE(rpnt->r_info);
178         symtab_index = ELF32_R_SYM(rpnt->r_info);
179         symbol_addr = 0;
180         symname = strtab + symtab[symtab_index].st_name;
181
182         if (symtab_index) {
183                 if (symtab[symtab_index].st_shndx != SHN_UNDEF &&
184                     ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_LOCAL) {
185                         symbol_addr = (unsigned long)tpnt->loadaddr;
186                 } else {
187                   symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
188                                                              elf_machine_type_class(reloc_type), NULL);
189                 }
190
191                 if (unlikely(!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
192                         _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
193                         _dl_exit(1);
194                 };
195
196                 symbol_addr += rpnt->r_addend;
197         }
198
199 #if defined (__SUPPORT_LD_DEBUG__)
200         if (reloc_type != R_METAG_NONE)
201                 old_val = __get_unaligned_reloc(reloc_addr);
202 #endif
203
204         switch (reloc_type) {
205                 case R_METAG_NONE:
206                         break;
207                 case R_METAG_GLOB_DAT:
208                 case R_METAG_JMP_SLOT:
209                 case R_METAG_ADDR32:
210                         __put_unaligned_reloc(reloc_addr, symbol_addr);
211                         break;
212                 case R_METAG_COPY:
213 #if defined (__SUPPORT_LD_DEBUG__)
214                         if (_dl_debug_move)
215                                 _dl_dprintf(_dl_debug_file,
216                                             "\t%s move %d bytes from %x to %x\n",
217                                             symname, symtab[symtab_index].st_size,
218                                             symbol_addr, reloc_addr);
219 #endif
220
221                         _dl_memcpy((char *)reloc_addr,
222                                    (char *)symbol_addr,
223                                    symtab[symtab_index].st_size);
224                         break;
225                 case R_METAG_RELATIVE:
226                         __put_unaligned_reloc(reloc_addr,
227                                               (unsigned long)tpnt->loadaddr +
228                                               rpnt->r_addend);
229                         break;
230                 default:
231                         return -1;      /* Calls _dl_exit(1). */
232         }
233
234 #if defined (__SUPPORT_LD_DEBUG__)
235         if (_dl_debug_reloc && _dl_debug_detail &&
236             (reloc_type != R_METAG_NONE)) {
237                 unsigned long new_val = __get_unaligned_reloc(reloc_addr);
238                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
239                             old_val, new_val, reloc_addr);
240         }
241 #endif
242
243         return 0;
244 }
245
246 static int
247 _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
248                   ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
249 {
250         int reloc_type;
251         unsigned long *reloc_addr;
252 #if defined (__SUPPORT_LD_DEBUG__)
253         unsigned long old_val;
254 #endif
255
256         reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
257         reloc_type = ELF32_R_TYPE(rpnt->r_info);
258
259 #if defined (__SUPPORT_LD_DEBUG__)
260         old_val = *reloc_addr;
261 #endif
262
263         switch (reloc_type) {
264                 case R_METAG_NONE:
265                         break;
266                 case R_METAG_JMP_SLOT:
267                         *reloc_addr += (unsigned long)tpnt->loadaddr;
268                         break;
269                 default:
270                         return -1;      /* Calls _dl_exit(1). */
271         }
272
273 #if defined (__SUPPORT_LD_DEBUG__)
274         if (_dl_debug_reloc && _dl_debug_detail)
275                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
276                             old_val, *reloc_addr, reloc_addr);
277 #endif
278
279         return 0;
280 }
281
282 /* External interface to the generic part of the dynamic linker. */
283
284 void
285 _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
286                                       unsigned long rel_addr,
287                                       unsigned long rel_size)
288 {
289         _dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
290 }
291
292 int
293 _dl_parse_relocation_information(struct dyn_elf *rpnt,
294                                  unsigned long rel_addr,
295                                  unsigned long rel_size)
296 {
297         return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr,
298                          rel_size, _dl_do_reloc);
299 }