-
Couldn't load subscription status.
- Fork 8.1k
llext: load: memcpy section header #97993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
When loading from `.elf` files, it is not guaranteed that section headers are word aligned with the `.elf` file. Attempting to perform a direct assignment results in the compiler assuming the input pointer is aligned, resulting in usage faults if the assumption is broken. Signed-off-by: Jordan Yates <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
|
| if (shdr->sh_type == SHT_SYMTAB && ldr->hdr.e_type == ET_REL) { | ||
| LOG_DBG("symtab at %d", i); | ||
| ldr->sects[LLEXT_MEM_SYMTAB] = *shdr; | ||
| memcpy(&ldr->sects[LLEXT_MEM_SYMTAB], shdr, sizeof(*shdr)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, interesting. Is this compiler-specific? IIRC I've seen a structure assignment being replaced by the compiler with a memcpy builtin. That implementation shouldn't assume any alignment. And in general - as long as there's no alignment attribute, is it valid to assume any such alignment? I think structures are defined to be aligned in arrays, but stand-alone - I wasn't aware of such restrictions at least
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've ran into similar issues with mipi sys-t logging in the past which, yes, when derefencing arbitrary pointers and applying assignment would result in unaligned access faults just like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@teburd but that depends on the type of the pointer. If it's a char * certainly no alignment is needed. For a 2-byte type a two-byte alignment is needed and so on. And for a structure? Even the fact that the linker placed a section header above without a 4-byte alignment suggests, that it's valid. And then the compiler fails to generate code to read it. Interesting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even the fact that the linker placed a section header above without a 4-byte alignment suggests, that it's valid
Its not clear to me why the section header should have any alignment requirements, its a binary data structure, not executable code. Even if it did have an alignment, what should that be? A uint32_t alignment, that would work fine for the 32 bit targets, would still have an incorrect alignment on 64 bit platforms.



When loading from
.elffiles, it is not guaranteed that section headers are word aligned within the.elffile. Attempting to perform a direct assignment results in the compiler assuming the input pointer is aligned, resulting in usage faults if the assumption is broken.Example logs from my first attempt loading an
.elf