1 Subject: [PATCH] ldso: AVR32 support
3 This implements the AVR32-specific parts of the dynamic linker.
7 ldso/ldso/avr32/dl-debug.h | 45 +++++++++
8 ldso/ldso/avr32/dl-startup.h | 110 ++++++++++++++++++++++++
9 ldso/ldso/avr32/dl-syscalls.h | 5 +
10 ldso/ldso/avr32/dl-sysdep.h | 103 ++++++++++++++++++++++
11 ldso/ldso/avr32/elfinterp.c | 191 ++++++++++++++++++++++++++++++++++++++++++
12 ldso/ldso/avr32/resolve.S | 28 ++++++
13 6 files changed, 482 insertions(+)
15 Index: uClibc-0.9.28/ldso/ldso/avr32/dl-debug.h
16 ===================================================================
17 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
18 +++ uClibc-0.9.28/ldso/ldso/avr32/dl-debug.h 2006-05-05 09:30:43.000000000 +0200
21 + * AVR32 ELF shared libary loader support
23 + * Copyright (C) 2005 Atmel Norway
24 + * All rights reserved.
26 + * Redistribution and use in source and binary forms, with or without
27 + * modification, are permitted provided that the following conditions
29 + * 1. Redistributions of source code must retain the above copyright
30 + * notice, this list of conditions and the following disclaimer.
31 + * 2. The name of the above contributors may not be
32 + * used to endorse or promote products derived from this software
33 + * without specific prior written permission.
35 + * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
36 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 + * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
39 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 +static const char *_dl_reltypes_tab[] = {
50 + "R_AVR32_32", "R_AVR32_16", "R_AVR32_8",
51 + "R_AVR32_32_PCREL", "R_AVR32_16_PCREL", "R_AVR32_8_PCREL",
52 + "R_AVR32_DIFF32", "R_AVR32_DIFF16", "R_AVR32_DIFF8",
53 + "R_AVR32_GOT32", "R_AVR32_GOT16", "R_AVR32_GOT8",
54 + "R_AVR32_21S", "R_AVR32_16U", "R_AVR32_16S", "R_AVR32_8S", "R_AVR32_8S_EXT",
55 + "R_AVR32_22H_PCREL", "R_AVR32_18W_PCREL", "R_AVR32_16B_PCREL",
56 + "R_AVR32_16N_PCREL", "R_AVR32_14UW_PCREL", "R_AVR32_11H_PCREL",
57 + "R_AVR32_10UW_PCREL", "R_AVR32_9H_PCREL", "R_AVR32_9UW_PCREL",
58 + "R_AVR32_HI16", "R_AVR32_LO16",
59 + "R_AVR32_GOTPC", "R_AVR32_GOTCALL", "R_AVR32_LDA_GOT",
60 + "R_AVR32_GOT21S", "R_AVR32_GOT18SW", "R_AVR32_GOT16S", "R_AVR32_GOT7UW",
61 + "R_AVR32_32_CPENT", "R_AVR32_CPCALL", "R_AVR32_16_CP", "R_AVR32_9W_CP",
62 + "R_AVR32_RELATIVE", "R_AVR32_GLOB_DAT", "R_AVR32_JMP_SLOT",
65 Index: uClibc-0.9.28/ldso/ldso/avr32/dl-startup.h
66 ===================================================================
67 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
68 +++ uClibc-0.9.28/ldso/ldso/avr32/dl-startup.h 2006-05-05 09:29:45.000000000 +0200
70 +/* vi: set sw=4 ts=4: */
72 + * Architecture specific code used by dl-startup.c
73 + * Copyright (C) 2005 Atmel Norway
76 +/* This is the library loader's main entry point. Let _dl_boot2 do its
77 + * initializations and jump to the application's entry point
81 + " .type _start,@function\n"
83 + /* All arguments are on the stack initially */
85 + " rcall _dl_start\n"
86 + /* Returns user entry point in r12. Save it. */
88 + /* We're PIC, so get the Global Offset Table */
89 + " lddpc r6, .L_GOT\n"
92 + /* Adjust argc and argv according to _dl_skip_args */
93 + " ld.w r1, r6[_dl_skip_args@got]\n"
97 + " add sp, sp, r1 << 2\n"
99 + /* Load the finalizer function */
100 + " ld.w r12, r6[_dl_fini@got]\n"
101 + /* Jump to the user's entry point */
106 + " .long .L_RGOT - _GLOBAL_OFFSET_TABLE_\n"
107 + " .size _start, . - _start\n"
110 +/* Get a pointer to the argv array. On many platforms this can be just
111 + * the address if the first argument, on other platforms we need to
112 + * do something a little more subtle here. */
113 +#define GET_ARGV(ARGVP, ARGS) ARGVP = ((unsigned long *)ARGS + 1)
116 +/* We can't call functions before the GOT has been initialized */
117 +#define NO_FUNCS_BEFORE_BOOTSTRAP
120 + * Relocate the GOT during dynamic loader bootstrap. This will add
121 + * the load address to all entries in the GOT, which is necessary
122 + * because the linker doesn't generate R_AVR32_RELATIVE relocs for the
125 +static __always_inline
126 +void PERFORM_BOOTSTRAP_GOT(struct elf_resolve *tpnt)
128 + Elf32_Addr i, nr_got;
129 + register Elf32_Addr *__r6 __asm__("r6");
130 + Elf32_Addr *got = __r6;
132 + nr_got = tpnt->dynamic_info[DT_AVR32_GOTSZ_IDX] / sizeof(*got);
133 + for (i = 2; i < nr_got; i++)
134 + got[i] += tpnt->loadaddr;
137 +#define PERFORM_BOOTSTRAP_GOT(tpnt) PERFORM_BOOTSTRAP_GOT(tpnt)
139 +/* Handle relocation of the symbols in the dynamic loader. */
140 +static __always_inline
141 +void PERFORM_BOOTSTRAP_RELOC(ELF_RELOC *rpnt, unsigned long *reloc_addr,
142 + unsigned long symbol_addr,
143 + unsigned long load_addr, Elf32_Sym *symtab)
145 + switch(ELF32_R_TYPE(rpnt->r_info)) {
148 + case R_AVR32_GLOB_DAT:
149 + case R_AVR32_JMP_SLOT:
150 + *reloc_addr = symbol_addr;
152 + case R_AVR32_RELATIVE:
153 + SEND_STDERR_DEBUG("Applying RELATIVE relocation: ");
154 + SEND_ADDRESS_STDERR_DEBUG(load_addr, 0);
155 + SEND_STDERR_DEBUG(" + ");
156 + SEND_ADDRESS_STDERR_DEBUG(rpnt->r_addend, 1);
157 + *reloc_addr = load_addr + rpnt->r_addend;
160 + SEND_STDERR("BOOTSTRAP_RELOC: unhandled reloc_type ");
161 + SEND_NUMBER_STDERR(ELF32_R_TYPE(rpnt->r_info), 1);
162 + SEND_STDERR("REL, SYMBOL, LOAD: ");
163 + SEND_ADDRESS_STDERR(reloc_addr, 0);
165 + SEND_ADDRESS_STDERR(symbol_addr, 0);
167 + SEND_ADDRESS_STDERR(load_addr, 1);
172 +/* Transfer control to the user's application, once the dynamic loader
173 + * is done. This routine has to exit the current function, then call
174 + * the _dl_elf_main function.
176 + * Since our _dl_boot will simply call whatever is returned by
177 + * _dl_boot2, we can just return the address we're supposed to
179 +#define START() return _dl_elf_main;
180 Index: uClibc-0.9.28/ldso/ldso/avr32/dl-syscalls.h
181 ===================================================================
182 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
183 +++ uClibc-0.9.28/ldso/ldso/avr32/dl-syscalls.h 2006-05-05 09:29:25.000000000 +0200
185 +/* We can't use the real errno in ldso, since it has not yet
186 + * been dynamicly linked in yet. */
187 +extern int _dl_errno;
188 +#define __set_errno(X) {(_dl_errno) = (X);}
189 +#include "sys/syscall.h"
190 Index: uClibc-0.9.28/ldso/ldso/avr32/dl-sysdep.h
191 ===================================================================
192 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
193 +++ uClibc-0.9.28/ldso/ldso/avr32/dl-sysdep.h 2006-05-05 09:30:43.000000000 +0200
195 +/* vi: set sw=4 ts=4: */
197 + * Various assembly language/system dependent hacks that are required
198 + * so that we can minimize the amount of platform specific code.
199 + * Copyright (C) 2004-2005 Atmel Norway
202 +/* Define this if the system uses RELOCA. */
203 +#define ELF_USES_RELOCA
208 +#define DT_AVR32_GOTSZ_IDX (DT_NUM + OS_NUM)
210 +#define ARCH_DYNAMIC_INFO(dpnt, dynamic, debug_addr) \
212 + if (dpnt->d_tag == DT_AVR32_GOTSZ) \
213 + dynamic[DT_AVR32_GOTSZ_IDX] = dpnt->d_un.d_val; \
216 +/* Initialization sequence for the application/library GOT. */
217 +#define INIT_GOT(GOT_BASE,MODULE) \
219 + unsigned long i, nr_got; \
221 + GOT_BASE[0] = (unsigned long) _dl_linux_resolve; \
222 + GOT_BASE[1] = (unsigned long) MODULE; \
224 + /* Add load address displacement to all GOT entries */ \
225 + nr_got = MODULE->dynamic_info[DT_AVR32_GOTSZ_IDX] / 4; \
226 + for (i = 2; i < nr_got; i++) \
227 + GOT_BASE[i] += (unsigned long)MODULE->loadaddr; \
230 +#define do_rem(result, n, base) ((result) = (n) % (base))
232 +/* Here we define the magic numbers that this dynamic loader should accept */
233 +#define MAGIC1 EM_AVR32
236 +/* Used for error messages */
237 +#define ELF_TARGET "AVR32"
239 +unsigned long _dl_linux_resolver(unsigned long got_offset, unsigned long *got);
241 +/* 4096 bytes alignment */
242 +#define PAGE_ALIGN 0xfffff000
243 +#define ADDR_ALIGN 0xfff
244 +#define OFFS_ALIGN 0x7ffff000
246 +#define elf_machine_type_class(type) \
247 + ((type == R_AVR32_JMP_SLOT) * ELF_RTYPE_CLASS_PLT)
249 +/* AVR32 doesn't need any COPY relocs */
250 +#define DL_NO_COPY_RELOCS
252 +/* Return the link-time address of _DYNAMIC. Conveniently, this is the
253 + first element of the GOT. This must be inlined in a function which
254 + uses global data. */
255 +static inline Elf32_Addr
256 +elf_machine_dynamic (void)
258 + register Elf32_Addr *got asm ("r6");
262 +/* Return the run-time load address of the shared object. */
263 +static inline Elf32_Addr
264 +elf_machine_load_address (void)
266 + extern void __dl_start asm("_dl_start");
267 + Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
268 + Elf32_Addr pcrel_addr;
270 + asm (" lddpc %0, 2f\n"
274 + "2: .long _dl_start - 1b\n"
276 + : "=r"(pcrel_addr) : : "cc");
278 + return pcrel_addr - got_addr;
282 + * Perform any RELATIVE relocations specified by DT_RELCOUNT.
283 + * Currently, we don't use that tag, but we might in the future as
284 + * this would reduce the startup time somewhat (although probably not by much).
287 +elf_machine_relative (Elf32_Addr load_off, const Elf32_Addr rel_addr,
288 + Elf32_Word relative_count)
290 + Elf32_Rela *rpnt = (void *)rel_addr;
293 + Elf32_Addr *reloc_addr;
294 + reloc_addr = (void *)(load_off + (rpnt++)->r_offset);
295 + *reloc_addr = load_off + rpnt->r_addend;
296 + } while (--relative_count);
298 Index: uClibc-0.9.28/ldso/ldso/avr32/elfinterp.c
299 ===================================================================
300 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
301 +++ uClibc-0.9.28/ldso/ldso/avr32/elfinterp.c 2006-05-05 09:30:43.000000000 +0200
304 + * AVR32 ELF shared library loader suppport
306 + * Copyright (C) 2004-2006 Atmel Corporation
308 + * All rights reserved.
310 + * Redistribution and use in source and binary forms, with or without
311 + * modification, are permitted provided that the following conditions
313 + * 1. Redistributions of source code must retain the above copyright
314 + * notice, this list of conditions and the following disclaimer.
315 + * 2. The name of the above contributors may not be
316 + * used to endorse or promote products derived from this software
317 + * without specific prior written permission.
319 + * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
320 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
321 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
322 + * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
323 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
324 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
325 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
326 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
327 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
328 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
332 +unsigned long _dl_linux_resolver(unsigned long got_offset, unsigned long *got)
334 + struct elf_resolve *tpnt = (struct elf_resolve *)got[1];
336 + unsigned long local_gotno;
337 + unsigned long gotsym;
338 + unsigned long new_addr;
339 + char *strtab, *symname;
340 + unsigned long *entry;
341 + unsigned long sym_index = got_offset / 4;
344 + local_gotno = tpnt->dynamic_info[DT_AVR32_LOCAL_GOTNO];
345 + gotsym = tpnt->dynamic_info[DT_AVR32_GOTSYM];
347 + sym = ((Elf32_Sym *)(tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr))
349 + strtab = (char *)(tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
350 + symname = strtab + sym->st_name;
353 + new_addr = (unsigned long) _dl_find_hash(strtab + sym->st_name,
354 + tpnt->symbol_scope, tpnt,
358 + entry = (unsigned long *)(got + local_gotno + sym_index - gotsym);
366 +_dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
367 + unsigned long rel_addr, unsigned long rel_size,
368 + int (*reloc_func)(struct elf_resolve *tpnt, struct dyn_elf *scope,
369 + Elf32_Rela *rpnt, Elf32_Sym *symtab, char *strtab))
376 + rpnt = (Elf32_Rela *)rel_addr;
377 + rel_size /= sizeof(Elf32_Rela);
378 + symtab = (Elf32_Sym *)tpnt->dynamic_info[DT_SYMTAB];
379 + strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
381 + for (i = 0; i < rel_size; i++, rpnt++) {
382 + int symtab_index, res;
384 + symtab_index = ELF32_R_SYM(rpnt->r_info);
386 + debug_sym(symtab, strtab, symtab_index);
387 + debug_reloc(symtab, strtab, rpnt);
389 + res = reloc_func(tpnt, scope, rpnt, symtab, strtab);
394 + _dl_dprintf(2, "\n%s: ", _dl_progname);
397 + _dl_dprintf(2, "symbol '%s': ",
398 + strtab + symtab[symtab_index].st_name);
401 + int reloc_type = ELF32_R_TYPE(rpnt->r_info);
402 +#if defined(__SUPPORT_LD_DEBUG__)
403 + _dl_dprintf(2, "can't handle reloc type %s\n",
404 + _dl_reltypes(reloc_type));
406 + _dl_dprintf(2, "can't handle reloc type %x\n",
411 + _dl_dprintf(2, "can't resolve symbol\n");
419 +static int _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
420 + Elf32_Rela *rpnt, Elf32_Sym *symtab, char *strtab)
425 + unsigned long *reloc_addr;
426 + unsigned long symbol_addr;
427 +#if defined(__SUPPORT_LD_DEBUG__)
428 + unsigned long old_val;
431 + reloc_addr = (unsigned long *)(tpnt->loadaddr + rpnt->r_offset);
432 + reloc_type = ELF32_R_TYPE(rpnt->r_info);
433 + symtab_index = ELF32_R_SYM(rpnt->r_info);
435 + symname = strtab + symtab[symtab_index].st_name;
437 + if (symtab_index) {
438 + symbol_addr = (unsigned long)
439 + _dl_find_hash(strtab + symtab[symtab_index].st_name,
440 + tpnt->symbol_scope, tpnt,
441 + elf_machine_type_class(reloc_type));
443 + /* Allow undefined references to weak symbols */
444 + if (!symbol_addr &&
445 + ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK) {
446 + _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
447 + _dl_progname, symname);
452 +#if defined(__SUPPORT_LD_DEBUG__)
453 + old_val = *reloc_addr;
455 + switch (reloc_type) {
458 + case R_AVR32_GLOB_DAT:
459 + case R_AVR32_JMP_SLOT:
460 + *reloc_addr = symbol_addr + rpnt->r_addend;
462 + case R_AVR32_RELATIVE:
463 + *reloc_addr = (unsigned long)tpnt->loadaddr
470 +#if defined(__SUPPORT_LD_DEBUG__)
471 + if (_dl_debug_reloc && _dl_debug_detail)
472 + _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
473 + old_val, *reloc_addr);
479 +void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
480 + unsigned long rel_addr,
481 + unsigned long rel_size)
483 + /* TODO: Might want to support this in order to get faster
484 + * startup times... */
487 +int _dl_parse_relocation_information(struct dyn_elf *rpnt,
488 + unsigned long rel_addr,
489 + unsigned long rel_size)
491 + return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size,
494 Index: uClibc-0.9.28/ldso/ldso/avr32/resolve.S
495 ===================================================================
496 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
497 +++ uClibc-0.9.28/ldso/ldso/avr32/resolve.S 2006-05-05 09:29:25.000000000 +0200
500 + * Linux dynamic resolving code for AVR32. Fixes up the GOT entry as
501 + * indicated in register r12 and jumps to the resolved address.
503 + * This file is subject to the terms and conditions of the GNU Lesser General
504 + * Public License. See the file "COPYING.LIB" in the main directory of this
505 + * archive for more details.
507 + * Copyright (C) 2004 Atmel Norway
513 + .global _dl_linux_resolve
514 + .type _dl_linux_resolve,@function
516 + /* The PLT code pushed r8 for us. It contains the address of this
517 + function's GOT entry, that is entry 0. ip contains the address
518 + of the GOT entry of the function we wanted to call. */
519 + stm --sp, r9-r12, lr
522 + rcall _dl_linux_resolver
526 + .size _dl_linux_resolve, . - _dl_linux_resolve