新ちゃん 发表于 2020-10-1 13:34:27

RISC-V GCC:-specs=nano.specs 的作用

本帖最后由 新ちゃん 于 2020-10-1 13:34 编辑

-specs 的作用首先看 gcc 的文档:
-specs=file
Process file after the compiler reads in the standard specs file, in order to override the defaults which the gcc driver program uses when determining what switches to pass to cc1, cc1plus, as, ld, etc. More than one -specs=file can be specified on the command line, and they are processed in order, from left to right. See Spec Files, for information about the format of the file.
原来咱们执行的 gcc 并不是真正的编译器,而是 driver ,即驱动器,gcc 驱动器根据命令行的参数再调用真正的编译器(cc1,cc1plus)、汇编器(as)、链接器(ld)。gcc 驱动器调用 cc1、as、ld 时遵循一定的规则,这个规则就叫做 specs,gcc 有一套自带的 specs ,就是帮助中所说的 standard specs file,standard specs file 可以通过命令打印:riscv-none-embed-gcc -dumpspecs
如果命令行中没有指定 -specs=file 参数,那么 gcc 就使用自带的 specs,如果指定了 -specs=file 参数,那么在处理完自带的 specs 后,继续处理参数给出的 specs 文件,而且可以通过多个 -specs=file 参数指定多个 specs 文件,gcc 会按出现的次序依次处理。后面的 specs 文件可以覆盖、修改、删除前面的 specs 中的规则。这里的规则是指 gcc 以什么样的参数调用 cc1、as、ld 。cc1 是真正的 C 编译器,cc1plus 是 C++ 编译器。
nona.specs 的作用
可以翻看一下 nano.specs 的文件内容,文件位于 <gcc-install-dir>/riscv-none-embed/lib/ 目录下:%rename link                nano_link
%rename link_gcc_c_sequence                nano_link_gcc_c_sequence
%rename cpp                nano_cpp

*cpp:
-isystem =/include/newlib-nano %(nano_cpp)

*nano_libc:
-lc_nano

*nano_libgloss:
%{specs=nosys.specs:-lnosys}

*link_gcc_c_sequence:
%(nano_link_gcc_c_sequence) --start-group %G %(nano_libc) %(nano_libgloss) --end-group

*link:
%(nano_link) %:replace-outfile(-lc -lc_nano) %:replace-outfile(-lg -lg_nano)%:replace-outfile(-lrdimon -lrdimon_nano) %:replace-outfile(-lstdc++ -lstdc++_nano) %:replace-outfile(-lsupc++ -lsupc++_nano)

*lib:
%{!shared:%{g*:-lg_nano} %{!p:%{!pg:-lc_nano}}%{p:-lc_p}%{pg:-lc_p}}
specs 文件的格式请参考 Spec Files。
nona.specs 将 -lc 替换成 -lc_nano,即:使有精简版的C库替代标准C库。精简的C库有些特性是被排除掉的,比如 printf* 系列函数不支持浮点数的格式化,因为做了精简,因此最终生成的程序映像要比使用标准C库要小一些。如果没有用到这部分特性,就可以通过 -specs=nano.specs 节约有限的代码空间,如果使用了该参数后发现有些C库函数行为不符合预期,比如 sprintf 没有格式化浮点数,那么将这个参数去掉。


页: [1]
查看完整版本: RISC-V GCC:-specs=nano.specs 的作用