Skip to content
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

Allow to specify own path to linker script (ld) using "build_flags" option #233

Closed
ivankravets opened this issue Jun 10, 2015 · 11 comments
Closed
Assignees
Milestone

Comments

@ivankravets
Copy link
Member

Process -Wl,-T%PATH_TO_LD_SCRIPT% option from http://docs.platformio.org/en/latest/projectconf.html#build-flags

@me21
Copy link

me21 commented Jul 18, 2019

@me21
Copy link

me21 commented Jul 18, 2019

@ivankravets I try to pass this option to linker:

build_flags = -Wl,-T${platformio.src_dir}\flash.ld,-v

but all I get now is

Linking .pio\build\bluepill_f103c8_arduino\firmware.elf
.pio\build\bluepill_f103c8_arduino\FrameworkArduino\main.cpp.o: In function `main':
main.cpp:(.text.startup.main+0x2): warning: undefined reference to `setup()'
main.cpp:(.text.startup.main+0x6): warning: undefined reference to `loop()'

@ivankravets
Copy link
Member Author

build_flags = -Wl,-T${platformio.src_dir}\flash.ld,-

It should be build_flags = -Wl,-T$PROJECT_DIR/src/flash.ld,-v

http://docs.platformio.org/en/latest/projectconf/section_env_build.html#built-in-variables

@me21
Copy link

me21 commented Jul 18, 2019

@ivankravets Went a little further by using -Wl,-v,-T"C:\Temp\Platformio Projects\bluepill\src\flash.ld", but now -T option is included in linker command line twice, once from build_flags, once from the build script.

Linking .pio\build\bluepill_f103c8_arduino\firmware.elf
collect2 version 7.2.1 20170904 (release) [ARM/embedded-7-branch revision 255204]
c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe -plugin c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/liblto_plugin-0.dll -plugin-opt=c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\ED71~1\AppData\Local\Temp\ccYommBf.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc --sysroot=c:\.platformio\packages\toolchain-gccarmnoneeabi@1.70201.0\bin\../arm-none-eabi -X -o .pio\build\bluepill_f103c8_arduino\firmware.elf c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/thumb/v7-m/crti.o c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/thumb/v7-m/crtbegin.o c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/crt0.o -LC:\.platformio\platforms\ststm32\ldscripts -L.pio\build\bluepill_f103c8_arduino -LC:\.platformio\packages\framework-arduinoststm32-maple\STM32F1\variants\maple\ld -Lc:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/thumb/v7-m -Lc:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/lib/thumb/v7-m -Lc:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../arm-none-eabi/lib/thumb/v7-m -Lc:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1 -Lc:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc -Lc:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/lib -Lc:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../arm-none-eabi/lib -v -TC:\Temp\Platformio Projects\bluepill\src\flash.ld --check-sections --gc-sections --unresolved-symbols=report-all --warn-common --warn-section-align --warn-unresolved-symbols .pio\build\bluepill_f103c8_arduino\FrameworkArduinoVariant\board.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduinoVariant\wirish\boards.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduinoVariant\wirish\boards_setup.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduinoVariant\wirish\start.S.o .pio\build\bluepill_f103c8_arduino\FrameworkArduinoVariant\wirish\start_c.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduinoVariant\wirish\syscalls.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\HardwareSerial.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\HardwareTimer.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\IPAddress.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\Print.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\Stream.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\WString.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\avr\dtostrf.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\cxxabi-compat.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\ext_interrupts.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\hooks.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\itoa.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\adc.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\adc_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\bkp_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\dac.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\dma.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\dma_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\exc.S.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\exti.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\exti_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\flash.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\fsmc_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\gpio.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\gpio_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\i2c.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\i2c_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\iwdg.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\nvic.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\pwr.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\rcc.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\rcc_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\spi.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\spi_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\stm32f1\performance\isrs.S.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\stm32f1\performance\vector_table.S.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\systick.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\timer.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\timer_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usart.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usart_f1.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usart_private.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usb\stm32f1\usb.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usb\stm32f1\usb_cdcacm.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usb\stm32f1\usb_reg_map.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usb\usb_lib\usb_core.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usb\usb_lib\usb_init.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usb\usb_lib\usb_mem.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\usb\usb_lib\usb_regs.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\libmaple\util.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\main.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\new.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\pwm.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\sdio.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\stm32f1\util_hooks.c.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\stm32f1\wiring_pulse_f1.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\stm32f1\wirish_debug.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\stm32f1\wirish_digital_f1.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\tone.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\usb_serial.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\wirish_analog.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\wirish_digital.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\wirish_math.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\wirish_shift.cpp.o .pio\build\bluepill_f103c8_arduino\FrameworkArduino\wirish_time.cpp.o .pio\build\bluepill_f103c8_arduino\src\main.cpp.o --start-group -lgcc --end-group -lstdc++ -lm --start-group -lgcc -lc --end-group c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/thumb/v7-m/crtend.o c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/thumb/v7-m/crtn.o -T flash.ld
c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe:mem-flash.inc:3: warning: redeclaration of memory region `ram'
GNU ld (GNU Tools for Arm Embedded Processors 7-2017-q4-major) 2.29.51.20171128
c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe:mem-flash.inc:4: warning: redeclaration of memory region `rom'
c:/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe:flash.ld:20: error: redefinition of memory region alias `REGION_TEXT'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\bluepill_f103c8_arduino\firmware.elf] Error 1

@ivankravets
Copy link
Member Author

If you want to pass complex flags I recommend to do this directly with build environment using an extra script (PRE). See https://github.com/platformio/platformio-core/blob/develop/platformio/builder/tools/platformio.py#L127

@me21
Copy link

me21 commented Jul 18, 2019

So specifying own path to linker script in build_flags is not supported now?

I tried passing -Wl,-T$PROJECT_DIR/src/flash.ld,-v, but my project dir contains spaces in path and I get

Linking .pio\build\bluepill_f103c8_arduino\firmware.elf
arm-none-eabi-g++: error: Projects\bluepill/src/flash.ld,-v: No such file or directory

Whereas full path should be "C:\Temp\Platformio Projects\bluepill"

@me21
Copy link

me21 commented Jul 18, 2019

@ivankravets The combination of build_flags which works for me is

build_flags = -Wl,-T"C:\Temp\Platformio Projects\bluepill\src\flash.ld",-v

Since my project path contains spaces, your proposal build_flags = -Wl,-T$PROJECT_DIR/src/flash.ld,-v didn't work for me.

But there's another bug: linker script includes other files. Example: INCLUDE mem-flash.inc. I placed mem-flash.inc next to my custom linker file, but apparently mem-flash.inc from framework's variants/maple/ld/ folder is preferred. Possibly if custom linker file is specified, it should include files from its containing folder first.

@me21
Copy link

me21 commented Jul 18, 2019

Okay, I've found how to escape spaces in path: -Wl,-T'"$PROJECT_DIR\src\flash.ld"',-v.

And to make the linker include my changed files, I need to specify their full path in linker script.

Here are my linker script and include file for reference:

Linker script flash.ld:

/*
 * libmaple linker script for "Flash" builds.
 *
 * A Flash build puts .text (and .rodata) in Flash, and
 * .data/.bss/heap (of course) in SRAM, but offsets the sections by
 * enough space to store the Maple bootloader, which lives in low
 * Flash and uses low memory.
 */

/*
 * This pulls in the appropriate MEMORY declaration from the right
 * subdirectory of stm32/mem/ (the environment must call ld with the
 * right include directory flags to make this happen). Boards can also
 * use this file to use any of libmaple's memory-related hooks (like
 * where the heap should live).
 */
INCLUDE "C:\Temp\Platformio Projects\bluepill\src\mem-flash.inc"

/* Provide memory region aliases for common.inc */
REGION_ALIAS("REGION_TEXT", rom);
REGION_ALIAS("REGION_DATA", ram);
REGION_ALIAS("REGION_BSS", ram);
REGION_ALIAS("REGION_RODATA", rom);

/* Let common.inc handle the real work. */
INCLUDE common.inc

Included file mem-flash.inc:

MEMORY
{
  ram (rwx) : ORIGIN = 0x20000C00, LENGTH = 17K
  rom (rx)  : ORIGIN = 0x08002000, LENGTH = 120K
}

@ivankravets
Copy link
Member Author

Okay, I've found how to escape spaces in path: -Wl,-T'"$PROJECT_DIR\src\flash.ld"',-v.

This is correct format. Thanks for the example

@me21
Copy link

me21 commented Jul 19, 2019

@ivankravets, just for the sake of completeness - when I include other files on linker scripts, do I have to specify full path as I did? Specifying filename only didn't work for me.

@ivankravets
Copy link
Member Author

The CWD is current project directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants