Skip to content

Commit b0dbbb7

Browse files
pillo79kartben
authored andcommitted
device: add CONFIG_LLEXT_EXPORT_DEV_IDS_BY_HASH option
This new option allows to export devices using identifiers generated from the hash of the devicetree node path, instead of the device's ordinal number. Identifiers generated this way are stable across rebuilds. Add new test cases to test this new option. Signed-off-by: Luca Burelli <[email protected]>
1 parent 1bb939a commit b0dbbb7

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

include/zephyr/device.h

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,35 @@ typedef int16_t device_handle_t;
101101
* The ordinal used in this name can be mapped to the path by
102102
* examining zephyr/include/generated/zephyr/devicetree_generated.h.
103103
*/
104-
#define Z_DEVICE_DT_DEV_ID(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))
104+
#define Z_DEVICE_DT_DEP_ORD(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))
105105

106-
#if defined(CONFIG_LLEXT_EXPORT_DEVICES)
106+
/* Same as above, but uses the hash of the node path instead of the ordinal.
107+
*
108+
* The hash used in this name can be mapped to the path by
109+
* examining zephyr/include/generated/zephyr/devicetree_generated.h.
110+
*/
111+
#define Z_DEVICE_DT_HASH(node_id) _CONCAT(dts_, DT_NODE_HASH(node_id))
112+
113+
/* By default, device identifiers are obtained using the dependency ordinal.
114+
* When LLEXT_EXPORT_DEV_IDS_BY_HASH is defined, the main Zephyr binary exports
115+
* DT identifiers via EXPORT_SYMBOL_NAMED as hashed versions of their paths.
116+
* When matching extensions are built, that is what they need to look for.
117+
*
118+
* The ordinal or hash used in this name can be mapped to the path by
119+
* examining zephyr/include/generated/zephyr/devicetree_generated.h.
120+
*/
121+
#if defined(LL_EXTENSION_BUILD) && defined(CONFIG_LLEXT_EXPORT_DEV_IDS_BY_HASH)
122+
#define Z_DEVICE_DT_DEV_ID(node_id) Z_DEVICE_DT_HASH(node_id)
123+
#else
124+
#define Z_DEVICE_DT_DEV_ID(node_id) Z_DEVICE_DT_DEP_ORD(node_id)
125+
#endif
126+
127+
#if defined(CONFIG_LLEXT_EXPORT_DEV_IDS_BY_HASH)
128+
/* Export device identifiers by hash */
129+
#define Z_DEVICE_EXPORT(node_id) \
130+
EXPORT_SYMBOL_NAMED(DEVICE_DT_NAME_GET(node_id), \
131+
DEVICE_NAME_GET(Z_DEVICE_DT_HASH(node_id)))
132+
#elif defined(CONFIG_LLEXT_EXPORT_DEVICES)
107133
/* Export device identifiers using the builtin name */
108134
#define Z_DEVICE_EXPORT(node_id) EXPORT_SYMBOL(DEVICE_DT_NAME_GET(node_id))
109135
#endif
@@ -175,8 +201,8 @@ typedef int16_t device_handle_t;
175201
*
176202
* This macro defines a @ref device that is automatically configured by the
177203
* kernel during system initialization. The global device object's name as a C
178-
* identifier is derived from the node's dependency ordinal. @ref device.name is
179-
* set to `DEVICE_DT_NAME(node_id)`.
204+
* identifier is derived from the node's dependency ordinal or hash.
205+
* @ref device.name is set to `DEVICE_DT_NAME(node_id)`.
180206
*
181207
* The device is declared with extern visibility, so a pointer to a global
182208
* device object can be obtained with `DEVICE_DT_GET(node_id)` from any source

subsys/llext/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ config LLEXT_EXPORT_DEVICES
8686
When enabled, all Zephyr devices defined in the device tree are
8787
made available to llexts via the standard DT_ / DEVICE_* macros.
8888

89+
config LLEXT_EXPORT_DEV_IDS_BY_HASH
90+
bool "Use hash of device path in device name"
91+
depends on LLEXT_EXPORT_DEVICES
92+
help
93+
When enabled, exported device names are generated from a hash of the
94+
node path instead of an ordinal number. Identifiers generated this
95+
way are stable across rebuilds.
96+
8997
config LLEXT_EXPORT_BUILTINS_BY_SLID
9098
bool "Export built-in symbols to llexts via SLIDs"
9199
help

tests/subsys/llext/testcase.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,21 @@ tests:
124124
- CONFIG_LLEXT_STORAGE_WRITABLE=y
125125
- CONFIG_LLEXT_TYPE_ELF_RELOCATABLE=y
126126
- CONFIG_LLEXT_EXPORT_BUILTINS_BY_SLID=y
127+
128+
# Test the export device IDs by hash feature on a single architecture in
129+
# both normal and SLID mode.
130+
llext.devices_by_hash:
131+
arch_allow: arm
132+
filter: not CONFIG_MPU and not CONFIG_MMU
133+
extra_conf_files: ['no_mem_protection.conf']
134+
extra_configs:
135+
- CONFIG_LLEXT_STORAGE_WRITABLE=n
136+
- CONFIG_LLEXT_EXPORT_DEV_IDS_BY_HASH=y
137+
llext.devices_by_hash_slid_linking:
138+
arch_allow: arm
139+
filter: not CONFIG_MPU and not CONFIG_MMU
140+
extra_conf_files: ['no_mem_protection.conf']
141+
extra_configs:
142+
- CONFIG_LLEXT_STORAGE_WRITABLE=n
143+
- CONFIG_LLEXT_EXPORT_DEV_IDS_BY_HASH=y
144+
- CONFIG_LLEXT_EXPORT_BUILTINS_BY_SLID=y

0 commit comments

Comments
 (0)