diff options
Diffstat (limited to '')
26 files changed, 1138 insertions, 323 deletions
diff --git a/cpukit/libdl/rtl-alloc-heap.c b/cpukit/libdl/rtl-alloc-heap.c index 10150a0753..f90233874e 100644 --- a/cpukit/libdl/rtl-alloc-heap.c +++ b/cpukit/libdl/rtl-alloc-heap.c @@ -9,7 +9,7 @@ */ /* - * COPYRIGHT (c) 2012 Chris Johns <chrisj@rtems.org> + * COPYRIGHT (c) 2012,2023 Chris Johns <chrisj@rtems.org> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -54,6 +54,9 @@ rtems_rtl_alloc_heap (rtems_rtl_alloc_cmd cmd, free (*address); *address = NULL; break; + case RTEMS_RTL_ALLOC_RESIZE: + *address = realloc (*address, size); + break; case RTEMS_RTL_ALLOC_LOCK: _RTEMS_Lock_allocator(); break; diff --git a/cpukit/libdl/rtl-allocator.c b/cpukit/libdl/rtl-allocator.c index f09690d71a..7503183367 100644 --- a/cpukit/libdl/rtl-allocator.c +++ b/cpukit/libdl/rtl-allocator.c @@ -9,7 +9,7 @@ */ /* - * COPYRIGHT (c) 2012, 2018 Chris Johns <chrisj@rtems.org> + * COPYRIGHT (c) 2012, 2018, 2023 Chris Johns <chrisj@rtems.org> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -110,6 +110,39 @@ rtems_rtl_alloc_del (rtems_rtl_alloc_tag tag, void* address) rtems_rtl_unlock (); } +void* rtems_rtl_alloc_resize (rtems_rtl_alloc_tag tag, + void* address, + size_t size, + bool zero) +{ + rtems_rtl_data* rtl = rtems_rtl_lock (); + const void* prev_address = address; + + /* + * Resize memory of an existing allocation. The address field is set + * by the allocator and may change. + */ + if (rtl != NULL) + rtl->allocator.allocator (RTEMS_RTL_ALLOC_RESIZE, tag, &address, size); + + rtems_rtl_unlock (); + + if (rtems_rtl_trace (RTEMS_RTL_TRACE_ALLOCATOR)) + printf ("rtl: alloc: resize: %s%s prev-addr=%p addr=%p size=%zu\n", + rtems_rtl_trace_tag_label (tag), prev_address == address ? "" : " MOVED", + prev_address, address, size); + + /* + * Only zero the memory if asked to and the resize was successful. We + * cannot clear the resized area if bigger than the previouis allocation + * because we do not have the original size. + */ + if (address != NULL && zero) + memset (address, 0, size); + + return address; +} + void rtems_rtl_alloc_wr_enable (rtems_rtl_alloc_tag tag, void* address) { @@ -277,21 +310,23 @@ rtems_rtl_alloc_module_new (void** text_base, size_t text_size, { *text_base = *const_base = *data_base = *bss_base = NULL; - if (text_size) + if (data_size != 0) { - *text_base = rtems_rtl_alloc_new (rtems_rtl_alloc_text_tag (), - text_size, false); - if (!*text_base) + *data_base = rtems_rtl_alloc_new (rtems_rtl_alloc_data_tag (), + data_size, false); + if (*data_base == NULL) { + rtems_rtl_alloc_module_del (text_base, const_base, eh_base, + data_base, bss_base); return false; } } - if (const_size) + if (bss_size != 0) { - *const_base = rtems_rtl_alloc_new (rtems_rtl_alloc_const_tag (), - const_size, false); - if (!*const_base) + *bss_base = rtems_rtl_alloc_new (rtems_rtl_alloc_bss_tag (), + bss_size, false); + if (*bss_base == NULL) { rtems_rtl_alloc_module_del (text_base, const_base, eh_base, data_base, bss_base); @@ -299,11 +334,11 @@ rtems_rtl_alloc_module_new (void** text_base, size_t text_size, } } - if (eh_size) + if (eh_size != 0) { *eh_base = rtems_rtl_alloc_new (rtems_rtl_alloc_eh_tag (), eh_size, false); - if (!*eh_base) + if (*eh_base == NULL) { rtems_rtl_alloc_module_del (text_base, const_base, eh_base, data_base, bss_base); @@ -311,11 +346,11 @@ rtems_rtl_alloc_module_new (void** text_base, size_t text_size, } } - if (data_size) + if (const_size != 0) { - *data_base = rtems_rtl_alloc_new (rtems_rtl_alloc_data_tag (), - data_size, false); - if (!*data_base) + *const_base = rtems_rtl_alloc_new (rtems_rtl_alloc_const_tag (), + const_size, false); + if (*const_base == NULL) { rtems_rtl_alloc_module_del (text_base, const_base, eh_base, data_base, bss_base); @@ -323,14 +358,72 @@ rtems_rtl_alloc_module_new (void** text_base, size_t text_size, } } - if (bss_size) + if (text_size != 0) { - *bss_base = rtems_rtl_alloc_new (rtems_rtl_alloc_bss_tag (), - bss_size, false); - if (!*bss_base) + *text_base = rtems_rtl_alloc_new (rtems_rtl_alloc_text_tag (), + text_size, false); + if (*text_base == NULL) + { + return false; + } + } + + return true; +} + +bool +rtems_rtl_alloc_module_resize (void** text_base, size_t text_size, + void** const_base, size_t const_size, + void** eh_base, size_t eh_size, + void** data_base, size_t data_size, + void** bss_base, size_t bss_size) +{ + if (data_size != 0) + { + *data_base = rtems_rtl_alloc_resize (rtems_rtl_alloc_data_tag (), + *data_base, data_size, false); + if (*data_base == NULL) + { + return false; + } + } + + if (bss_size != 0) + { + *bss_base = rtems_rtl_alloc_resize (rtems_rtl_alloc_bss_tag (), + *bss_base, bss_size, false); + if (*bss_base == NULL) + { + return false; + } + } + + if (eh_size != 0) + { + *eh_base = rtems_rtl_alloc_resize (rtems_rtl_alloc_eh_tag (), + *eh_base, eh_size, false); + if (*eh_base == NULL) + { + return false; + } + } + + if (const_size != 0) + { + *const_base = rtems_rtl_alloc_resize (rtems_rtl_alloc_const_tag (), + *const_base, const_size, false); + if (*const_base == NULL) + { + return false; + } + } + + if (text_size != 0) + { + *text_base = rtems_rtl_alloc_resize (rtems_rtl_alloc_text_tag (), + *text_base, text_size, false); + if (*text_base == NULL) { - rtems_rtl_alloc_module_del (text_base, const_base, eh_base, - data_base, bss_base); return false; } } diff --git a/cpukit/libdl/rtl-archive.c b/cpukit/libdl/rtl-archive.c index f916336f7c..4a6d2cbf0b 100644 --- a/cpukit/libdl/rtl-archive.c +++ b/cpukit/libdl/rtl-archive.c @@ -1174,6 +1174,7 @@ rtems_rtl_obj_archive_find_obj (int fd, */ if (header[0] == '/') { + const char* name_ = *name; off_t extended_off; switch (header[1]) @@ -1190,7 +1191,7 @@ rtems_rtl_obj_archive_find_obj (int fd, * return the result. */ *extended_file_names = *ooffset + RTEMS_RTL_AR_FHDR_SIZE; - if (*name[0] == '/' && *name[1] == '/') + if (name_[0] == '/' && name_[1] == '/') { *ooffset = *ooffset + RTEMS_RTL_AR_FHDR_SIZE; return true; diff --git a/cpukit/libdl/rtl-elf.c b/cpukit/libdl/rtl-elf.c index 8a21c5df73..dd6d8617bb 100644 --- a/cpukit/libdl/rtl-elf.c +++ b/cpukit/libdl/rtl-elf.c @@ -178,12 +178,19 @@ rtems_rtl_elf_find_symbol (rtems_rtl_obj* obj, /* * If the symbol type is STT_NOTYPE the symbol references a global - * symbol. The gobal symbol table is searched to find it and that value + * symbol. The global symbol table is searched to find it and that value * returned. If the symbol is local to the object module the section for the * symbol is located and it's base added to the symbol's value giving an * absolute location. + * + * If the symbols type of TLS return the symbols value. It is the + * offset from the thread's TLS area base. The offset is set by the + * linker for the base image and by the TLS allocator for loaded + * modules. There is no section and no absolute base. */ - if (ELF_ST_TYPE(sym->st_info) == STT_NOTYPE || sym->st_shndx == SHN_COMMON) + if (ELF_ST_TYPE (sym->st_info) == STT_NOTYPE || + sym->st_shndx == SHN_COMMON || + ELF_ST_TYPE (sym->st_info) == STT_TLS) { /* * Search the object file then the global table for the symbol. @@ -192,7 +199,7 @@ rtems_rtl_elf_find_symbol (rtems_rtl_obj* obj, if (!*symbol) return false; - *value = (Elf_Addr) (*symbol)->value; + *value = (Elf_Addr)(uintptr_t) (*symbol)->value; return true; } @@ -202,7 +209,7 @@ rtems_rtl_elf_find_symbol (rtems_rtl_obj* obj, if (!sect) return false; - *value = sym->st_value + (Elf_Addr) sect->base; + *value = sym->st_value + (Elf_Addr)(uintptr_t) sect->base; return true; } @@ -247,6 +254,13 @@ rtems_rtl_elf_reloc_parser (rtems_rtl_obj* obj, rtems_rtl_elf_rel_status rs; /* + * TLS are not parsed. + */ + if (ELF_ST_TYPE (sym->st_info) == STT_TLS) { + return true; + } + + /* * Check the reloc record to see if a trampoline is needed. */ if (is_rela) @@ -302,7 +316,7 @@ rtems_rtl_elf_reloc_parser (rtems_rtl_obj* obj, * Find the symbol's object file. It cannot be NULL so ignore that result * if returned, it means something is corrupted. We are in an iterator. */ - rtems_rtl_obj* sobj = rtems_rtl_find_obj_with_symbol (symbol); + rtems_rtl_obj* sobj = rtems_rtl_find_obj_with_symbol (symbol); if (sobj != NULL) { /* @@ -791,7 +805,7 @@ rtems_rtl_elf_tramp_resolve_reloc (rtems_rtl_unresolv_rec* rec, } if (unresolved || rs == rtems_rtl_elf_rel_tramp_add) - tramp->obj->tramps_size += tramp->obj->tramp_size; + ++tramp->obj->tramp_slots; if (rs == rtems_rtl_elf_rel_failure) { *failure = true; @@ -804,7 +818,7 @@ rtems_rtl_elf_tramp_resolve_reloc (rtems_rtl_unresolv_rec* rec, } static bool -rtems_rtl_elf_alloc_trampoline (rtems_rtl_obj* obj, size_t unresolved) +rtems_rtl_elf_find_trampolines (rtems_rtl_obj* obj, size_t unresolved) { rtems_rtl_tramp_data td = { 0 }; td.obj = obj; @@ -815,21 +829,20 @@ rtems_rtl_elf_alloc_trampoline (rtems_rtl_obj* obj, size_t unresolved) if (td.failure) return false; rtems_rtl_trampoline_remove (obj); - obj->tramp_relocs = obj->tramp_size == 0 ? 0 : obj->tramps_size / obj->tramp_size; + obj->tramp_relocs = obj->tramp_slots; if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) - printf ("rtl: tramp:elf: tramps: %zu count:%zu total:%zu\n", + printf ("rtl: tramp:elf: tramps: slots:%zu count:%zu total:%zu\n", obj->tramp_relocs, td.count, td.total); /* * Add on enough space to handle the unresolved externals that need to be * resolved at some point in time. They could all require fixups and * trampolines. */ - obj->tramps_size += obj->tramp_size * unresolved; + obj->tramp_slots += unresolved; if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) - printf ("rtl: tramp:elf: slots: %zu (%zu)\n", - obj->tramp_size == 0 ? 0 : obj->tramps_size / obj->tramp_size, - obj->tramps_size); - return rtems_rtl_obj_alloc_trampoline (obj); + printf ("rtl: tramp:elf: slots:%zu (%zu)\n", + obj->tramp_slots, obj->tramp_slots * obj->tramp_slot_size); + return true; } static bool @@ -1716,7 +1729,7 @@ rtems_rtl_elf_file_load (rtems_rtl_obj* obj, int fd) /* * Set the format's architecture's maximum tramp size. */ - obj->tramp_size = rtems_rtl_elf_relocate_tramp_max_size (); + obj->tramp_slot_size = rtems_rtl_elf_relocate_tramp_max_size (); /* * Parse the section information first so we have the memory map of the object @@ -1765,13 +1778,23 @@ rtems_rtl_elf_file_load (rtems_rtl_obj* obj, int fd) if (!rtems_rtl_obj_alloc_sections (obj, fd, rtems_rtl_elf_arch_alloc, &ehdr)) return false; - if (!rtems_rtl_obj_load_symbols (obj, fd, rtems_rtl_elf_symbols_locate, &ehdr)) + if (!rtems_rtl_elf_dependents (obj, &relocs)) return false; - if (!rtems_rtl_elf_dependents (obj, &relocs)) + if (!rtems_rtl_elf_find_trampolines (obj, relocs.unresolved)) return false; - if (!rtems_rtl_elf_alloc_trampoline (obj, relocs.unresolved)) + /* + * Resize the sections to allocate the trampoline memory as part of + * the text section. + */ + if (rtems_rtl_obj_has_trampolines (obj)) + { + if (!rtems_rtl_obj_resize_sections (obj)) + return false; + } + + if (!rtems_rtl_obj_load_symbols (obj, fd, rtems_rtl_elf_symbols_locate, &ehdr)) return false; /* diff --git a/cpukit/libdl/rtl-mdreloc-aarch64.c b/cpukit/libdl/rtl-mdreloc-aarch64.c index 41147e285c..1eb1d1e87d 100644 --- a/cpukit/libdl/rtl-mdreloc-aarch64.c +++ b/cpukit/libdl/rtl-mdreloc-aarch64.c @@ -73,16 +73,15 @@ __RCSID("$NetBSD: mdreloc.c,v 1.14 2020/06/16 21:01:30 joerg Exp $"); #include <inttypes.h> #include <sys/stat.h> #include <sys/endian.h> -#include <unwind.h> #include <rtems/rtl/rtl.h> #include "rtl-elf.h" #include "rtl-error.h" #include <rtems/rtl/rtl-trace.h> -#include "rtl-unwind.h" +#include <rtems/score/tls.h> -typedef unsigned _Unwind_Word __attribute__((__mode__(__word__))); -typedef _Unwind_Word _uw; +#include "rtl-unwind.h" +#include "rtl-unwind-dw2.h" struct tls_data { size_t td_tlsindex; @@ -110,7 +109,7 @@ checkoverflow(Elf_Addr addr, int bitwidth, Elf_Addr targetaddr, const Elf_Addr mask = ~__BITS(bitwidth - 1, 0); if (((addr & mask) != 0) && ((addr & mask) != mask)) { - printf("kobj_reloc: Relocation 0x%jx too far from %p" + printf("kobj_reloc: Relocation 0x%" PRIxPTR " too far from %p" " (base+0x%jx) for %dbit%s\n", (uintptr_t)targetaddr, where, off, bitwidth, bitscale); return true; @@ -123,7 +122,7 @@ static inline bool checkalign(Elf_Addr addr, int alignbyte, void *where, Elf64_Addr off) { if ((addr & (alignbyte - 1)) != 0) { - printf("kobj_reloc: Relocation 0x%jx unaligned at %p" + printf("kobj_reloc: Relocation 0x%" PRIxPTR " unaligned at %p" " (base+0x%jx). must be aligned %d\n", (uintptr_t)addr, where, off, alignbyte); return true; @@ -164,6 +163,12 @@ get_veneer_size(int type) return 16; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint64_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { @@ -241,6 +246,7 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, * A - the addend of the reolcation * P - the address of the place being relocated (derived from r_offset) * Page(expr) - the page address of the expression expr, defined as (expr & ~0xFFF). + * TPREL(expr) - the thread pointer offset of the expression expr */ switch (ELF_R_TYPE(rela->r_info)) { case R_TYPE(NONE): @@ -259,7 +265,7 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) printf ("rtl: reloc 64/GLOB_DAT in %s --> %p in %s\n", - sect->name, (void *)*where, + sect->name, (void *)(uintptr_t)*where, rtems_rtl_obj_oname (obj)); } break; @@ -272,10 +278,10 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, */ case R_TYPE(RELATIVE): /* Delta(S) + A */ if (!parsing) { - *where = (Elf_Addr)(sect->base + rela->r_addend); + *where = (Elf_Addr)(uintptr_t)(sect->base + rela->r_addend); if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) printf ("rtl: reloc RELATIVE in %s --> %p in %s\n", - sect->name, (void *)*where, + sect->name, (void *)(uintptr_t)*where, rtems_rtl_obj_oname (obj)); } break; @@ -290,11 +296,44 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, printf("rtl: reloc COPY (please report)\n"); break; + case R_AARCH64_ADD_TPREL_HI12: /* TPREL(S + A) */ + case R_AARCH64_ADD_TPREL_LO12: + case R_AARCH64_ADD_TPREL_LO12_NC: + uint32_t of_check = 0; + switch (ELF_R_TYPE(rela->r_info)) { + case R_AARCH64_ADD_TPREL_LO12: + of_check = 212; + /* fallthrough */ + case R_AARCH64_ADD_TPREL_LO12_NC: + shift = 0; + break; + case R_AARCH64_ADD_TPREL_HI12: + of_check = 224; + shift = 12; + break; + default: + printf("illegal rtype: %" PRIu64 "\n", ELF_R_TYPE(rela->r_info)); + break; + } + + if (!parsing) { + target = (Elf_Addr)symvalue; + target >>= shift; + target &= WIDTHMASK(12); + if (of_check && target >= of_check) { + return rtems_rtl_elf_rel_failure; + } + *insn = htole32( + (le32toh(*insn) & ~__BITS(21,10)) | (target << 10)); + } + break; + case R_AARCH64_ADD_ABS_LO12_NC: /* S + A */ case R_AARCH64_LDST8_ABS_LO12_NC: case R_AARCH_LDST16_ABS_LO12_NC: case R_AARCH_LDST32_ABS_LO12_NC: case R_AARCH_LDST64_ABS_LO12_NC: + case R_AARCH64_LDST128_ABS_LO12_NC: switch (ELF_R_TYPE(rela->r_info)) { case R_AARCH64_ADD_ABS_LO12_NC: case R_AARCH64_LDST8_ABS_LO12_NC: @@ -309,8 +348,11 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, case R_AARCH_LDST64_ABS_LO12_NC: shift = 3; break; + case R_AARCH64_LDST128_ABS_LO12_NC: + shift = 4; + break; default: - printf("illegal rtype: %ld\n", ELF_R_TYPE(rela->r_info)); + printf("illegal rtype: %" PRIu64 "\n", ELF_R_TYPE(rela->r_info)); break; } @@ -326,9 +368,9 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, target = (Elf_Addr)symvalue + rela->r_addend; if (checkalign(target, 1 << shift, where, off)) { printf ("rtl: reloc checkalign failed in %s --> %p in %s\n", - sect->name, (void *)*where, + sect->name, (void *)(uintptr_t)*where, rtems_rtl_obj_oname (obj)); - printf("ELF_R_TYPE is : %ld\n", ELF_R_TYPE(rela->r_info)); + printf("ELF_R_TYPE is : %" PRIu64 "\n", ELF_R_TYPE(rela->r_info)); break; } target &= WIDTHMASK(12); @@ -350,6 +392,7 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, target -= (uintptr_t)where >> 12; if (checkoverflow(target, 21, raddr, " x 4k", where, off)) { + printf("]] %d\n", __LINE__); return rtems_rtl_elf_rel_failure; } @@ -380,6 +423,13 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, return rtems_rtl_elf_rel_failure; } + if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) + printf ( + "rtl: JUMP26/PC26/CALL: insn=%p where=%p target=%p raddr=%p parsing=%d\n", + insn, (void*) where, (void*)(uintptr_t) target, (void*)(uintptr_t) raddr, + parsing + ); + target = (intptr_t)target >> 2; if (((Elf_Sword)target > 0x1FFFFFF) || ((Elf_Sword)target < -0x2000000)) { @@ -399,9 +449,8 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, return rtems_rtl_elf_rel_failure; } - tramp_addr = ((Elf_Addr) obj->tramp_brk) | (symvalue & 1); + tramp_addr = ((Elf_Addr)(uintptr_t) obj->tramp_brk) | (symvalue & 1); obj->tramp_brk = set_veneer(obj->tramp_brk, symvalue); - target = tramp_addr + rela->r_addend - (uintptr_t)where; target = (uintptr_t)target >> 2; } @@ -426,6 +475,7 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, raddr = (Elf_Addr)symvalue + rela->r_addend; target = raddr - (uintptr_t)where; if (checkoverflow(target, 32, raddr, "", where, off)) { + printf("]] %d\n", __LINE__); return rtems_rtl_elf_rel_failure; } *where32 = target; @@ -434,30 +484,31 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, case R_TYPE(TLSDESC): printf ("rtl: reloc TLSDESC in %s --> %p in %s\n", - sect->name, (void *)*where, + sect->name, (void *)(uintptr_t)*where, rtems_rtl_obj_oname (obj)); break; case R_TLS_TYPE(TLS_DTPREL): printf ("rtl: reloc TLS_DTPREL in %s --> %p in %s\n", - sect->name, (void *)*where, + sect->name, (void *)(uintptr_t)*where, rtems_rtl_obj_oname (obj)); break; case R_TLS_TYPE(TLS_DTPMOD): printf ("rtl: reloc TLS_DTPMOD in %s --> %p in %s\n", - sect->name, (void *)*where, + sect->name, (void *)(uintptr_t)*where, rtems_rtl_obj_oname (obj)); break; case R_TLS_TYPE(TLS_TPREL): printf ("rtl: reloc TLS_TPREL in %s --> %p in %s\n", - sect->name, (void *)*where, + sect->name, (void *)(uintptr_t)*where, rtems_rtl_obj_oname (obj)); break; default: - printf ("rtl: Unsupported relocation type in %s --> %p in %s\n", - sect->name, (void *)where, + printf ("rtl: Unsupported relocation type (%" PRIu64 + ") in %s --> %p in %s\n", + ELF_R_TYPE(rela->r_info), sect->name, (void *)where, rtems_rtl_obj_oname (obj)); return rtems_rtl_elf_rel_failure; } @@ -526,74 +577,16 @@ rtems_rtl_elf_relocate_rel (rtems_rtl_obj* obj, bool rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj, const char* name, - uint32_t flags) -{ - /* - * We location the EH sections in section flags. - */ - return false; + uint32_t flags) { + return rtems_rtl_elf_unwind_dw2_parse (obj, name, flags); } bool -rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj) -{ - return true; +rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj) { + return rtems_rtl_elf_unwind_dw2_register (obj); } bool -rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj) -{ - obj->loader = NULL; - return true; -} - -/* An exception index table entry. */ -typedef struct __EIT_entry -{ - _uw fnoffset; - _uw content; -} __EIT_entry; - -/* The exception index table location in the base module */ -extern __EIT_entry __exidx_start; -extern __EIT_entry __exidx_end; - -/* - * A weak reference is in libgcc, provide a real version and provide a way to - * manage loaded modules. - * - * Passed in the return address and a reference to the number of records - * found. We set the start of the exidx data and the number of records. - */ -_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, - int* nrec) __attribute__ ((__noinline__, - __used__, - __noclone__)); - -_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, - int* nrec) -{ - rtems_rtl_data* rtl; - rtems_chain_node* node; - __EIT_entry* exidx_start = &__exidx_start; - __EIT_entry* exidx_end = &__exidx_end; - - rtl = rtems_rtl_lock (); - - node = rtems_chain_first (&rtl->objects); - while (!rtems_chain_is_tail (&rtl->objects, node)) { - rtems_rtl_obj* obj = (rtems_rtl_obj*) node; - if (rtems_rtl_obj_text_inside (obj, (void*) return_address)) { - exidx_start = (__EIT_entry*) obj->eh_base; - exidx_end = (__EIT_entry*) (obj->eh_base + obj->eh_size); - break; - } - node = rtems_chain_next (node); - } - - rtems_rtl_unlock (); - - *nrec = exidx_end - exidx_start; - - return (_Unwind_Ptr) exidx_start; +rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj) { + return rtems_rtl_elf_unwind_dw2_deregister (obj); } diff --git a/cpukit/libdl/rtl-mdreloc-arm.c b/cpukit/libdl/rtl-mdreloc-arm.c index 4950dcdab1..16efbe79da 100644 --- a/cpukit/libdl/rtl-mdreloc-arm.c +++ b/cpukit/libdl/rtl-mdreloc-arm.c @@ -12,14 +12,12 @@ #include <string.h> #include <sys/types.h> #include <sys/stat.h> -#include <unwind.h> -#include <unwind-arm-common.h> #include <rtems/rtl/rtl.h> #include "rtl-elf.h" #include "rtl-error.h" #include <rtems/rtl/rtl-trace.h> -#include "rtl-unwind.h" +#include "rtl-unwind-arm.h" /* * Set to 1 to allow untested relocations. If you tested one and it @@ -110,6 +108,12 @@ get_veneer_size(int type) return 8; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { @@ -528,7 +532,6 @@ rtems_rtl_elf_reloc_rel (rtems_rtl_obj* obj, break; case R_TYPE(TLS_LE32): -#if ALLOW_UNTESTED_RELOCS if (!parsing) { addend = *where; *where = symvalue + addend; @@ -537,7 +540,7 @@ rtems_rtl_elf_reloc_rel (rtems_rtl_obj* obj, (void *)*where, where, rtems_rtl_obj_oname (obj)); } break; -#endif + case R_TYPE(TLS_GD32): case R_TYPE(TLS_LDM32): case R_TYPE(TLS_LDO32): @@ -597,78 +600,3 @@ rtems_rtl_elf_relocate_rel (rtems_rtl_obj* obj, symvalue, false); } - -bool -rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj, - const char* name, - uint32_t flags) -{ - /* - * We location the EH sections in section flags. - */ - return false; -} - -bool -rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj) -{ - return true; -} - -bool -rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj) -{ - obj->loader = NULL; - return true; -} - -/* An exception index table entry. */ -typedef struct __EIT_entry -{ - _uw fnoffset; - _uw content; -} __EIT_entry; - -/* The exception index table location in the base module */ -extern __EIT_entry __exidx_start; -extern __EIT_entry __exidx_end; - -/* - * A weak reference is in libgcc, provide a real version and provide a way to - * manage loaded modules. - * - * Passed in the return address and a reference to the number of records - * found. We set the start of the exidx data and the number of records. - */ -_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, - int* nrec) __attribute__ ((__noinline__, - __used__, - __noclone__)); - -_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, - int* nrec) -{ - rtems_rtl_data* rtl; - rtems_chain_node* node; - __EIT_entry* exidx_start = &__exidx_start; - __EIT_entry* exidx_end = &__exidx_end; - - rtl = rtems_rtl_lock (); - - node = rtems_chain_first (&rtl->objects); - while (!rtems_chain_is_tail (&rtl->objects, node)) { - rtems_rtl_obj* obj = (rtems_rtl_obj*) node; - if (rtems_rtl_obj_text_inside (obj, (void*) return_address)) { - exidx_start = (__EIT_entry*) obj->eh_base; - exidx_end = (__EIT_entry*) (obj->eh_base + obj->eh_size); - break; - } - node = rtems_chain_next (node); - } - - rtems_rtl_unlock (); - - *nrec = exidx_end - exidx_start; - - return (_Unwind_Ptr) exidx_start; -} diff --git a/cpukit/libdl/rtl-mdreloc-bfin.c b/cpukit/libdl/rtl-mdreloc-bfin.c index f46b86ce34..cf3f3f88c0 100644 --- a/cpukit/libdl/rtl-mdreloc-bfin.c +++ b/cpukit/libdl/rtl-mdreloc-bfin.c @@ -99,6 +99,12 @@ load_ptr(void *where) return (res); } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { diff --git a/cpukit/libdl/rtl-mdreloc-i386.c b/cpukit/libdl/rtl-mdreloc-i386.c index 40b09d4be2..09cbe4f42f 100644 --- a/cpukit/libdl/rtl-mdreloc-i386.c +++ b/cpukit/libdl/rtl-mdreloc-i386.c @@ -64,6 +64,12 @@ rtems_rtl_elf_rel_resolve_sym (Elf_Word type) return true; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { diff --git a/cpukit/libdl/rtl-mdreloc-lm32.c b/cpukit/libdl/rtl-mdreloc-lm32.c index de0609345f..af904cd126 100644 --- a/cpukit/libdl/rtl-mdreloc-lm32.c +++ b/cpukit/libdl/rtl-mdreloc-lm32.c @@ -92,6 +92,12 @@ rtems_rtl_elf_rel_resolve_sym (Elf_Word type) return true; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { diff --git a/cpukit/libdl/rtl-mdreloc-m68k.c b/cpukit/libdl/rtl-mdreloc-m68k.c index 873574ef7c..a88d612c28 100644 --- a/cpukit/libdl/rtl-mdreloc-m68k.c +++ b/cpukit/libdl/rtl-mdreloc-m68k.c @@ -77,6 +77,12 @@ rtems_rtl_elf_rel_resolve_sym (Elf_Word type) return true; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { diff --git a/cpukit/libdl/rtl-mdreloc-microblaze.c b/cpukit/libdl/rtl-mdreloc-microblaze.c new file mode 100644 index 0000000000..0956e22bc7 --- /dev/null +++ b/cpukit/libdl/rtl-mdreloc-microblaze.c @@ -0,0 +1,298 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +/* + * Copyright (C) 2023 On-Line Applications Research Corporation (OAR) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> + +#include <errno.h> +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include <rtems/rtl/rtl.h> +#include "rtl-elf.h" +#include "rtl-error.h" +#include <rtems/rtl/rtl-trace.h> +#include "rtl-unwind.h" +#include "rtl-unwind-dw2.h" + +uint32_t +rtems_rtl_elf_section_flags (const rtems_rtl_obj* obj, + const Elf_Shdr* shdr) { + (void) obj; + (void) shdr; + return 0; +} + +uint32_t +rtems_rtl_elf_arch_parse_section (const rtems_rtl_obj* obj, + int section, + const char* name, + const Elf_Shdr* shdr, + const uint32_t flags) { + (void) obj; + (void) section; + (void) name; + (void) shdr; + return flags; +} + +bool +rtems_rtl_elf_arch_section_alloc (const rtems_rtl_obj* obj, + rtems_rtl_obj_sect* sect) { + (void) obj; + (void) sect; + return false; +} + +bool +rtems_rtl_elf_arch_section_free (const rtems_rtl_obj* obj, + rtems_rtl_obj_sect* sect) { + (void) obj; + (void) sect; + return false; +} + +bool +rtems_rtl_elf_rel_resolve_sym (Elf_Word type) { + return type != 0; +} + +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + +size_t +rtems_rtl_elf_relocate_tramp_max_size (void) { + /* + * Disable by returning 0. + */ + return 0; +} + +rtems_rtl_elf_rel_status +rtems_rtl_elf_relocate_rel_tramp (rtems_rtl_obj* obj, + const Elf_Rel* rel, + const rtems_rtl_obj_sect* sect, + const char* symname, + const Elf_Byte syminfo, + const Elf_Word symvalue) { + (void) obj; + (void) rel; + (void) sect; + (void) symname; + (void) syminfo; + (void) symvalue; + return rtems_rtl_elf_rel_no_error; +} + +static void write16le(void *loc, uint16_t val) { + *((uint16_t *) loc) = val; +} + +static void write32le(void *loc, uint32_t val) { + *((uint32_t *) loc) = val; +} + +static uint16_t read16le(void *loc) { + return *((uint16_t *) loc); +} + +static uint32_t read32le(void *loc) { + return *((uint32_t *) loc); +} + +static rtems_rtl_elf_rel_status +rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, + const Elf_Rela* rela, + const rtems_rtl_obj_sect* sect, + const char* symname, + const Elf_Byte syminfo, + const Elf_Word symvalue, + const bool parsing) { + Elf_Word *where; + Elf_Word addend = (Elf_Word)rela->r_addend; + Elf_Addr target; + + where = (Elf_Addr *)(sect->base + rela->r_offset); + + /* Target value */ + target = (Elf_Addr)symvalue + addend; + /* Final PCREL value */ + Elf_Word pcrel_val = target - ((Elf_Word)where); + + if (parsing) { + return rtems_rtl_elf_rel_no_error; + } + + switch (ELF_R_TYPE(rela->r_info)) { + case R_TYPE(NONE): + break; + + case R_TYPE(64): + /* Set the lower 16 bits of where to the upper 16 bits of target */ + write16le(where, + (read16le(where) & 0xFFFF0000) | (target >> 16)); + /* Set the lower 16 bits of where + 4 to the lower 16 bits of target */ + write16le(where + 1, + (read16le(where + 1) & 0xFFFF0000) | (target & 0xFFFF)); + break; + + case R_TYPE(32): + { + uintptr_t addr = (uintptr_t)where; + if ((uintptr_t)where & 3) { + /* `where` is not aligned to a 4-byte boundary. */ + uintptr_t addr_down = addr & ~3; + uintptr_t addr_up = (addr + 3) & ~3; + + uint32_t value_down = read32le((void*)addr_down); + uint32_t value_up = read32le((void*)addr_up); + + /* + * Calculate how many bytes `where` is offset from the previous 4-byte + * boundary. + */ + unsigned offset = addr & 3; + + /* + * Combine `target` with `value_down` and `value_up` to form the new + * values to write. + */ + uint32_t new_value_down = (value_down & ((1 << (offset * 8)) - 1)) | + (target << (offset * 8)); + uint32_t new_value_up = (value_up & ~((1 << (offset * 8)) - 1)) | + (target >> ((4 - offset) * 8)); + + write32le((void*)addr_down, new_value_down); + write32le((void*)addr_up, new_value_up); + } else { + write32le(where, target); + } + } + break; + + case R_TYPE(32_PCREL): + write32le(where, pcrel_val); + + if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) + printf ("rtl: R_MICROBLAZE_32_PCREL %p @ %p in %s\n", + (void *) * (where), where, rtems_rtl_obj_oname (obj)); + break; + + case R_TYPE(64_PCREL): + /* + * Compensate for the fact that the PC is 4 bytes ahead of the instruction + */ + pcrel_val -= 4; + /* Set the lower 16 bits of where to the upper 16 bits of target */ + write16le(where, + (read16le(where) & 0xFFFF0000) | (pcrel_val >> 16)); + /* Set the lower 16 bits of where + 4 to the lower 16 bits of target */ + write16le(where + 1, + (read16le(where + 1) & 0xFFFF0000) | (pcrel_val & 0xFFFF)); + + if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) + printf ("rtl: R_MICROBLAZE_64_PCREL %p @ %p in %s\n", + (void *) * (where), where, rtems_rtl_obj_oname (obj)); + break; + + case R_TYPE(32_PCREL_LO): + write16le(where, (read16le(where) & 0xFFFF0000) | (pcrel_val & 0xFFFF)); + break; + + default: + rtems_rtl_set_error (EINVAL, + "%s: Unsupported relocation type %d " + "in non-PLT relocations", + sect->name, (uint32_t) ELF_R_TYPE(rela->r_info)); + return rtems_rtl_elf_rel_failure; + } + + return rtems_rtl_elf_rel_no_error; +} + +rtems_rtl_elf_rel_status +rtems_rtl_elf_relocate_rela (rtems_rtl_obj* obj, + const Elf_Rela* rela, + const rtems_rtl_obj_sect* sect, + const char* symname, + const Elf_Byte syminfo, + const Elf_Word symvalue) { + return rtems_rtl_elf_reloc_rela (obj, + rela, + sect, + symname, + syminfo, + symvalue, + false); +} + +rtems_rtl_elf_rel_status +rtems_rtl_elf_relocate_rela_tramp (rtems_rtl_obj* obj, + const Elf_Rela* rela, + const rtems_rtl_obj_sect* sect, + const char* symname, + const Elf_Byte syminfo, + const Elf_Word symvalue) { + return rtems_rtl_elf_reloc_rela (obj, + rela, + sect, + symname, + syminfo, + symvalue, + true); +} + +rtems_rtl_elf_rel_status +rtems_rtl_elf_relocate_rel (rtems_rtl_obj* obj, + const Elf_Rel* rel, + const rtems_rtl_obj_sect* sect, + const char* symname, + const Elf_Byte syminfo, + const Elf_Word symvalue) { + rtems_rtl_set_error (EINVAL, "rel type record not supported"); + return rtems_rtl_elf_rel_failure; +} + +bool +rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj, + const char* name, + uint32_t flags) { + return rtems_rtl_elf_unwind_dw2_parse (obj, name, flags); +} + +bool +rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj) { + return rtems_rtl_elf_unwind_dw2_register (obj); +} + +bool +rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj) { + return rtems_rtl_elf_unwind_dw2_deregister (obj); +} diff --git a/cpukit/libdl/rtl-mdreloc-mips.c b/cpukit/libdl/rtl-mdreloc-mips.c index d43b144895..91d88c9986 100644 --- a/cpukit/libdl/rtl-mdreloc-mips.c +++ b/cpukit/libdl/rtl-mdreloc-mips.c @@ -93,6 +93,12 @@ rtems_rtl_elf_rel_resolve_sym (Elf_Word type) return true; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { diff --git a/cpukit/libdl/rtl-mdreloc-moxie.c b/cpukit/libdl/rtl-mdreloc-moxie.c index 6238ac99d2..a3cfe63b6a 100644 --- a/cpukit/libdl/rtl-mdreloc-moxie.c +++ b/cpukit/libdl/rtl-mdreloc-moxie.c @@ -93,6 +93,12 @@ rtems_rtl_elf_rel_resolve_sym (Elf_Word type) return true; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { diff --git a/cpukit/libdl/rtl-mdreloc-powerpc.c b/cpukit/libdl/rtl-mdreloc-powerpc.c index c6062c872a..09c2ab1512 100644 --- a/cpukit/libdl/rtl-mdreloc-powerpc.c +++ b/cpukit/libdl/rtl-mdreloc-powerpc.c @@ -207,6 +207,12 @@ rtems_rtl_elf_rel_resolve_sym (Elf_Word type) return true; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { @@ -348,37 +354,47 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, break; case R_TYPE(16_HA): + case R_TYPE(TPREL16_HA): /* * value:6; Field:half16; Expression: #ha(S+A) + * value:72; Field:half16; Expression: #ha(S+A) */ if (!parsing) { tmp = symvalue + rela->r_addend; *(uint16_t *)where = (((tmp >> 16) + ((tmp & 0x8000) ? 1: 0)) & 0xffff); if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) - printf ("rtl: 16_HA %p @ %p in %s\n", + printf ("rtl: %s16_HA %p @ %p in %s\n", + ELF_R_TYPE(rela->r_info) == R_TYPE(TPREL16_HA) ? "TPREL" : "", (void *)*(where), where, rtems_rtl_obj_oname (obj)); } break; case R_TYPE(16_HI): + case R_TYPE(TPREL16_HI): /* * value:5; Field:half16; Expression: #hi(S+A) + * value:71; Field:half16; Expression: #hi(S+A) */ if (!parsing) { *(uint16_t *)where = ((symvalue + rela->r_addend) >> 16) & 0xffff; if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) - printf ("rtl: 16_HI %p @ %p in %s\n", + printf ("rtl: %s16_HI %p @ %p in %s\n", + ELF_R_TYPE(rela->r_info) == R_TYPE(TPREL16_HI) ? "TPREL" : "", (void *)*where, where, rtems_rtl_obj_oname (obj)); } break; + case R_TYPE(16_LO): + case R_TYPE(TPREL16_LO): /* * value:4; Field:half16; Expression: #lo(S+A) + * value:71; Field:half16; Expression: #lo(S+A) */ if (!parsing) { *(uint16_t *)where = (symvalue + (rela->r_addend)) & 0xffff; if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) - printf ("rtl: 16_LO %p @ %p in %s\n", + printf ("rtl: %s16_LO %p @ %p in %s\n", + ELF_R_TYPE(rela->r_info) == R_TYPE(TPREL16_LO) ? "TPREL" : "", (void *)*where, where, rtems_rtl_obj_oname (obj)); } break; @@ -484,7 +500,7 @@ rtems_rtl_elf_reloc_rela (rtems_rtl_obj* obj, (void *)rela->r_offset, (void *)*where); rtems_rtl_set_error (EINVAL, "%s: Unsupported relocation type %" PRId32 - "in non-PLT relocations", + " in non-PLT relocations", sect->name, (uint32_t) ELF_R_TYPE(rela->r_info)); return rtems_rtl_elf_rel_failure; } diff --git a/cpukit/libdl/rtl-mdreloc-riscv.c b/cpukit/libdl/rtl-mdreloc-riscv.c index e6778dcc90..c0b7556366 100644 --- a/cpukit/libdl/rtl-mdreloc-riscv.c +++ b/cpukit/libdl/rtl-mdreloc-riscv.c @@ -89,6 +89,12 @@ rtems_rtl_elf_rel_resolve_sym (Elf_Word type) { return true; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { /* diff --git a/cpukit/libdl/rtl-mdreloc-sparc.c b/cpukit/libdl/rtl-mdreloc-sparc.c index f8a4312a8a..9810de988e 100644 --- a/cpukit/libdl/rtl-mdreloc-sparc.c +++ b/cpukit/libdl/rtl-mdreloc-sparc.c @@ -87,46 +87,66 @@ static const uint32_t reloc_target_flags[] = { _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */ _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */ _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */ - _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */ - _RF_SZ(32) | _RF_RS(0), /* COPY */ + _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */ + _RF_SZ(32) | _RF_RS(0), /* COPY */ _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_DAT */ - _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */ - _RF_A| _RF_B|_RF_SZ(32) | _RF_RS(0), /* RELATIVE */ - _RF_S|_RF_A| _RF_U|_RF_SZ(32) | _RF_RS(0), /* UA_32 */ + _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */ + _RF_A| _RF_B| _RF_SZ(32) | _RF_RS(0), /* RELATIVE */ + _RF_S|_RF_A|_RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */ + + /* TLS and 64 bit relocs not listed here... */ }; +#define RELOC_TARGET_FLAGS_SIZE \ + (sizeof(reloc_target_flags) / sizeof(reloc_target_flags[0])) -#define NOT_CURRENTLY_USED_BUT_MAYBE_USEFUL -#ifdef NOT_CURRENTLY_USED_BUT_MAYBE_USEFUL +#define RTLD_DEBUG_RELOC +#ifdef RTLD_DEBUG_RELOC static const char *reloc_names[] = { "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8", "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22", "22", "13", "LO10", "GOT10", "GOT13", "GOT22", "PC10", "PC22", "WPLT30", "COPY", - "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32" + "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", + + /* not used with 32bit userland, besides a few of the TLS ones */ + "PLT32", + "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32", + "10", "11", "64", "OLO10", "HH22", + "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22", + "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6", + "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44", + "L44", "REGISTER", "UA64", "UA16", + "TLS_GD_HI22", "TLS_GD_LO10", "TLS_GD_ADD", "TLS_GD_CALL", + "TLS_LDM_HI22", "TLS_LDM_LO10", "TLS_LDM_ADD", "TLS_LDM_CALL", + "TLS_LDO_HIX22", "TLS_LDO_LOX10", "TLS_LDO_ADD", "TLS_IE_HI22", + "TLS_IE_LO10", "TLS_IE_LD", "TLS_IE_LDX", "TLS_IE_ADD", "TLS_LE_HIX22", + "TLS_LE_LOX10", "TLS_DTPMOD32", "TLS_DTPMOD64", "TLS_DTPOFF32", + "TLS_DTPOFF64", "TLS_TPOFF32", "TLS_TPOFF64", }; #endif -#define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) -#define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) -#define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) -#define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) -#define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) -#define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) -#define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) +#define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) +#define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) +#define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) +#define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) +#define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) +#define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) +#define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) +#define RELOC_TLS(t) (t >= R_TYPE(TLS_GD_HI22)) static const int reloc_target_bitmask[] = { #define _BM(x) (~(-(1ULL << (x)))) - 0, /* NONE */ - _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */ - _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */ - _BM(30), _BM(22), /* WDISP30, WDISP22 */ - _BM(22), _BM(22), /* HI22, _22 */ - _BM(13), _BM(10), /* RELOC_13, _LO10 */ - _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */ - _BM(10), _BM(22), /* _PC10, _PC22 */ - _BM(30), 0, /* _WPLT30, _COPY */ - -1, -1, -1, /* _GLOB_DAT, JMP_SLOT, _RELATIVE */ - _BM(32) /* _UA32 */ + 0, /* NONE */ + _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */ + _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */ + _BM(30), _BM(22), /* WDISP30, WDISP22 */ + _BM(22), _BM(22), /* HI22, _22 */ + _BM(13), _BM(10), /* RELOC_13, _LO10 */ + _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */ + _BM(10), _BM(22), /* _PC10, _PC22 */ + _BM(30), 0, /* _WPLT30, _COPY */ + -1, -1, -1, /* _GLOB_DAT, JMP_SLOT, _RELATIVE */ + _BM(32) /* _UA32 */ #undef _BM }; #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) @@ -173,7 +193,26 @@ rtems_rtl_elf_arch_section_free (const rtems_rtl_obj* obj, bool rtems_rtl_elf_rel_resolve_sym (Elf_Word type) { - return RELOC_RESOLVE_SYMBOL (type) ? true : false; + bool r = false; + if (type < RELOC_TARGET_FLAGS_SIZE) { + r = RELOC_RESOLVE_SYMBOL (type) ? true : false; + } + switch (type) { + case R_TYPE(TLS_DTPOFF32): + case R_TYPE(TLS_LE_HIX22): + case R_TYPE(TLS_LE_LOX10): + r = true; + break; + default: + break; + } + return r; +} + +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); } size_t @@ -241,6 +280,73 @@ rtems_rtl_elf_relocate_rela (rtems_rtl_obj* obj, value = rela->r_addend; /* + * Handle TLS relocations here, they are different. + */ + if (RELOC_TLS(type)) { + switch (type) { + case R_TYPE(TLS_DTPMOD32): + #if NETBSD_CODE__NOT_USED + *where = (Elf_Addr)defobj->tlsindex; + #endif + + if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) + printf("rtl: reloc: TLS_DTPMOD32 %s in %s --> %p\n", + symname, rtems_rtl_obj_oname (obj), (void *)*where); + break; + + case R_TYPE(TLS_DTPOFF32): + *where = (Elf_Addr)(symvalue + rela->r_addend); + + if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) + printf("rtl: reloc: TLS_DTPOFF32 %s in %s --> %p\n", + symname, rtems_rtl_obj_oname (obj), (void *)*where); + break; + + case R_TYPE(TLS_TPOFF32): + #if NETBSD_CODE__NOT_USED + if (!defobj->tls_static && + _rtld_tls_offset_allocate(__UNCONST(defobj))) + return ; + + *where = (Elf_Addr)(def->st_value - + defobj->tlsoffset + rela->r_addend); + #endif + if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) + printf("rtl: reloc: TLS_TPOFF32 %s in %s --> %p\n", + symname, rtems_rtl_obj_oname (obj), (void *)*where); + return rtems_rtl_elf_rel_failure; + + case R_TYPE(TLS_LE_HIX22): + *where |= ((symvalue + rela->r_addend) ^ 0xffffffff) >> 10; + if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) + printf("rtl: reloc: R_SPARC_TLS_LE_HIX22 %s in %s --> %p\n", + symname, rtems_rtl_obj_oname (obj), (void *)*where); + break; + + case R_TYPE(TLS_LE_LOX10): + *where |= ((symvalue + rela->r_addend) & 0x3ff) | 0x1c00; + if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) + printf("rtl: reloc: R_SPARC_TLS_LE_LOX10 %s in %s --> %p\n", + symname, rtems_rtl_obj_oname (obj), (void *)*where); + break; + + default: + if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) + printf("rtl: reloc: unknown TLS relo: %d for %s in %s --> %p\n", + type, symname, rtems_rtl_obj_oname (obj), (void *)*where); + return rtems_rtl_elf_rel_failure; + } + return rtems_rtl_elf_rel_no_error; + } + + /* + * If it is no TLS relocation (handled above), we can not + * deal with it if it is beyond R_SPARC_6. + */ + if (type > R_TYPE(6)) + return rtems_rtl_elf_rel_failure; + + /* * Handle relative relocs here, as an optimization. */ if (type == R_TYPE (RELATIVE)) { diff --git a/cpukit/libdl/rtl-mdreloc-v850.c b/cpukit/libdl/rtl-mdreloc-v850.c index 8d1f3c02a7..826763a2b5 100644 --- a/cpukit/libdl/rtl-mdreloc-v850.c +++ b/cpukit/libdl/rtl-mdreloc-v850.c @@ -93,6 +93,12 @@ rtems_rtl_elf_rel_resolve_sym (Elf_Word type) return true; } +uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj) +{ + (void) obj; + return sizeof(uint32_t); +} + size_t rtems_rtl_elf_relocate_tramp_max_size (void) { diff --git a/cpukit/libdl/rtl-obj.c b/cpukit/libdl/rtl-obj.c index 24d1e09048..c99e9f703f 100644 --- a/cpukit/libdl/rtl-obj.c +++ b/cpukit/libdl/rtl-obj.c @@ -137,7 +137,6 @@ rtems_rtl_obj_free (rtems_rtl_obj* obj) rtems_rtl_obj_erase_sections (obj); rtems_rtl_obj_erase_dependents (obj); rtems_rtl_symbol_obj_erase (obj); - rtems_rtl_obj_erase_trampoline (obj); rtems_rtl_obj_free_names (obj); if (obj->sec_num != NULL) free (obj->sec_num); @@ -600,26 +599,6 @@ rtems_rtl_obj_find_section_by_mask (const rtems_rtl_obj* obj, } bool -rtems_rtl_obj_alloc_trampoline (rtems_rtl_obj* obj) -{ - if (obj->tramps_size == 0) - return true; - obj->trampoline = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, - obj->tramps_size, - true); - if (obj->trampoline == NULL) - rtems_rtl_set_error (ENOMEM, "no memory for the trampoline"); - obj->tramp_brk = obj->trampoline; - return obj->trampoline != NULL; -} - -void -rtems_rtl_obj_erase_trampoline (rtems_rtl_obj* obj) -{ - rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, obj->trampoline); -} - -bool rtems_rtl_obj_alloc_dependents (rtems_rtl_obj* obj, size_t dependents) { rtems_rtl_obj_depends* depends; @@ -828,6 +807,12 @@ rtems_rtl_obj_bss_size (const rtems_rtl_obj* obj) return rtems_rtl_obj_section_size (obj, RTEMS_RTL_OBJ_SECT_BSS); } +size_t +rtems_rtl_obj_tramp_size (const rtems_rtl_obj* obj) +{ + return obj->tramp_slots * obj->tramp_slot_size; +} + uint32_t rtems_rtl_obj_bss_alignment (const rtems_rtl_obj* obj) { @@ -919,10 +904,10 @@ rtems_rtl_obj_synchronize_cache (rtems_rtl_obj* obj) size); } - if (obj->trampoline != NULL) + if (obj->tramp_base != NULL) { - rtems_cache_instruction_sync_after_code_change(obj->trampoline, - obj->tramps_size); + rtems_cache_instruction_sync_after_code_change(obj->tramp_base, + obj->tramp_size); } } @@ -1047,6 +1032,7 @@ rtems_rtl_obj_sections_locate (uint32_t mask, { base_offset = rtems_rtl_obj_align (base_offset, sect->alignment); sect->base = base + base_offset; + base_offset += sect->size; } if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT)) @@ -1055,9 +1041,6 @@ rtems_rtl_obj_sections_locate (uint32_t mask, order, sect->name, sect->base, sect->size, sect->flags, sect->alignment, sect->link); - if (sect->base) - base_offset += sect->size; - ++order; node = rtems_chain_first (sections); @@ -1069,34 +1052,95 @@ rtems_rtl_obj_sections_locate (uint32_t mask, } } -bool -rtems_rtl_obj_alloc_sections (rtems_rtl_obj* obj, - int fd, - rtems_rtl_obj_sect_handler handler, - void* data) +static void +rtems_rtl_obj_set_sizes (rtems_rtl_obj* obj) { size_t text_size; + size_t tramp_size; size_t const_size; size_t eh_size; size_t data_size; size_t bss_size; - text_size = rtems_rtl_obj_text_size (obj) + rtems_rtl_obj_const_alignment (obj); - const_size = rtems_rtl_obj_const_size (obj) + rtems_rtl_obj_eh_alignment (obj); - eh_size = rtems_rtl_obj_eh_size (obj) + rtems_rtl_obj_data_alignment (obj); - data_size = rtems_rtl_obj_data_size (obj) + rtems_rtl_obj_bss_alignment (obj); - bss_size = rtems_rtl_obj_bss_size (obj); + /* + * The allocator may not align memory to the required boundary. Add + * the alignment size to the size allocated. + */ + text_size = rtems_rtl_obj_text_size (obj) + rtems_rtl_obj_text_alignment (obj); + tramp_size = rtems_rtl_obj_tramp_size (obj); + if (tramp_size != 0) + tramp_size += rtems_rtl_obj_tramp_alignment (obj); + const_size = rtems_rtl_obj_const_size (obj) + rtems_rtl_obj_const_alignment (obj); + eh_size = rtems_rtl_obj_eh_size (obj) + rtems_rtl_obj_eh_alignment (obj); + data_size = rtems_rtl_obj_data_size (obj) + rtems_rtl_obj_data_alignment (obj); + bss_size = rtems_rtl_obj_bss_size (obj) + rtems_rtl_obj_bss_alignment (obj); /* * Set the sizes held in the object data. We need this for a fast reference. */ - obj->text_size = text_size; + obj->text_size = text_size + tramp_size; + obj->tramp_size = tramp_size; obj->const_size = const_size; obj->data_size = data_size; obj->eh_size = eh_size; obj->bss_size = bss_size; + obj->exec_size = text_size + const_size + eh_size + data_size + bss_size; +} + +static void +rtems_rtl_obj_print_sizes (rtems_rtl_obj* obj, const char* label) +{ + if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT)) + { + printf ("rtl: %s sect: text - b:%p s:%zi a:%" PRIu32 "\n", + label, obj->text_base, obj->text_size, rtems_rtl_obj_text_alignment (obj)); + printf ("rtl: %s sect: tramp - b:%p s:%zi a:%" PRIu32 "\n", + label, obj->tramp_base, obj->tramp_size, rtems_rtl_obj_tramp_alignment (obj)); + printf ("rtl: %s sect: const - b:%p s:%zi a:%" PRIu32 "\n", + label, obj->const_base, obj->const_size, rtems_rtl_obj_const_alignment (obj)); + printf ("rtl: %s sect: eh - b:%p s:%zi a:%" PRIu32 "\n", + label, obj->eh_base, obj->eh_size, rtems_rtl_obj_eh_alignment (obj)); + printf ("rtl: %s sect: data - b:%p s:%zi a:%" PRIu32 "\n", + label, obj->data_base, obj->data_size, rtems_rtl_obj_data_alignment (obj)); + printf ("rtl: %s sect: bss - b:%p s:%zi a:%" PRIu32 "\n", + label, obj->bss_base, obj->bss_size, rtems_rtl_obj_bss_alignment (obj)); + } +} + +static void +rtems_rtl_obj_locate (rtems_rtl_obj* obj) +{ /* + * Locate all text, data and bss sections in seperate operations so each type of + * section is grouped together. + */ + rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_TEXT, + rtems_rtl_alloc_text_tag (), + obj, obj->text_base); + rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_CONST, + rtems_rtl_alloc_const_tag (), + obj, obj->const_base); + rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_EH, + rtems_rtl_alloc_eh_tag (), + obj, obj->eh_base); + rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_DATA, + rtems_rtl_alloc_data_tag (), + obj, obj->data_base); + rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_BSS, + rtems_rtl_alloc_bss_tag (), + obj, obj->bss_base); +} + +bool +rtems_rtl_obj_alloc_sections (rtems_rtl_obj* obj, + int fd, + rtems_rtl_obj_sect_handler handler, + void* data) +{ + rtems_rtl_obj_set_sizes (obj); + + /* * Perform any specific allocations for sections. */ if (handler != NULL) @@ -1116,33 +1160,28 @@ rtems_rtl_obj_alloc_sections (rtems_rtl_obj* obj, * Let the allocator manage the actual allocation. The user can use the * standard heap or provide a specific allocator with memory protection. */ - if (!rtems_rtl_alloc_module_new (&obj->text_base, text_size, - &obj->const_base, const_size, - &obj->eh_base, eh_size, - &obj->data_base, data_size, - &obj->bss_base, bss_size)) + if (!rtems_rtl_alloc_module_new (&obj->text_base, obj->text_size, + &obj->const_base, obj->const_size, + &obj->eh_base, obj->eh_size, + &obj->data_base, obj->data_size, + &obj->bss_base, obj->bss_size)) { obj->exec_size = 0; rtems_rtl_set_error (ENOMEM, "no memory to load obj"); return false; } - obj->exec_size = text_size + const_size + eh_size + data_size + bss_size; - - if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT)) + /* + * Set the trampoline base if there are trampolines + */ + if (obj->tramp_size != 0) { - printf ("rtl: load sect: text - b:%p s:%zi a:%" PRIu32 "\n", - obj->text_base, text_size, rtems_rtl_obj_text_alignment (obj)); - printf ("rtl: load sect: const - b:%p s:%zi a:%" PRIu32 "\n", - obj->const_base, const_size, rtems_rtl_obj_const_alignment (obj)); - printf ("rtl: load sect: eh - b:%p s:%zi a:%" PRIu32 "\n", - obj->eh_base, eh_size, rtems_rtl_obj_eh_alignment (obj)); - printf ("rtl: load sect: data - b:%p s:%zi a:%" PRIu32 "\n", - obj->data_base, data_size, rtems_rtl_obj_data_alignment (obj)); - printf ("rtl: load sect: bss - b:%p s:%zi a:%" PRIu32 "\n", - obj->bss_base, bss_size, rtems_rtl_obj_bss_alignment (obj)); + obj->tramp_base = obj->tramp_brk = + obj->text_base + obj->text_size - obj->tramp_size; } + rtems_rtl_obj_print_sizes (obj, "alloc"); + /* * Determine the load order. */ @@ -1153,24 +1192,48 @@ rtems_rtl_obj_alloc_sections (rtems_rtl_obj* obj, rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_BSS, obj); /* - * Locate all text, data and bss sections in seperate operations so each type of - * section is grouped together. + * Locate the sections to the allocated section bases */ - rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_TEXT, - rtems_rtl_alloc_text_tag (), - obj, obj->text_base); - rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_CONST, - rtems_rtl_alloc_const_tag (), - obj, obj->const_base); - rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_EH, - rtems_rtl_alloc_eh_tag (), - obj, obj->eh_base); - rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_DATA, - rtems_rtl_alloc_data_tag (), - obj, obj->data_base); - rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_BSS, - rtems_rtl_alloc_bss_tag (), - obj, obj->bss_base); + rtems_rtl_obj_locate (obj); + + return true; +} + +bool +rtems_rtl_obj_resize_sections (rtems_rtl_obj* obj) +{ + rtems_rtl_obj_set_sizes (obj); + + /* + * Let the allocator manage the resizing. + */ + if (!rtems_rtl_alloc_module_resize (&obj->text_base, obj->text_size, + &obj->const_base, obj->const_size, + &obj->eh_base, obj->eh_size, + &obj->data_base, obj->data_size, + &obj->bss_base, obj->bss_size)) + { + rtems_rtl_obj_free (obj); + obj->exec_size = 0; + rtems_rtl_set_error (ENOMEM, "no memory resize obj"); + return false; + } + + /* + * Set the trampoline base if there are trampolines + */ + if (obj->tramp_size != 0) + { + obj->tramp_base = obj->tramp_brk = + obj->text_base + obj->text_size - obj->tramp_size; + } + + rtems_rtl_obj_print_sizes (obj, "resize"); + + /* + * Locate the sections to the allocated section bases + */ + rtems_rtl_obj_locate (obj); return true; } diff --git a/cpukit/libdl/rtl-rap.c b/cpukit/libdl/rtl-rap.c index 64ee2947a3..3fd1428bf2 100644 --- a/cpukit/libdl/rtl-rap.c +++ b/cpukit/libdl/rtl-rap.c @@ -336,7 +336,7 @@ rtems_rtl_rap_relocate (rtems_rtl_rap* rap, rtems_rtl_obj* obj) " addend=%" PRIu32 "\n", r, info, offset, addend); - type = info & 0xff; + type = ELF_R_TYPE(info); if ((info & (1 << 31)) == 0) { @@ -350,7 +350,7 @@ rtems_rtl_rap_relocate (rtems_rtl_rap* rap, rtems_rtl_obj* obj) return false; } - symvalue = (Elf_Addr) symsect->base + addend; + symvalue = (Elf_Addr)(uintptr_t) symsect->base + addend; } else if (rtems_rtl_elf_rel_resolve_sym (type)) { @@ -390,7 +390,7 @@ rtems_rtl_rap_relocate (rtems_rtl_rap* rap, rtems_rtl_obj* obj) return false; } - symvalue = (Elf_Addr) symbol->value; + symvalue = (Elf_Addr)(uintptr_t) symbol->value; } if (is_rela) diff --git a/cpukit/libdl/rtl-shell.c b/cpukit/libdl/rtl-shell.c index 69de6bad83..18f1e08901 100644 --- a/cpukit/libdl/rtl-shell.c +++ b/cpukit/libdl/rtl-shell.c @@ -572,7 +572,7 @@ rtems_rtl_obj_printer (rtems_rtl_obj_print* print, rtems_rtl_obj* obj) rtems_printf (print->printer, "%-*cslots : %zu\n", indent + 4, ' ', slots); rtems_printf (print->printer, "%-*csize : %zu\n", indent + 4, ' ', - obj->tramps_size); + obj->tramp_size); rtems_printf (print->printer, "%-*cslot size : %zu\n", indent + 4, ' ', obj->tramp_size); rtems_printf (print->printer, "%-*cused : %zu\n", indent + 4, ' ', diff --git a/cpukit/libdl/rtl-sym.c b/cpukit/libdl/rtl-sym.c index 5c9c3981df..bd203fd158 100644 --- a/cpukit/libdl/rtl-sym.c +++ b/cpukit/libdl/rtl-sym.c @@ -77,6 +77,22 @@ rtems_rtl_symbol_global_insert (rtems_rtl_symbols* symbols, &symbol->node); } +static rtems_rtl_tls_offset* +rtems_rtl_symbol_find_tls_offset (size_t index, + rtems_rtl_tls_offset* tls_offsets, + size_t tls_size) +{ + size_t entry; + for (entry = 0; entry < tls_size; ++entry) + { + if (tls_offsets[entry].index == index) + { + return &tls_offsets[entry]; + } + } + return NULL; +} + bool rtems_rtl_symbol_table_open (rtems_rtl_symbols* symbols, size_t buckets) @@ -103,9 +119,11 @@ rtems_rtl_symbol_table_close (rtems_rtl_symbols* symbols) } bool -rtems_rtl_symbol_global_add (rtems_rtl_obj* obj, - const unsigned char* esyms, - unsigned int size) +rtems_rtl_symbol_global_add (rtems_rtl_obj* obj, + const unsigned char* esyms, + unsigned int size, + rtems_rtl_tls_offset* tls_offsets, + unsigned int tls_size) { rtems_rtl_symbols* symbols; rtems_rtl_obj_sym* sym; @@ -159,6 +177,9 @@ rtems_rtl_symbol_global_add (rtems_rtl_obj* obj, symbols = rtems_rtl_global_symbols (); + obj->global_syms = count; + + count = 0; s = 0; sym = obj->global_table; @@ -171,24 +192,29 @@ rtems_rtl_symbol_global_add (rtems_rtl_obj* obj, */ union { uint8_t data[sizeof (void*)]; - void* value; + void* voidp; } copy_voidp; + rtems_rtl_tls_offset* tls_off; int b; sym->name = (const char*) &esyms[s]; s += strlen (sym->name) + 1; for (b = 0; b < sizeof (void*); ++b, ++s) copy_voidp.data[b] = esyms[s]; - sym->value = copy_voidp.value; + tls_off = rtems_rtl_symbol_find_tls_offset (count, tls_offsets, tls_size); + if (tls_off == NULL) { + sym->value = copy_voidp.voidp; + } else { + sym->value = (void*) tls_off->offset(); + } if (rtems_rtl_trace (RTEMS_RTL_TRACE_GLOBAL_SYM)) printf ("rtl: esyms: %s -> %8p\n", sym->name, sym->value); if (rtems_rtl_symbol_global_find (sym->name) == NULL) rtems_rtl_symbol_global_insert (symbols, sym); + ++count; ++sym; } - obj->global_syms = count; - return true; } diff --git a/cpukit/libdl/rtl-tls.c b/cpukit/libdl/rtl-tls.c new file mode 100644 index 0000000000..7eb12831eb --- /dev/null +++ b/cpukit/libdl/rtl-tls.c @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +/** + * @file + * + * @ingroup rtems_rtld + * + * @brief RTEMS Run-Time Link Editor Thread Local Storage + * + * TLS support the RTL. + */ + +/* + * COPYRIGHT (c) 2023 Chris Johns <chrisj@rtems.org> + * + * Copyright (C) 2023 embedded brains GmbH & Co. KG + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "rtl-tls.h" + +#include <rtems/score/cpuimpl.h> +#include <rtems/score/percpu.h> +#include <rtems/score/thread.h> + +void* rtems_rtl_tls_get_base (void) +{ + return _CPU_Get_TLS_thread_pointer (&_Thread_Get_executing()->Registers); +} diff --git a/cpukit/libdl/rtl-tls.h b/cpukit/libdl/rtl-tls.h new file mode 100644 index 0000000000..78dc738dd4 --- /dev/null +++ b/cpukit/libdl/rtl-tls.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +/** + * @file + * + * @ingroup rtems_rtld + * + * @brief RTEMS Run-Time Link Editor Thread Local Storage + * + * TLS support the RTL. + */ + +/* + * COPYRIGHT (c) 2023 Chris Johns <chrisj@rtems.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#if !defined (_RTEMS_RTL_TLS_H_) +#define _RTEMS_RTL_TLS_H_ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +void* rtems_rtl_tls_get_base (void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/cpukit/libdl/rtl-unwind-arm.c b/cpukit/libdl/rtl-unwind-arm.c new file mode 100644 index 0000000000..35361fe8d5 --- /dev/null +++ b/cpukit/libdl/rtl-unwind-arm.c @@ -0,0 +1,65 @@ +#include "rtl-unwind-arm.h" + +bool +rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj, + const char* name, + uint32_t flags) +{ + /* + * We location the EH sections in section flags. + */ + return false; +} + +bool +rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj) +{ + return true; +} + +bool +rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj) +{ + obj->loader = NULL; + return true; +} + +/* + * A weak reference is in libgcc, provide a real version and provide a way to + * manage loaded modules. + * + * Passed in the return address and a reference to the number of records + * found. We set the start of the exidx data and the number of records. + */ +_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, + int* nrec) __attribute__ ((__noinline__, + __used__, + __noclone__)); + +_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, + int* nrec) +{ + rtems_rtl_data* rtl; + rtems_chain_node* node; + __EIT_entry* exidx_start = &__exidx_start; + __EIT_entry* exidx_end = &__exidx_end; + + rtl = rtems_rtl_lock (); + + node = rtems_chain_first (&rtl->objects); + while (!rtems_chain_is_tail (&rtl->objects, node)) { + rtems_rtl_obj* obj = (rtems_rtl_obj*) node; + if (rtems_rtl_obj_text_inside (obj, (void*) return_address)) { + exidx_start = (__EIT_entry*) obj->eh_base; + exidx_end = (__EIT_entry*) (obj->eh_base + obj->eh_size); + break; + } + node = rtems_chain_next (node); + } + + rtems_rtl_unlock (); + + *nrec = exidx_end - exidx_start; + + return (_Unwind_Ptr) exidx_start; +} diff --git a/cpukit/libdl/rtl-unwind-arm.h b/cpukit/libdl/rtl-unwind-arm.h new file mode 100644 index 0000000000..08a2660560 --- /dev/null +++ b/cpukit/libdl/rtl-unwind-arm.h @@ -0,0 +1,46 @@ +#include <unwind.h> + +#include <rtems/rtl/rtl.h> +#include "rtl-unwind.h" + +typedef unsigned _Unwind_Word __attribute__((__mode__(__word__))); +typedef _Unwind_Word _uw; + +bool +rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj, + const char* name, + uint32_t flags); + +bool +rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj); + +bool +rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj); + +/* An exception index table entry. */ +typedef struct __EIT_entry +{ + _uw fnoffset; + _uw content; +} __EIT_entry; + +/* The exception index table location in the base module */ +extern __EIT_entry __exidx_start; +extern __EIT_entry __exidx_end; + +/* + * A weak reference is in libgcc, provide a real version and provide a way to + * manage loaded modules. + * + * Passed in the return address and a reference to the number of records + * found. We set the start of the exidx data and the number of records. + */ +_Unwind_Ptr +__gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, + int* nrec) __attribute__ ((__noinline__, + __used__, + __noclone__)); + +_Unwind_Ptr +__gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, + int* nrec); diff --git a/cpukit/libdl/rtl.c b/cpukit/libdl/rtl.c index 21ddb00aac..8250af24c9 100644 --- a/cpukit/libdl/rtl.c +++ b/cpukit/libdl/rtl.c @@ -857,8 +857,10 @@ rtems_rtl_path_prepend (const char* path) } void -rtems_rtl_base_sym_global_add (const unsigned char* esyms, - unsigned int size) +rtems_rtl_base_sym_global_add (const unsigned char* esyms, + unsigned int size, + rtems_rtl_tls_offset* tls_offsets, + unsigned int tls_size) { if (rtems_rtl_trace (RTEMS_RTL_TRACE_GLOBAL_SYM)) printf ("rtl: adding global symbols, table size %u\n", size); @@ -869,7 +871,7 @@ rtems_rtl_base_sym_global_add (const unsigned char* esyms, return; } - rtems_rtl_symbol_global_add (rtl->base, esyms, size); + rtems_rtl_symbol_global_add (rtl->base, esyms, size, tls_offsets, tls_size); rtems_rtl_unlock (); } |