4-byte alignment for embedded files

SpenZerX
Posts: 16
Joined: Sun Dec 13, 2015 9:23 am

4-byte alignment for embedded files

Postby SpenZerX » Wed Nov 23, 2016 7:48 pm

Hello,


COMPONENT_EMBED_FILES := xyz.bin

Included files are stored to .rodata but no 4-byte alignment.

How can i change this?
Creator of Smart Connected Devices - for EcSUHA.de Project

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: 4-byte alignment for embedded files

Postby WiFive » Wed Nov 23, 2016 8:51 pm

Hmm that is interesting

Code: Select all

--rename-section .data=.rodata.embedded

Code: Select all

*(.rodata.*)
...
. = ALIGN(4);
Seems like it should be. Maybe this is related to people reporting certificate parsing errors.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: 4-byte alignment for embedded files

Postby ESP_Angus » Wed Nov 23, 2016 9:51 pm

Hi SpenZerX,

Unfortunately embedding files in this way doesn't guarantee alignment. The esp-idf build system uses objcopy which generates an object file with alignment == 0 - so the linker applies no alignment constraints when linking the symbol. (There's a stackoverflow thread that explains it pretty well here: http://stackoverflow.com/questions/8822 ... es#8858950 )

In your own project, here are a few ideas that may handle this:
  • Memcpy the data to an aligned buffer in RAM.
  • Call out to "xxd -i" (which installs as part of vim) to code generate a header file with the bytes of your file. You can apply "const __attribute__(((aligned(4)))" to the variable in the header in order to tell the compiler to align it to a 4 byte boundary (even though it's 'char' data) and store it in flash.
  • If you need to regenerate the file as part of the build process, so the embedded content changes automatically when the binary file changes, then you can use xxd from your component makefile. A very similar example is documented here: http://esp-idf.readthedocs.io/en/latest ... generation

    The code generated header won't have alignment and const constraints applied, but you can (very hackily) apply them at include-time like this:

    Code: Select all

    const __attribute__(((aligned(4)))
    #include <my_binary_file.h>
    
There are probably other approaches that I can't think of right now. :)
WiFive wrote: Seems like it should be. Maybe this is related to people reporting certificate parsing errors.
Unfortunately the ALIGN constraint in linker scripts only applies to the next symbol/marker in the script, not all symbols in a section.

(BTW, we reproduced the certificate parsing errors on OS X - embedding text files didn't work as expected there because of different "echo" behaviour. A fix is on its way.)

Who is online

Users browsing this forum: Bing [Bot] and 403 guests