1 /* vi: set sw=4 ts=4: */
2 /* mips/mipsel ELF shared library loader suppport
4 Copyright (C) 2002, Steven J. Hill (sjhill@realitydiluted.com)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. The name of the above contributors may not be
14 * used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 extern int _dl_runtime_resolve(void);
33 extern int _dl_runtime_pltresolve(void);
35 #define OFFSET_GP_GOT 0x7ff0
37 unsigned long __dl_runtime_resolve(unsigned long sym_index
,
38 unsigned long old_gpreg
)
40 unsigned long *got
= (unsigned long *) (old_gpreg
- OFFSET_GP_GOT
);
41 struct elf_resolve
*tpnt
= (struct elf_resolve
*) got
[1];
44 unsigned long local_gotno
;
46 unsigned long new_addr
;
47 unsigned long instr_addr
;
51 gotsym
= tpnt
->dynamic_info
[DT_MIPS_GOTSYM_IDX
];
52 local_gotno
= tpnt
->dynamic_info
[DT_MIPS_LOCAL_GOTNO_IDX
];
54 sym
= ((ElfW(Sym
) *) tpnt
->dynamic_info
[DT_SYMTAB
]) + sym_index
;
55 strtab
= (char *) tpnt
->dynamic_info
[DT_STRTAB
];
56 symname
= strtab
+ sym
->st_name
;
58 new_addr
= (unsigned long) _dl_find_hash(symname
,
59 tpnt
->symbol_scope
, tpnt
, ELF_RTYPE_CLASS_PLT
, NULL
);
60 if (unlikely(!new_addr
)) {
61 _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
62 _dl_progname
, symname
);
66 /* Address of jump instruction to fix up */
67 instr_addr
= (unsigned long) (got
+ local_gotno
+ sym_index
- gotsym
);
68 got_addr
= (char **) instr_addr
;
70 #if defined (__SUPPORT_LD_DEBUG__)
71 if (_dl_debug_bindings
)
73 _dl_dprintf(_dl_debug_file
, "\nresolve function: %s", symname
);
74 if (_dl_debug_detail
) _dl_dprintf(_dl_debug_file
,
75 "\n\tpatched %x ==> %x @ %x\n", *got_addr
, new_addr
, got_addr
);
77 if (!_dl_debug_nofixups
) {
78 *got_addr
= (char*)new_addr
;
81 *got_addr
= (char*)new_addr
;
88 __dl_runtime_pltresolve(struct elf_resolve
*tpnt
, int reloc_entry
)
90 ELF_RELOC
*this_reloc
;
97 unsigned long instr_addr
;
100 rel_addr
= (char *)tpnt
->dynamic_info
[DT_JMPREL
];
101 this_reloc
= (ELF_RELOC
*)(intptr_t)(rel_addr
+ reloc_entry
);
102 symtab_index
= ELF32_R_SYM(this_reloc
->r_info
);
104 symtab
= (Elf32_Sym
*)(intptr_t)tpnt
->dynamic_info
[DT_SYMTAB
];
105 strtab
= (char *)tpnt
->dynamic_info
[DT_STRTAB
];
106 symname
= strtab
+ symtab
[symtab_index
].st_name
;
108 /* Address of the jump instruction to fix up. */
109 instr_addr
= ((unsigned long)this_reloc
->r_offset
+
110 (unsigned long)tpnt
->loadaddr
);
111 got_addr
= (char **)instr_addr
;
113 /* Get the address of the GOT entry. */
114 new_addr
= _dl_find_hash(symname
, tpnt
->symbol_scope
, tpnt
, ELF_RTYPE_CLASS_PLT
, NULL
);
115 if (unlikely(!new_addr
)) {
116 _dl_dprintf(2, "%s: can't resolve symbol '%s' in lib '%s'.\n", _dl_progname
, symname
, tpnt
->libname
);
120 #if defined (__SUPPORT_LD_DEBUG__)
121 if ((unsigned long)got_addr
< 0x40000000) {
122 if (_dl_debug_bindings
) {
123 _dl_dprintf(_dl_debug_file
, "\nresolve function: %s", symname
);
124 if (_dl_debug_detail
)
125 _dl_dprintf(_dl_debug_file
,
126 "\n\tpatched: %x ==> %x @ %x",
127 *got_addr
, new_addr
, got_addr
);
130 if (!_dl_debug_nofixups
) {
131 *got_addr
= new_addr
;
134 *got_addr
= new_addr
;
137 return (unsigned long)new_addr
;
140 void _dl_parse_lazy_relocation_information(struct dyn_elf
*rpnt
,
141 unsigned long rel_addr
, unsigned long rel_size
)
147 int _dl_parse_relocation_information(struct dyn_elf
*xpnt
,
148 unsigned long rel_addr
, unsigned long rel_size
)
155 unsigned long *reloc_addr
=NULL
;
156 unsigned long symbol_addr
;
157 int reloc_type
, symtab_index
;
158 struct elf_resolve
*tpnt
= xpnt
->dyn
;
159 char *symname
= NULL
;
160 #if defined (__SUPPORT_LD_DEBUG__)
161 unsigned long old_val
=0;
164 /* Now parse the relocation information */
165 rel_size
= rel_size
/ sizeof(ElfW(Rel
));
166 rpnt
= (ELF_RELOC
*) rel_addr
;
168 symtab
= (ElfW(Sym
) *) tpnt
->dynamic_info
[DT_SYMTAB
];
169 strtab
= (char *) tpnt
->dynamic_info
[DT_STRTAB
];
170 got
= (unsigned long *) tpnt
->dynamic_info
[DT_PLTGOT
];
172 for (i
= 0; i
< rel_size
; i
++, rpnt
++) {
173 reloc_addr
= (unsigned long *) (tpnt
->loadaddr
+
174 (unsigned long) rpnt
->r_offset
);
175 reloc_type
= ELF_R_TYPE(rpnt
->r_info
);
176 symtab_index
= ELF_R_SYM(rpnt
->r_info
);
179 debug_sym(symtab
,strtab
,symtab_index
);
180 debug_reloc(symtab
,strtab
,rpnt
);
181 symname
= strtab
+ symtab
[symtab_index
].st_name
;
182 #if defined (__SUPPORT_LD_DEBUG__)
184 old_val
= *reloc_addr
;
187 if (reloc_type
== R_MIPS_JUMP_SLOT
|| reloc_type
== R_MIPS_COPY
) {
188 symbol_addr
= (unsigned long)_dl_find_hash(symname
,
191 elf_machine_type_class(reloc_type
), NULL
);
192 if (unlikely(!symbol_addr
&& ELF32_ST_BIND(symtab
[symtab_index
].st_info
) != STB_WEAK
))
196 /* Relocs against STN_UNDEF are usually treated as using a
197 * symbol value of zero, and using the module containing the
200 symbol_addr
= symtab
[symtab_index
].st_value
;
203 switch (reloc_type
) {
204 #if defined USE_TLS && USE_TLS
205 # if _MIPS_SIM == _MIPS_SIM_ABI64
206 case R_MIPS_TLS_DTPMOD64
:
207 case R_MIPS_TLS_DTPREL64
:
208 case R_MIPS_TLS_TPREL64
:
210 case R_MIPS_TLS_DTPMOD32
:
211 case R_MIPS_TLS_DTPREL32
:
212 case R_MIPS_TLS_TPREL32
:
215 struct elf_resolve
*tls_tpnt
= NULL
;
216 struct symbol_ref sym_ref
;
217 sym_ref
.sym
= &symtab
[symtab_index
];
220 if (ELF32_ST_BIND(symtab
[symtab_index
].st_info
) != STB_LOCAL
) {
221 symbol_addr
= (unsigned long) _dl_find_hash(symname
, tpnt
->symbol_scope
,
222 tpnt
, elf_machine_type_class(reloc_type
), &sym_ref
);
223 tls_tpnt
= sym_ref
.tpnt
;
225 /* In case of a TLS reloc, tls_tpnt NULL means we have an 'anonymous'
226 symbol. This is the case for a static tls variable, so the lookup
227 module is just that one is referencing the tls variable. */
231 switch (reloc_type
) {
232 case R_MIPS_TLS_DTPMOD64
:
233 case R_MIPS_TLS_DTPMOD32
:
235 *(ElfW(Word
) *)reloc_addr
= tls_tpnt
->l_tls_modid
;
236 #ifdef __SUPPORT_LD_DEBUG__
237 _dl_dprintf(2, "TLS_DTPMOD : %s, %d, %d\n",
238 symname
, old_val
, *((unsigned int *)reloc_addr
));
242 case R_MIPS_TLS_DTPREL64
:
243 case R_MIPS_TLS_DTPREL32
:
244 *(ElfW(Word
) *)reloc_addr
+=
245 TLS_DTPREL_VALUE (symbol_addr
);
246 #ifdef __SUPPORT_LD_DEBUG__
247 _dl_dprintf(2, "TLS_DTPREL : %s, %x, %x\n",
248 symname
, old_val
, *((unsigned int *)reloc_addr
));
252 case R_MIPS_TLS_TPREL32
:
253 case R_MIPS_TLS_TPREL64
:
254 CHECK_STATIC_TLS((struct link_map
*)tls_tpnt
);
255 *(ElfW(Word
) *)reloc_addr
+=
256 TLS_TPREL_VALUE (tls_tpnt
, symbol_addr
);
257 #ifdef __SUPPORT_LD_DEBUG__
258 _dl_dprintf(2, "TLS_TPREL : %s, %x, %x\n",
259 symname
, old_val
, *((unsigned int *)reloc_addr
));
267 #if _MIPS_SIM == _MIPS_SIM_ABI64
268 case (R_MIPS_64
<< 8) | R_MIPS_REL32
:
269 #else /* O32 || N32 */
271 #endif /* O32 || N32 */
273 if (symtab_index
< tpnt
->dynamic_info
[DT_MIPS_GOTSYM_IDX
])
275 symtab
[symtab_index
].st_value
+
276 (unsigned long) tpnt
->loadaddr
;
278 *reloc_addr
+= got
[symtab_index
+ tpnt
->dynamic_info
[DT_MIPS_LOCAL_GOTNO_IDX
] -
279 tpnt
->dynamic_info
[DT_MIPS_GOTSYM_IDX
]];
283 *reloc_addr
+= (unsigned long) tpnt
->loadaddr
;
286 case R_MIPS_JUMP_SLOT
:
287 *reloc_addr
= symbol_addr
;
291 #if defined (__SUPPORT_LD_DEBUG__)
293 _dl_dprintf(_dl_debug_file
,
294 "\n%s move %d bytes from %x to %x",
295 symname
, symtab
[symtab_index
].st_size
,
296 symbol_addr
, reloc_addr
);
299 _dl_memcpy((char *)reloc_addr
,
301 symtab
[symtab_index
].st_size
);
308 _dl_dprintf(2, "\n%s: ",_dl_progname
);
311 _dl_dprintf(2, "symbol '%s': ", symname
);
313 #if defined (__SUPPORT_LD_DEBUG__)
314 _dl_dprintf(2, "can't handle reloc type '%s' in lib '%s'\n", _dl_reltypes(reloc_type
), tpnt
->libname
);
316 _dl_dprintf(2, "can't handle reloc type %x in lib '%s'\n", reloc_type
, tpnt
->libname
);
323 #if defined (__SUPPORT_LD_DEBUG__)
324 if (_dl_debug_reloc
&& _dl_debug_detail
&& reloc_addr
)
325 _dl_dprintf(_dl_debug_file
, "\tpatched: %x ==> %x @ %x\n", old_val
, *reloc_addr
, reloc_addr
);
331 /* Relocate the global GOT entries for the object */
332 void _dl_perform_mips_global_got_relocations(struct elf_resolve
*tpnt
, int lazy
)
336 unsigned long i
, tmp_lazy
;
337 unsigned long *got_entry
;
339 for (; tpnt
; tpnt
= tpnt
->next
) {
341 /* We don't touch the dynamic linker */
342 if (tpnt
->libtype
== program_interpreter
)
345 /* Setup the loop variables */
346 got_entry
= (unsigned long *) (tpnt
->dynamic_info
[DT_PLTGOT
])
347 + tpnt
->dynamic_info
[DT_MIPS_LOCAL_GOTNO_IDX
];
348 sym
= (ElfW(Sym
) *) tpnt
->dynamic_info
[DT_SYMTAB
] + tpnt
->dynamic_info
[DT_MIPS_GOTSYM_IDX
];
349 strtab
= (char *) tpnt
->dynamic_info
[DT_STRTAB
];
350 i
= tpnt
->dynamic_info
[DT_MIPS_SYMTABNO_IDX
] - tpnt
->dynamic_info
[DT_MIPS_GOTSYM_IDX
];
352 #if defined (__SUPPORT_LD_DEBUG__)
354 _dl_dprintf(2, "_dl_perform_mips_global_got_relocations for '%s'\n", tpnt
->libname
);
356 tmp_lazy
= lazy
&& !tpnt
->dynamic_info
[DT_BIND_NOW
];
357 /* Relocate the global GOT entries for the object */
359 if (sym
->st_shndx
== SHN_UNDEF
) {
360 if (ELF_ST_TYPE(sym
->st_info
) == STT_FUNC
&& sym
->st_value
&& tmp_lazy
) {
361 *got_entry
= sym
->st_value
+ (unsigned long) tpnt
->loadaddr
;
364 *got_entry
= (unsigned long) _dl_find_hash(strtab
+
365 sym
->st_name
, tpnt
->symbol_scope
, tpnt
, ELF_RTYPE_CLASS_PLT
, NULL
);
368 else if (sym
->st_shndx
== SHN_COMMON
) {
369 *got_entry
= (unsigned long) _dl_find_hash(strtab
+
370 sym
->st_name
, tpnt
->symbol_scope
, tpnt
, ELF_RTYPE_CLASS_PLT
, NULL
);
372 else if (ELF_ST_TYPE(sym
->st_info
) == STT_FUNC
&&
373 *got_entry
!= sym
->st_value
&& tmp_lazy
) {
374 *got_entry
+= (unsigned long) tpnt
->loadaddr
;
376 else if (ELF_ST_TYPE(sym
->st_info
) == STT_SECTION
) {
377 if (sym
->st_other
== 0)
378 *got_entry
+= (unsigned long) tpnt
->loadaddr
;
381 *got_entry
= (unsigned long) _dl_find_hash(strtab
+
382 sym
->st_name
, tpnt
->symbol_scope
, tpnt
, ELF_RTYPE_CLASS_PLT
, NULL
);