apple / swift-embedded-examples

A collection of example projects using Embedded Swift
Apache License 2.0
556 stars 30 forks source link

`pico-blink` Build Error #5

Open star-light-1 opened 2 months ago

star-light-1 commented 2 months ago

When trying to build the pico-blink example on macOS with:

cd swift-embedded-examples/pico-blink
rm -rf .build ; TOOLCHAINS=org.swift.59202404131a ./build.sh

I get the following error:

# Link
$CLANG .build/release/Support.build/{Support.c,crt0.S}.o .build/release/Blinky.build/*.o -target armv6m-apple-none-macho -o $BUILDROOT/blinky $LD_FLAGS
+ /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-04-13-a.xctoolchain/usr/bin/clang .build/release/Support.build/Support.c.o .build/release/Support.build/crt0.S.o .build/release/Blinky.build/Blinky.swift.o -target armv6m-apple-none-macho -o /path/to/swift-embedded-examples/pico-blink/.build/armv6m-apple-none-macho/release/blinky -static -Wl,-e,_reset -dead_strip -Wl,-no_zero_fill_sections -Wl,-segalign,4 -Wl,-segaddr,__RESET,0x20000000 -Wl,-segaddr,__VECTORS,0x20000100 -Wl,-seg1addr,0x20000200 -Wl,-pagezero_size,0 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/lib/darwin/macho_embedded/libclang_rt.soft_static.a
ld: warning: ignoring file /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-04-13-a.xctoolchain/usr/lib/clang/17/lib/darwin/macho_embedded/libclang_rt.soft_static.a, building for free standing-armv6m but attempting to link with file built for macOS-armv7
ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/lib/darwin/macho_embedded/libclang_rt.soft_static.a, building for free standing-armv6m but attempting to link with file built for macOS-armv7
Undefined symbols for architecture armv6m:
  "___muldi3", referenced from:
      _$s6Blinky4MainV5delayyySiFZTf4nd_n in Blinky.swift.o
ld: symbol(s) not found for architecture armv6m
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Is the ___muldi3 symbol not available on armv6m? If not, why is it being used?

System information:

swift-DEVELOPMENT-SNAPSHOT-2024-04-13-a.xctoolchain
Apple Swift version 6.0-dev (LLVM 01231f56c62f4c4, Swift abaa63821080b92)
Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:25 PDT 2024; root:xnu-10063.101.17~1

Toolchain ID:

$ plutil -extract CFBundleIdentifier raw -o - /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-*.xctoolchain/Info.plist                                    
org.swift.59202404131a

Git commit of swift-embedded-examples:

69dc23e718c5b20797daec17d4754cd42bb21c24

Full build log: pico-blink-build-log.txt

Joannis commented 2 months ago

@star-light-1 ___muldi3 multiplies two integers. The 100 * N is where multiplication is used in func delay(_ N: Int)

Joannis commented 2 months ago

Try using the toolchain from April 3rd, that worked for me.

star-light-1 commented 2 months ago

The oldest toolchain I could find in section "Trunk Development (main)" on swift.org/download is April 4, 2024, which for me produces the same error shown above. Latest toolchain of the 5.10 branch produces:

error: unable to load standard library for target 'armv6m-apple-none-macho'
kubamracek commented 2 months ago

I can sort-of reproduce the problem. One issue is that this part of the build script for pico-blink should not be necessary anymore, as the expectation is that the downloadable swift.org toolchain includes a valid libclang_rt.soft_static.a for the CPU arch we're building here (armv6m):

XCODE_RESOURCE_DIR=`env TOOLCHAINS="" xcrun clang -print-resource-dir`
LD_FLAGS+=" $XCODE_RESOURCE_DIR/lib/darwin/macho_embedded/libclang_rt.soft_static.a"

Once I drop this, I get:

# Link
$CLANG .build/release/Support.build/{Support.c,crt0.S}.o .build/release/Blinky.build/*.o -target armv6m-apple-none-macho -o $BUILDROOT/blinky $LD_FLAGS
+ /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-04-25-a.xctoolchain/usr/bin/clang .build/release/Support.build/Support.c.o .build/release/Support.build/crt0.S.o .build/release/Blinky.build/Blinky.swift.o -target armv6m-apple-none-macho -o /Users/kuba/Documents/swift-embedded-examples/pico-blink/.build/armv6m-apple-none-macho/release/blinky -static -Wl,-e,_reset -dead_strip -Wl,-no_zero_fill_sections -Wl,-segalign,4 -Wl,-segaddr,__RESET,0x20000000 -Wl,-segaddr,__VECTORS,0x20000100 -Wl,-seg1addr,0x20000200 -Wl,-pagezero_size,0
ld: warning: ignoring file /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-04-25-a.xctoolchain/usr/lib/clang/17/lib/darwin/macho_embedded/libclang_rt.soft_static.a, building for free standing-armv6m but attempting to link with file built for macOS-armv7
Undefined symbols for architecture armv6m:
  "___muldi3", referenced from:
      _$s6Blinky4MainV5delayyySiFZTf4nd_n in Blinky.swift.o
ld: symbol(s) not found for architecture armv6m

The problem is that libclang_rt.soft_static.a is somehow only armv7 and contains no other slices. That will need to be fixed, but I'm not sure how that happens in the first place.

The only workaround I can think of (short of actually removing the multiplication from source code, but sometimes those are really useful 😄) would be to rebuild libclang_rt from LLVM's compiler-rt from source and explicitly build the armv6m slice, and link that instead.

kubamracek commented 2 months ago

@star-light-1 can I also ask which Xcode version are you using?

star-light-1 commented 2 months ago

This happend using Xcode 15.3 (15E204a)

Bauer312 commented 1 month ago

The most recent 5.10 (May 1, 2024) and 6.0 (April 30, 2024) snapshot both still produce the linker warning ld: warning: ignoring file /<SNIP>/macho_embedded/libclang_rt.soft_static.a, building for free standing-armv6m but attempting to link with file built for macOS-armv7

5.10 fails with the undefined symbol, while 6.0 continues and produces the expected blinky.uf2 output, although not in the location that the README says it will appear. I have a pico w and it appears to reject loading it, but I need to do more testing to see if this is unrelated.

TG-Techie commented 2 weeks ago

Info

This issue is still present, I’m experiencing this on the June-13-2024 (org.swift.59202406131a) toolchain build. ( Xcode 15.2 Build version 15C500b )

I'm working on either a hanky-patch and/or a building libclang_rt.soft_static.a for armv6m (will ofc follow up with any success)

Question

Is there a suggested path or repo or chat-platform to engage contributing to or finding a longer term solution for a bug like this in the toolchain?

(Also hi all 👋)

TG-Techie commented 2 weeks ago

I have a (admittedly poorly maintainable) patch that works for adding armv6m ___muldi3

https://gist.github.com/TG-Techie/5be0cff9457bce50752c69eced81864f

Or....

removing the multiplication from source code, but sometimes those are really useful 😄

TG-Techie commented 2 weeks ago

update / re: @kubamracek 's suggestion of re-buliding a slice for armv6m,

Here is a version of the clang_rt I build from the main trunk of the llvm-project (753498e should be). libclang_rt.soft_static_armv6m_macho_embedded.a.zip

Usage

I copied the .a into the the Sources/Support directory and then added it as the first argument on this line https://github.com/apple/swift-embedded-examples/blob/2b93644fc4cca46c48e8da3023746c8d9e26007e/pico-blink/build.sh#L38

It worked well for the blink test so far 💁‍♂️ (provided you're comfortable downloading it from some nerd's GitHub comment, of course)

Happy Hacking, ~ TG-Techie

niqt commented 1 week ago

I added what @TG-Techie said, but now i have this error:

# Link
$CLANG Sources/Support/libclang_rt.soft_static_armv6m_macho_embedded.a .build/release/Support.build/{Support.c,crt0.S}.o .build/release/Blinky.build/*.o -target armv6m-apple-none-macho -o $BUILDROOT/blinky $LD_FLAGS
+ /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-06-13-a.xctoolchain/usr/bin/clang Sources/Support/libclang_rt.soft_static_armv6m_macho_embedded.a .build/release/Support.build/Support.c.o .build/release/Support.build/crt0.S.o .build/release/Blinky.build/Blinky.swift.o -target armv6m-apple-none-macho -o /Users/nicola/swift-embedded-examples/pico-blink/.build/armv6m-apple-none-macho/release/blinky -static -Wl,-e,_reset -dead_strip -Wl,-no_zero_fill_sections -Wl,-segalign,4 -Wl,-segaddr,__RESET,0x20000000 -Wl,-segaddr,__VECTORS,0x20000100 -Wl,-seg1addr,0x20000200 -Wl,-pagezero_size,0 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/lib/darwin/macho_embedded/libclang_rt.soft_static.a
ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/lib/darwin/macho_embedded/libclang_rt.soft_static.a, building for free standing-armv6m but attempting to link with file built for macOS-armv7
ld: warning: ignoring file /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-06-13-a.xctoolchain/usr/lib/clang/17/lib/darwin/macho_embedded/libclang_rt.soft_static.a, building for free standing-armv6m but attempting to link with file built for macOS-armv7

# Extract sections from executable into flashable binary
$PYTHON_EXEC $MACHO2UF2 $BUILDROOT/blinky $BUILDROOT/blinky.uf2 --base-address 0x20000000 --segments '__TEXT,__DATA,__VECTORS,__RESET'
+ /Applications/Xcode.app/Contents/Developer/usr/bin/python3 /Users/nicola/swift-embedded-examples/Tools/macho2uf2.py /Users/nicola/swift-embedded-examples/pico-blink/.build/armv6m-apple-none-macho/release/blinky /Users/nicola/swift-embedded-examples/pico-blink/.build/armv6m-apple-none-macho/release/blinky.uf2 --base-address 0x20000000 --segments __TEXT,__DATA,__VECTORS,__RESET
Traceback (most recent call last):
  File "/Users/nicola/swift-embedded-examples/Tools/macho2bin.py", line 32, in <module>
    from macholib import MachO
ModuleNotFoundError: No module named 'macholib'
Traceback (most recent call last):
  File "/Users/nicola/swift-embedded-examples/Tools/macho2uf2.py", line 89, in <module>
    main()
  File "/Users/nicola/swift-embedded-examples/Tools/macho2uf2.py", line 47, in main
    subprocess.check_call([MY_DIR + "/macho2bin.py", args.input, args.input + ".bin", "--base-address", "0x%08x" % args.base_address, "--segments", ",".join(args.segments)])
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 373, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/Users/nicola/swift-embedded-examples/Tools/macho2bin.py', '/Users/nicola/swift-embedded-examples/pico-blink/.build/armv6m-apple-none-macho/release/blinky', '/Users/nicola/swift-embedded-examples/pico-blink/.build/armv6m-apple-none-macho/release/blinky.bin', '--base-address', '0x20000000', '--segments', '__TEXT,__DATA,__VECTORS,__RESET']' returned non-zero exit status 1.
TG-Techie commented 1 week ago

@niqt , three follow ups: a) What version of the toolchain are you running? b) Would you mind adding ``` ticks to your above post for readability, please? c) it looks like you may need to install the python requirements.txt in the repo's tool folder based on these lines in the above text:

 from macholib import MachO
 ModuleNotFoundError: No module named 'macholib'
niqt commented 1 week ago
from macholib import MachO
ModuleNotFoundError: No module named 'macholib'

Yeah, it was missed python dependencies...