Skip to content

Conversation

@lyakh
Copy link
Contributor

@lyakh lyakh commented May 8, 2024

two fixes for relocatable LLEXT objects. This should fix errors in #72383

teburd
teburd previously approved these changes May 8, 2024
Copy link
Contributor

@pillo79 pillo79 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the debug, LGTM, just one small refactor request.

Comment on lines 612 to 619
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is duplicated, can you please refactor so the calculation and log is done once outside the if?
Also, note the commit that introduces it has a spelling issue in the commit title.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pillo79 @teburd they aren't duplicated. Both the sym_tab->syms[j].addr assignment and the log entry are different. I can unify the logging call by dropping the difference, but as of now they're different.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference in the address calculation (arguably the most important line in the whole function!) is just the name of one argument; in the log, there is a section name that could be calculated for the other case.
I really see no point in having 2 code paths that do the same logical thing in different ways.

@lyakh lyakh force-pushed the reloc branch 2 times, most recently from b34cc88 to 837b504 Compare May 10, 2024 12:34
@lyakh lyakh requested review from pillo79 and teburd May 10, 2024 13:40
Copy link
Contributor

@teburd teburd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor typo in commit s/stanrads/standard

changes look good to me

@teburd
Copy link
Contributor

teburd commented May 10, 2024

Sorry I maybe wasn't clear enough where I saw the typo, the commit message for 2235022 has
"llext: fix handling of non-stanrad sections in relocatable case"

github doesn't necessarily let me comment on a commit message, boooo

@lyakh
Copy link
Contributor Author

lyakh commented May 13, 2024

Sorry I maybe wasn't clear enough where I saw the typo, the commit message for 2235022 has "llext: fix handling of non-stanrad sections in relocatable case"

github doesn't necessarily let me comment on a commit message, boooo

@teburd arrrgh, you were perfectly clear, it's just that I cannot type

teburd
teburd previously approved these changes May 13, 2024
Copy link
Contributor

@teburd teburd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nits still, but the change looks ok to me otherwise

Copy link
Contributor

@pillo79 pillo79 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the idea of being able to respect the ELF-provided addresses, but IMO we really need a test case for this new feature in Zephyr. I don't want to have to guess if any of the patches I submit are breaking SOF... (or even worse, hear screaming coming that way 😅)

Comment on lines +612 to +613
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be:

Suggested change
sym_tab->syms[j].addr = (uint8_t *)sym.st_value +
(ldr->hdr.e_type == ET_REL ? section_addr : 0);
sym_tab->syms[j].addr = (uint8_t *)section_addr
+ sym.st_value;

because in the "pre-located" case the symbols'VMA should not be modified, so the calculation is the same regardless of dynamic or static linking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pillo79 no, the difference between ET_REL (relocatable object) and ET_DYN (shared object) remains. In the relocatable case that calculation is correct:

[  113.934401] <inf> llext: llext_copy_symbols: type: 1 sym volume_init: 0x1e8c section 0xa068a000
[  113.934410] <inf> llext: llext_copy_symbols: type: 1 sym volume_func_count: 0x24 section 0xa068e000
[  113.934420] <inf> llext: llext_copy_symbols: type: 1 sym volume_reset_state: 0xf50 section 0xa068a000

but in the shared case it's this:

[  150.092295] <inf> llext: llext_copy_symbols: type: 3 sym volume_init: 0xa068ba5c section 0xa068a000
[  150.092306] <inf> llext: llext_copy_symbols: type: 3 sym volume_func_count: 0xa068d0b0 section 0xa068d000
[  150.092318] <inf> llext: llext_copy_symbols: type: 3 sym volume_reset_state: 0xa068b510 section 0xa068a000

Copy link
Contributor

@pillo79 pillo79 May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that is indeed why it's subtracted in the else case!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like "Use the virtual symbol addresses from the ELF instead of the run-time ones" might be clearer here.

@lyakh
Copy link
Contributor Author

lyakh commented May 29, 2024

I love the idea of being able to respect the ELF-provided addresses, but IMO we really need a test case for this new feature in Zephyr.

@pillo79 testing is good... We could add a test where an LLEXT is built for a fixed address, but for which one? You'd probably need to modify a linker script to assign a fixed address range for that extension? And that would be testing / twister-specific, whereas now standard scripts, e.g. soc/cdns/dc233c/include/xtensa-dc233c.ld are used... Any ideas?

@pillo79
Copy link
Contributor

pillo79 commented May 29, 2024

We could add a test where an LLEXT is built for a fixed address, but for which one?

You could use the SOF strategy and add a custom re-link step that only fixes an integer variable to 0xdeadbeef, then just check its final address from the test. (EDIT: If partial placement is not feasible, limit the test to the qemu-xtensa arch only). WDYT?

@lyakh
Copy link
Contributor Author

lyakh commented May 29, 2024

We could add a test where an LLEXT is built for a fixed address, but for which one?

You could use the SOF strategy and add a custom re-link step that only fixes an integer variable to 0xdeadbeef, then just check its final address from the test. (EDIT: If partial placement is not feasible, limit the test to the qemu-xtensa arch only). WDYT?

@pillo79 not sure I understand. We need to build an LLEXT with some predefined address, similar to how we do for SOF by using gcc -Wl,-Tdata=0x........ right? And then QEMU must be able to access that address. So you could do something like:

  1. in test_llext_simple.c allocate space for .data from the llext object like uint8_t obj_data[4096];
  2. build everything
  3. check zephyr.elf and find the address of obj_data
  4. re-link LLEXT with -Wl,-Tdata=...
  5. load that LLEXT, copy .data to that array, maybe delete the original .data to make sure the correct one is used...

So, not quite trivial unless there's a simpler way

lyakh added 3 commits May 29, 2024 12:25
When building partially linked / relocatable objects no ELF segments
are created and it becomes more difficult to predict which sections
the compiler will build and use. In this case a .data.rel.local
section is created by the compiler and it is needed to link .rodata
strings in a twister test. We can handle arbitrary sections at run-
time if .peek() is supported. If it isn't we need to allocate and
copy the section. For now we simply error out in such cases. Fixing
that would represent a larger change and can be done incrementally.

This also fixes the relocation calculation to point to the correct
symbol address instead of the memory location, where it's currently
residing, because that can be a temporary buffer as is the case with
SOF.

Signed-off-by: Guennadi Liakhovetski <[email protected]>
Use an existing variable instead of re-calculating and fix swapped
space and a paranthesis.

Signed-off-by: Guennadi Liakhovetski <[email protected]>
Zero offset in a relocation entry is valid, shouldn't ignore it.

Signed-off-by: Guennadi Liakhovetski <[email protected]>
Copy link
Contributor

@pillo79 pillo79 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, will merge the test from #73479 after this.

@MaureenHelm MaureenHelm merged commit 518a712 into zephyrproject-rtos:main May 30, 2024
@lyakh lyakh deleted the reloc branch May 30, 2024 15:07
@henrikbrixandersen henrikbrixandersen added the area: llext Linkable Loadable Extensions label Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: llext Linkable Loadable Extensions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants