tannewt / embedded

Python toolkit for building (aka compiling and linking) embedded projects
MIT License
4 stars 1 forks source link

linker doesn't like --print-memory-usage on raspberry pi5 #3

Open d-c-d opened 5 months ago

d-c-d commented 5 months ago

running build.py gets an error with respect to the -Wl,--print-memory-usage being passed to clang ( oh, build.py was part of StemmaG0-Template )

(my_python_3.12_venv) dcd@pi5:~/git/StemmaG0-Template $ python build.py
WARNING:embedded.build:ld.lld: error: unknown argument '--print-memory-usage'
clang: error: ld.lld command failed with exit code 1 (use -v to see invocation)
ERROR:embedded.build:.$ clang -mthumb --target=arm-none-eabi -mcpu=cortex-m0plus -Wl,--print-memory-usage -Wl,-Map=build/main.elf.map -Wl,--gc-sections -nostdlib build/main.o -T linker.ld -o build/main.elf

(my_python_3.12_venv) dcd@pi5:~/git/StemmaG0-Template $ which ld.lld
/usr/bin/ld.lld
```Traceback (most recent call last):
  File "/home/dcd/git/StemmaG0-Template/build.py", line 32, in <module>
    embedded.cli.run(build_stemmag0)
  File "/home/dcd/my_python_3.12_venv/lib/python3.12/site-packages/embedded/cli.py", line 97, in run
    asyncio.run(run_eager(function(*function_args)))
  File "/home/dcd/git/cpython/Lib/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/home/dcd/git/cpython/Lib/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dcd/git/cpython/Lib/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/dcd/my_python_3.12_venv/lib/python3.12/site-packages/embedded/cli.py", line 22, in run_eager
    await coro
  File "/home/dcd/git/StemmaG0-Template/build.py", line 29, in build_stemmag0
    await compiler.link(mcu.cpu, [build / "main.o"], build / "main.elf", "linker.ld", ["-nostdlib"])
  File "/home/dcd/my_python_3.12_venv/lib/python3.12/site-packages/embedded/compiler.py", line 50, in link
    await build.run_command([self.c_compiler, *cpu_flags, *link_flags, *flags, *objects, "-T", linker_script, "-o", output_file], description=f"Link {output_file.relative_to(cwd)}", working_directory=caller_directory)
  File "/home/dcd/my_python_3.12_venv/lib/python3.12/site-packages/embedded/build/__init__.py", line 84, in run_command
    raise RuntimeError()
RuntimeError

including the rest of the traceback - though I don't think it applies

d-c-d commented 5 months ago

if I add -fuse-ld=bfd I don't get the link error, but the elf file text, data, or bss e.g:

 size build/main.elf
   text    data     bss     dec     hex filename
      0       0       0       0       0 build/main.elf
         cpu_flags = cpu.get_arch_cflags(self)
         link_flags = []
         if print_memory_use:
+            link_flags.append("-fuse-ld=bfd") # if using clang/LLVM
             link_flags.append("-Wl,--print-memory-usage")
         if output_map_file:
             link_flags.append("-Wl,-Map=" + str(output_file.with_suffix(".elf.map").relative_to(caller_directory)))
d-c-d commented 5 months ago

FYI: adding ENTRY(main) and merging linker.ld and common.ld allowed the file to link

$ cat linker.ld <( echo "ENTRY(main)" ) common.ld > bar.ld
$ clang  -mthumb --target=arm-none-eabi -mcpu=cortex-m0plus -fuse-ld=bfd -Wl,--print-memory-usage -Wl,-Map=build/main.elf.map -Wl,--gc-sections -nostdlib build/main.o -T bar.ld  -o build/main.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:         364 B        32 KB      1.11%
             RAM:          52 B         8 KB      0.63%
(my_python_3.12_venv) dcd@pi5:~/git/StemmaG0-Template $ size build/main.elf
   text    data     bss     dec     hex filename
    312      52       0     364     16c build/main.elf
tannewt commented 5 months ago

Please make a PR with the changes. Thanks for trying this out!

d-c-d commented 5 months ago

see https://github.com/tannewt/embedded/pull/4 along with https://github.com/tannewt/StemmaG0-Template/pull/1