Issue created by Anthony Cole: 
https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5827



**Summary**

`rtems_rtl_load()` synchronizes the cache of each object file on the pending 
queue, and runs its constructors (`rtems_rtl_obj_synchronize_cache()`, 
`cpukit/libdl/rtl.c`). But `rtems_rtl_load_object()` takes an object file that 
still has unresolved externals off the pending queue and puts it on the objects 
list, until all its externals are resolved:

```c
    if (obj->unresolved != 0) {
      rtems_chain_extract(&obj->link);
      rtems_chain_append(&rtl->objects, &obj->link);
    }
```

`dlopen()` returns such an object file and its code can be called (the `dl12` 
test does), but its cache is never synchronized: its relocated code may not be 
what the processor fetches. Later relocations that resolve some, but not all, 
of its externals (`rtems_rtl_unresolved_resolve_reloc()`, 
`cpukit/libdl/rtl-unresolved.c`) aren't synchronized either; only the one that 
resolves the last puts it back on the pending queue.

**How it shows**

On a target whose instruction fetch doesn't see stores still in the data cache, 
with instruction cache support that executes FENCE.I (a Codasip V730 on an 
FPGA, a BSP derived from `riscv/riscv`): `dl12`'s object file has one 
unresolved external (`__cxa_finalize`), and the test hung calling `dl_o1_func`, 
at its call to `rtems_printf`: the processor ran the unrelocated `auipc ra, 0; 
jalr ra`, which jumps to itself. Under QEMU and GDB, a watchpoint on that 
instruction showed the relocation being written and 
`rtems_cache_instruction_sync_after_code_change()` never being called before 
`dl_o1_func`.

Not visible on targets without caches (QEMU), or whose instruction fetch sees 
the data cache.

**Fix**

Synchronize the object file's cache when it is set aside with unresolved 
externals, and after each relocation that leaves it with unresolved externals:

```diff
     if (obj->unresolved != 0) {
       rtems_chain_extract(&obj->link);
       rtems_chain_append(&rtl->objects, &obj->link);
+      rtems_rtl_obj_synchronize_cache(obj);
     }
```

```diff
         if (rec->rec.reloc.obj->unresolved == 0) {
           pending = rtems_rtl_pending_unprotected();
           rtems_chain_extract(&rec->rec.reloc.obj->link);
           rtems_chain_append(pending, &rec->rec.reloc.obj->link);
+        } else {
+          rtems_rtl_obj_synchronize_cache(rec->rec.reloc.obj);
         }
```

Its constructors still run only once it is fully resolved, as before. With 
this, `dl12` passes on the FPGA, and the libdl tests still pass under QEMU.

**Environment**

RTEMS `main` as of 53d56d9494 (2026-11-13), `cpukit/libdl`. Found on a Codasip 
V730 (RV32, FPGA), with the instruction cache support of #5826.

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5827
You're receiving this email because of your account on gitlab.rtems.org. 
Unsubscribe from this thread: 
https://gitlab.rtems.org/-/namespace/49/sent_notifications/6-dqb9ns4s07n65gyz17rt93zyg-1d/unsubscribe
 | Manage all notifications: https://gitlab.rtems.org/-/profile/notifications | 
Help: https://gitlab.rtems.org/help


_______________________________________________
bugs mailing list
[email protected]
http://lists.rtems.org/mailman/listinfo/bugs

Reply via email to