Swap endian-ness in tools on big endian hosts #329
No reviewers
Labels
No labels
bazel
bluetooth
board settings
boot_stage2
bug
build
c++
can't reproduce
clang
clib-interface
cmsis
code cleanliness
code size
compiler version issues
cyw43_driver
debug
discussion
documentation
duplicate
dynamic_clock
elf2uf2
enhancement
errata
filesystem
flash layout
floating point
FreeRTOS
hardware_adc
hardware_claim
hardware_clocks
hardware_dma
hardware_exception
hardware_flash
hardware_gpio
hardware_i2c
hardware_irq
hardware_pio
hardware_pwm
hardware_rcp
hardware_rtc
hardware_spi
hardware_sync
hardware_uart
hardware_vreg
hardware_watchdog
hardware_xosc
help wanted
host
IAR
installation
invalid
linker scripts
lto
lwip
mbedtls
missing API
needs reporter detail
networking
newlib
newlib-multicore
non-c
non-cmake
non-gcc
openocd
orphan PR
OS integration
pico_async_context
pico_base
pico_binary_info
pico_flash
picolibc
pico_lwip
pico_malloc
pico_multicore
pico_printf
pico_runtime
pico_stdio
pico_stdio_usb
pico_sync
pico_time
pico_util
pioasm
port
question
review
review2
rtos interop
runtime init
static library
svd
test
tinyusb
vscode
wifi integration
wontfix
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-raspberrypi/pico-sdk!329
Loading…
Reference in a new issue
No description provided.
Delete branch "zeldin/endianness"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
When the host running the SDK is not little endian, all data that is exchanged with the Pico must be converted to/from little endian byte order.
Instead of sprinking explicit swaps around the code, I used available mechanisms for doing the swaps implicitly. Unfortunately, there is no such mechanism that works with both C and C++, so because the code base mixes C and C++ double mechanisms need to be employed...
The C++ fix also suffers from the fact that some structures are declared as
__attribute__((packed)), which requires the contents to be POD. Because of this, thelittle_endiantemplate is not allowed to have a constructor. This is not a problem in the SDK itself, but inpicotoolit would have been convenient to be able to createstd::vector<little_endian<uint32_t>> programwith an initializer.@kilograham may feel differently (and his opinion matters much more than mine), but I think it might be simpler to keep everything on the Pico little-endian, and rely on the host-side tools (
elf2uf2,picotool, etc.) to do any necessary conversion if the host itself isn't little-endian?Otherwise you might have some Picos presenting a little-endian host interface, and other Picos presenting a big-endian host interface, depending only on what firmware has been flashed to them, and that might get a bit confusing?
(Of course, that's assuming that I'm correctly understanding what this PR is trying to do, which I might not be? 🤷 )
@lurch I think there may be a misunderstanding here. What the code does it make the data being accessed as little endian, regardless of the systems actual endianness. On a system which is already little endian, such as the Pico itself, this is a no-operation (and in fact the macros will expand to nothing). There is nothing here that will make anything big endian on the Pico. All conversions happen on the host side (and only if the host is big endian).
Cool, please accept my apologies for my misunderstanding (I'm more of a Python guy than a low-level C guy). Is a similar patch needed for https://github.com/raspberrypi/picotool or does this PR already encompass that?
@lurch Yes, I have a patch for
picotooltoo. It depends on this patch though, so I wanted to test the waters here first before submitting it. 😄Just out of curiosity, what big-endian host machine / OS / environment are you using?
@lurch RCS Talos II (POWER9) with Gentoo Linux is my main host. I also have a big endian Raspberry Pi 3. 😸
I'd presumed you were on a Dec Alpha or a VAX or some such. Huh. I hadn't quite grokked that the Pi was Bi-endian.
@aallan All ARM-v8 cores from ARM are bi-endian. VAX is little-endian only, though, having been designed in the 1970:s, just like x86. 😆
Having used a VAX a lot during my undergrad I have total false memories of it being big endian at that point. Huh. Guess I was brain washed by my many years on Dec Alphas during my postgrad years. 🤷♂️
can you rebase this on develop
@kilograham Sure thing!
I'm a bit torn over this; scalar_storage_order isn't supported on clang (even gcc mode). I don't believe we have any non target device which touches this which isn't C++ anyway.
I'd be tempted perhaps to do (i will keep trying to think of a nice way to do this with typedefs - I guess we can guard with an #ifdef)
in the headers (or in a common one if we have one which works across elf2uf2, picotool, regular build etc)
And that way we defer everything to the user.
C code, can continue to have to reverse byte orders manually (or wrap the structure with another one which reverses the byte order using scalar_storage_order - if that's what they choose to do)
In our C++ we can define
le_uint32_tto bestored_little_endian<uint32_t>before including the headerAh, ok. In that case at the very least there should be a conditional
#errorto catch the case where you use clang on a big-endian host. But not using it at all might be preferable.Maybe I mistunderstand your point here, but C code compiled for the host using the relevant structs exist in the file
picoboot_connection.c(which is inpicotool, but the header filepicoboot.hthat it gets the structs from is inpico-sdk).Of course, changing this file to C++ could be an option.
Right, but wouldn't it make sense for
platform.hto make that define and provide the appropriate template? If you don't like e.g.uf2.hgetting an added#include "pico/platform.h", the C++ source code could be required to include it from the top level instead. Or I guess a new header file with a different name. It just doesn't seem like this wheel should have to be reinvented by each client. 😸@kilograham I reworked the patch to work along the lines you described. The SDK patch wasn't affected that much. The
picotoolpatch became a bit more messy whenpicoboot_connection.ccan't rely on__attribute__((scalar_storage_order))anymore, but it's not too bad. I'll open a PR for that repo to so you can take a look.a few thoughts; will look at the picotool patch too
@ -21,6 +21,27 @@ extern "C" {#define __packed __attribute__((packed))#endif#if !defined(le_uint16_t) || !defined(le_uint32_t) || !defined(le_int32_t)seems like this can go in platform.h, no?
@ -135,5 +135,38 @@ static inline void __compiler_memory_barrier(void) {}#ifdef __cplusplus}note we should only need this for big endian systems... either omit, or make it a no-op on little endian host
@ -9,7 +9,9 @@#include <vector>this could be in platform.h
@ -21,6 +21,27 @@ extern "C" {#define __packed __attribute__((packed))#endif#if !defined(le_uint16_t) || !defined(le_uint32_t) || !defined(le_int32_t)Yes, but the thing is that
structure.hdoes not includeplatform.h. Which is why it already has its own definition of__packedjust above.In the previous version of the patch, I did add an include of
platform.hhere, but then I noticed the comment at the start which says that this file is not supposed to use SDK includes...@ -9,7 +9,9 @@#include <vector>Ok, I guess if you want an alternate implementation you could make your
#defines before includingpico/platform.hinstead of after.@ -135,5 +135,38 @@ static inline void __compiler_memory_barrier(void) {}#ifdef __cplusplus}Sure. A modern compiler will in fact already turn this code into a no-op on little endian, but I can put an explicitly no-op version inside an
#ifdef.@ -135,5 +135,38 @@ static inline void __compiler_memory_barrier(void) {}#ifdef __cplusplus}(Here's what I mean about the compiler fixing this for you, btw: https://godbolt.org/z/3cTdTYGzY)
@ -21,6 +21,27 @@ extern "C" {#define __packed __attribute__((packed))#endif#if !defined(le_uint16_t) || !defined(le_uint32_t) || !defined(le_int32_t)ah yes; danger of reading in github - yes we should keep that as you have it
@ -135,5 +135,38 @@ static inline void __compiler_memory_barrier(void) {}#ifdef __cplusplus}Yeah, I figured, I didn't know they all did so so uniformly... still it is clearer, and some maniacs may use -O0.
Moved to 1.2.1 as I think i want to put the stuff in platform.h after all
We are now carrying this as a patch to the Debian package of pico-sdk so that it works on s390x:
https://salsa.debian.org/debian/pico-sdk/-/blob/master/debian/patches/329.patch?ref_type=heads
This closes raspberrypi/picotool#104
FYI, the pico-sdk autopkgtest failed on (big endian) s390x before but with the (rebased) patch from this pull request it passes now: https://ci.debian.net/packages/p/pico-sdk/testing/s390x/47384722/
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.