Hello!
I am trying to make a bin image of my application for me to flash it into my ARM Cortex M0 processor. I have succeeded in outputting an elf image, but failed with converting it to a bin with the arm-none-eabi-objcopy tool. Here is the line from the makefile:
%.$(TARGET).bin: %.$(TARGET).elf $(TRACE_OBJCOPY) $(Q)$(OBJCOPY) -O binary $< $@
Here is all of my Makefile.
#Set the toolchain program names. CC = arm-none-eabi-gcc AR = arm-none-eabi-ar AS = arm-none-eabi-as LD = arm-none-eabi-gcc OBJCOPY = arm-none-eabi-objcopy #Set the processor related flags (common to C code and linker). CPUFLAGS += -mcpu=cortex-m0plus -mthumb -mfloat-abi=soft CPUFLAGS += -specs=nano.specs -specs=nosys.specs #Set common C code flags. CFLAGS += -D__SAMD21G18A__=1 CFLAGS += -Wall -g -ffunction-sections -fdata-sections -O2 CFLAGS += $(CPUFLAGS) #Set the linker flags. LDFLAGS += -Wl,--gc-sections LDFLAGS += -Wl,-T$(CONTIKI_CPU)/atsamd21g18a.ld -lc LDFLAGS += -Wl,-Map=$(OBJECTDIR)/$(CONTIKI_PROJECT).map LDFLAGS += $(CPUFLAGS) #Configure the CPU path and source files. CONTIKI_CPU_DIRS += . CONTIKI_CPU_DIRS += dev CONTIKI_CPU_DIRS += samd21dfp/ CONTIKI_CPU_DIRS += samd21dfp/samd21a/include CONTIKI_CPU_DIRS += samd21dfp/samd21a/include/component CONTIKI_CPU_DIRS += samd21dfp/samd21a/include/instance CONTIKI_CPU_DIRS += ../arm/common/CMSIS CONTIKI_SOURCEFILES += samd21-startup.c clock.c rtimer-arch.c CONTIKI_SOURCEFILES += rtc.c evsys.c PROJECT_OBJECTFILES += ${addprefix $(OBJECTDIR)/,$(CONTIKI_TARGET_MAIN:.c=.o)} #Add objcopy to the verbosity control. ifeq ($(V),1) TRACE_OBJCOPY = else TRACE_OBJCOPY = @echo " OBJCOPY " $@ endif #Don't treat project object files and syscalls.o as intermediates (avoids deletion and recompiling). .SECONDARY: $(PROJECT_OBJECTFILES) .SECONDARY: $(OBJECTDIR)/syscalls.o #This rule creates a .bin image from the .elf file. Its output is made precious so it doesn't get #deleted even if it's an intermediate. .PRECIOUS: %.$(TARGET).bin %.$(TARGET).bin: %.$(TARGET).elf $(TRACE_OBJCOPY) $(Q)$(OBJCOPY) -O binary $< $@ .PRECIOUS: %.$(TARGET).elf %.$(TARGET).elf: %.co $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) contiki-$(TARGET).a \ $(OBJECTDIR)/syscalls.o $(CONTIKI_SOURCEFILES) $(TRACE_LD) $(Q)$(CC) $(LDFLAGS) -Wl,--section-start=.text=0x2000 $(TARGET_STARTFILES) ${filter-out %.a,$^} \ ${filter %.a,$^} $(TARGET_LIBFILES) -o $@ -I$(CONTIKI_CPU_DIRS) -I$(OBJECTDIR) #Add the .elf file to the clean list. CLEAN += *.$(TARGET).elf *.$(TARGET).bin
Thank you for replying!
My TARGET is the name of my platform "Adafruit-feather" that includes the processor's Makefile. I have updated this Makefile, but another error comes up.
The update:
%.$(TARGET): %.$(TARGET).bin %.$(TARGET).elf cp $< $@ .PRECIOUS: %.$(TARGET).bin %.$(TARGET).bin: %.$(TARGET).elf -I$(CONTIKI_CPU_DIRS) $(TRACE_OBJCOPY) $(Q)$(OBJCOPY) -O binary $< $@
I included the -I$(CONTIKI_CPU_DIRS) since it wasn't finding those directories. No errors are given and the output is still just an elf file (ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped).
The name of the outputted file is hello-world.Adafruit-feather , and I have tried make hello-world.Adafruit-feather.bin but it gives out:
make: *** No rule to make target `hello-world.Adafruit-feather.bin'. Stop.
GNU make takes --debug flag to allow debugging its behaviour. Try with --debug=a (or its short-cut -d), and analyze the output.
Edit:
BDyi said:failed with converting it to a bin
Is the TRACE_OBJCOPY message visible when building? Does the objcopy command even run?
Since you do get an elf file, try manually running the objcopy command to see if it does or does not generate the binary.
You can also sprinkle $(error "msg") and @echo at appropriate locations in the Makefile to print debug messages.
I have turned on Verbose and no, the TRACE_OBJCOPY message isn't visible when building. This leads me to believe objcopy is not being run for some reason.
I have tried to run the objcopy command manually, and it outputs:
objcopy: Unable to recognize the format of the input file `hello-world.Adafruit-feather'
Note that the output file doesn't have .elf extension explicitly, but when I use the file command in linux it gives out:
ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped.
I have even tried to use the linker command "--oformat binary" that should specifiy the binary format for the output object file, hoping it would make the objcopy recognize the input format. But it didn't change anything:
%.$(TARGET).elf: %.co $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) contiki-$(TARGET).a \ $(OBJECTDIR)/syscalls.o $(CONTIKI_SOURCEFILES) $(TRACE_LD) $(Q)$(CC) $(LDFLAGS) -Wl,--section-start=.text=0x2000 -Wl,--oformat binary $(TARGET_STARTFILES) ${filter-out %.a,$^} \ ${filter %.a,$^} $(TARGET_LIBFILES) -o $@ -I$(CONTIKI_CPU_DIRS) -I$(OBJECTDIR)
I have not built .elf files. My linker line, for SiliconLabs EFM32GG series, is such that *.o are linked, along with any .a files you need, into a .axf file. Perhaps your .elf is my .axf.
Once that is done, the .bin file can be obtained from the .axf. You don't need -I directives as prerequisites of the .bin file. The -I directive is to tell the C compiler where header files (*.h) live. That step is way earlier in the build process. If you can't build foo.o from foo.c, you can't produce an elf/axf file at all.
I suspect you are a make newbie? Fair enough, we all were once. I suggest you read the GNU make manual a bit.
FWIW I just did this on my system, where I had a .axf file to hand:
$ mv foo.axf foo.elf
$ arm-none-eabi-objcopy -O binary foo.elf foo.bin
works fine. I then deleted the suffix altogether:
$ mv foo.elf foo
$ arm-none-eabi-objcopy -O binary foo foo.bin
and that works too. So the file sufffix of the linker output is not crucial.
Yes tobermory, I am a make newbie :)
I managed to convert the elf to a bin file!
I called the objcopy command manually from Windows host OS instead of my linux virtual machine, and it worked.
For some reason, the same command didn't work in the VM.
Thank you for the help!