provide nano libc in source

Asked by DU HUANPENG

It seems complicate to compile a toolchain provided pre-compile c library.
I compile a Hello world program, it is 31KiB, my SoC just has 32KiB flash. :(

my toolchain is installed in
/opt/gcc-arm-none-eabi-4_8-2014q3

my gcc version:
http://paste.ubuntu.com/9567874/
gcc version 4.8.4 20140725 (release) [ARM/embedded-4_8-branch revision 213147] (GNU Tools for ARM Embedded Processors)

here is my Makefile
- - - -
ARCH := arm
CROSS_COMPILE := arm-none-eabi-

CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)ld
OBJCOPY=$(CROSS_COMPILE)objcopy

CFLAGS := -mthumb -mcpu=cortex-m0
CFLAGS += --specs=nano.specs
CFLAGS += -Os -flto -ffunction-sections -fdata-sections
CFLAGS += -fno-builtin
CFLAGS += --specs=nosys.specs

LDFLAGS := --gc-sections
LDFLAGS += -nostartfiles
LDFLAGS += -Map=jingwen.map
LDFLAGS += -T jingwen.lds
LIBPATH := -L /opt/gcc-arm-none-eabi-4_8-2014q3/lib/gcc/arm-none-eabi/4.8.4/armv6-m/
LIBPATH += -L /opt/gcc-arm-none-eabi-4_8-2014q3/arm-none-eabi/lib/armv6-m/

SRCS := arch/arm/kernel/startup.S
SRCS += arch/arm/mach-asm018x/lowlevel_init.c
SRCS += arch/arm/mach-asm018x/retarget.c
SRCS += usr/main.c

OBJS := arch/arm/kernel/startup.o
OBJS += arch/arm/mach-asm018x/lowlevel_init.o
OBJS += arch/arm/mach-asm018x/retarget.o
OBJS += usr/main.o

jingwen.bin: jingwen
 $(OBJCOPY) -O binary $< $@
 cp $@ ~/

jingwen: $(OBJS)
 $(LD) $(LDFLAGS) -o $@ --start-group $^ --end-group $(LIBPATH) -lc -lgcc

%.o: %.S
 $(CC) $< -c -o $@ $(CFLAGS)

%.o: %.s
 $(AS) $< -c -o $@ $(CFLAGS)

%.o: %.c
 $(CC) $< -c -o $@ $(CFLAGS)

clean:
 rm -f hello jingwen jingwen.bin startup.o $(OBJS) *.map

- - - -
1. what is wrong with my makefile.
2. can arm provide the source of nano-libc, not mixing with newlib.
   integrate whole newlib source into a project, the project will big and become complex.

thanks,
Du Huanpeng

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
DU HUANPENG
Solved:
Last query:
Last reply:
Revision history for this message
Terry Guo (terry.guo) said :
#1

Quick answer to question 2), we don't have separated newlib-nano source either. All the code are contributed back to upstream Newlib community where there is only one code repository. The build of newlib-nano is done via configuration options.

I don't know your project. But usually we use CMSIS to handle board related stuff. Normally we don't directly pull file from Newlib source tree or put our files there.

Revision history for this message
DU HUANPENG (u74147) said :
#2

this project don't contain anything. all functions is empty. No CMSIS.

Revision history for this message
Terry Guo (terry.guo) said :
#3

Is it possible for you to share your project here for us to reproduce?

Revision history for this message
DU HUANPENG (u74147) said :
#4

I will push it very soon.

Revision history for this message
DU HUANPENG (u74147) said :
#5

Hi, my source code is here:

https://github.com/zhang3/bar-ctrl-board/tree/asm018x

if your current pc don't have git installed, you can download it as a zip ball.

https://github.com/zhang3/bar-ctrl-board/archive/asm018x.zip

Revision history for this message
Terry Guo (terry.guo) said :
#6

Hi,

You are not linking with the newlib-nano, you are linking with the newlib.

For the final link step, please use arm-none-eabi-gcc rather than the arm-none-eabi-ld. The correct link command is:

arm-none-eabi-gcc -Wl,--gc-sections -nostartfiles -Wl,-Map=jingwen.map -T jingwen.lds -o jingwen arch/arm/kernel/startup.o arch/arm/mach-asm018x/lowlevel_init.o arch/arm/mach-asm018x/retarget.o usr/main.o -specs=nano.specs -mthumb -mcpu=cortex-m0

key points:
1) you don't need to worry about the positions of input files in this way.
2) newlib nano will be linked via option -specs=nano.specs
3) arm-none-eabi-gcc will help you find correct libc and libgcc via multilib system, the option "-mthumb -mcpu=cortex-m0" is critical here.

terguo01@terry-pc01:bar-ctrl-board-asm018x$ arm-none-eabi-size jingwen
   text data bss dec hex filename
   4264 100 12 4376 1118 jingwen

Revision history for this message
DU HUANPENG (u74147) said :
#7

I know I was linking with newlib, not nano-newlib.
I add --specs=nano.specs to ld, but result this:
- - -
arm-none-eabi-ld --gc-sections -nostartfiles -Map=jingwen.map --specs=nano.specs -T jingwen.lds -o jingwen --start-group arch/arm/kernel/startup.o arch/arm/mach-asm018x/lowlevel_init.o arch/arm/mach-asm018x/retarget.o usr/main.o --end-group -L /opt/gcc-arm-none-eabi-4_8-2014q3/lib/gcc/arm-none-eabi/4.8.4/armv6-m/ -L /opt/gcc-arm-none-eabi-4_8-2014q3/arm-none-eabi/lib/armv6-m/ -lc -lgcc
arm-none-eabi-ld: unrecognized option '--specs=nano.specs'
arm-none-eabi-ld: use the --help option for usage information
make: *** [jingwen] Error 1
- - -
if i remove this option, it link to newlib, result firmware size 31KiB.
is this option is compatible with other toolchain?
where is the nano newlib located?

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#8

Hi Kevin,

--specs is a gcc option so you need to link with gcc. Therefore you can also remove the -lc -lgcc. Just replace arm-none-eabi-ld by arm-none-eabi-gcc and it should work. If needed, you can pass ld specific option from gcc by using -Wl.

Hope this helps.

Best regards.

Revision history for this message
DU HUANPENG (u74147) said :
#9

maybe it is not compatible with other gcc toolchain.

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#10

Hi Kevin,

This should definitely work with our 4.8Q3 toolchain. What I mean is that you should enter:

arm-none-eabi-gcc --gc-sections -nostartfiles -Wl,-Map=jingwen.map --specs=nano.specs -T jingwen.lds -o jingwen -Wl,--start-group,arch/arm/kernel/startup.o arch/arm/mach-asm018x/lowlevel_init.o,arch/arm/mach-asm018x/retarget.o,usr/main.o,--end-group -L /opt/gcc-arm-none-eabi-4_8-2014q3/lib/gcc/arm-none-eabi/4.8.4/armv6-m/ -L /opt/gcc-arm-none-eabi-4_8-2014q3/arm-none-eabi/lib/armv6-m/

The form -Wl,option1,option2,option3 will pass option1 option2 option3 to the linker which allow to give linker specific option to gcc such as the --start-group --end-group block. If it doesn't work please let me know what error do you have and what command line you tried and we should fine the problem.

Best regards.

Revision history for this message
DU HUANPENG (u74147) said :
#11

no problem with arm's toolchain. but may not works with *other* gcc toolchain.
so, my projects depends on arm's gcc.
i just check gcc's manual for option -spec and take a look at arm's gcc toolchain.
there are spec.xxx files, it is special.
it's different with others, i don't want my projects depends on special toolchain.
that's why i ask for a isolate nano libc.
i know i can pull sources from newlib, but it cost works.

Revision history for this message
DU HUANPENG (u74147) said :
#12

- - - ecos - - -
du@sailor:~/m0/m01/m0$ make
arm-eabi-gcc arch/arm/kernel/startup.S -c -o arch/arm/kernel/startup.o -mthumb -mcpu=cortex-m0 -Wl,--gc-sections -nostartfiles -Wl,-Map=jingwen.map -T jingwen.lds -specs=nano.specs --specs=nosys.specs -Os -flto -ffunction-sections -fdata-sections -fno-builtin
arm-eabi-gcc: error: nano.specs: No such file or directory
make: *** [arch/arm/kernel/startup.o] Error 1

- - - codesourcery - - - - -
du@sailor:~/m0/m01/m0$ make
arm-none-linux-gnueabi-gcc arch/arm/kernel/startup.S -c -o arch/arm/kernel/startup.o -mthumb -mcpu=cortex-m0 -Wl,--gc-sections -nostartfiles -Wl,-Map=jingwen.map -T jingwen.lds -specs=nano.specs --specs=nosys.specs -Os -flto -ffunction-sections -fdata-sections -fno-builtin
arm-none-linux-gnueabi-gcc: error: nano.specs: No such file or directory
make: *** [arch/arm/kernel/startup.o] Error 1

- - - linaro - - -
du@sailor:~/m0/m01/m0$ make
arm-linux-gnueabi-gcc arch/arm/kernel/startup.S -c -o arch/arm/kernel/startup.o -mthumb -mcpu=cortex-m0 -Wl,--gc-sections -nostartfiles -Wl,-Map=jingwen.map -T jingwen.lds -specs=nano.specs --specs=nosys.specs -Os -flto -ffunction-sections -fdata-sections -fno-builtin
arm-linux-gnueabi-gcc: error: nano.specs: No such file or directory
make: *** [arch/arm/kernel/startup.o] Error 1

- - - no toolchain else can compile with this. - - -

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#13

Hi Kevin,

I understand your concern now and indeed it's a valid one. What the --specs option does is just specify the location of a linker script which describe how to do the link. It just avoid creating very long command line. You can take a look at the linker script specification at [1] and translate the content of the nano.specs into command line switches for each toolchain. As you said it's a bit more work but it would achieve your goal of being more toolchain independent.

[1] https://sourceware.org/binutils/docs/ld/Scripts.html

Hope this helps.

Best regards.