Linking with another start-up file

你说的曾经没有我的故事 提交于 2021-02-19 02:37:09

问题


I am trying to link a program with my own start-up file by using the STARTUP directive in a LD script:

...
ENTRY(_start)
STARTUP(my_crt1.o)
...

GCC driver is used to link the program (not to bother with library paths like libgcc, etc.):

gcc -T my_script.ld ...

Unfortunately, it only works with a GCC compiled for powerpc targets, while arm or i686 targets don't and still include crt0.o in collect2. For example:

arm-eabi-g++ -v -T my_script.ld ...

gives me:

collect2 ... /opt/lib/gcc/arm-eabi/4.8.0/../../../../arm-eabi/lib/crt0.o ...

and thus:

crt0.S:101: multiple definition of `_start'

It seems the STARTUP directive is totally ignored (the powerpc target uses its default crt0 too unless the STARTUP directive is specified) and there is no way to disable the default crt0.

Is there a portable way to link against another start-up file?

My start-up file uses libgcc functions (to call ctors and dtors) so crtbegin.o, crtend.o, etc. are needed so I would like to avoid the -nostartfiles option which disables crt*.o - I need to disable crt0.o only.

Thank you


回答1:


I am trying to link a program with my own start-up file ...
GCC driver is used to link the program ...

In that case, you must also supply -nostartfiles flag to GCC.




回答2:


This limitation indeed forces you to disable the default startup files with -nostartfiles (I prefer -nostdlib). You then need to build by yourself the list of run-time objects. gcc has the option -print-file-name to print the absolute path of libraries it was compiled with (crtbegin.o, crtend.o, libgcc.a...). For example: arm-eabi-g++ <FLAGS> -print-file-name=crtbegin.o

Here is the GNU Make macro I use (providing gcc and cflags):

define m.in/toolchain/gnu/locate =
$(strip
  $(shell $(m.in/toolchain/gnu/bin/gcc) $(m.in/toolchain/gnu/cflags) \
          -print-file-name=$(m.in/argv/1))
)
endef

crtn := $(call m.in/toolchain/gnu/locate, crtn.o)


来源:https://stackoverflow.com/questions/16639792/linking-with-another-start-up-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!