g ++,静态初始化和-nostdlib [英] g++, static initialization and -nostdlib

查看:154
本文介绍了g ++,静态初始化和-nostdlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 .init编译/链接 -nostdlib 似乎可以防止静态初始化,即使我将自己的crti.s和crtn.s添加到 .init / .fini 部分。

是否有解决方法使g ++生成静态初始化代码插入 .init 或者我可以手动调用?



这就是我试过的:

  g ++ -o test.o -c -fno-use-cxa -atexit test.cc#has _start(entry point)
#调用_init和_main
作为-o crti.o crti.s#在_init节具有_init .init
作为-o crtn.o crtn.s
g ++ -o测试./ crti.o test.o -nodefaultlibs -nostartfiles ./crtn.o

-nodefaultlibs 单独包含静态初始化代码和调用,但强制使用libc-_start / _init。

-nodefaultlibs -nostartfiles 允许我使用自己的_start / _init,但不包含代码或cal l来静态初始化。

解决方案

From gcc linker docs



-nostdlib



链接时不要使用标准系统启动文件或库。 没有启动文件,只有指定的库将传递给链接器
,以及指定系统
库的链接的选项,例如 - static-libgcc或-shared-libgcc将被忽略。


因此,使用

-nodefaultlibs



链接时不要使用标准系统库。 只有指定的库将传递给链接器,指定系统库链接的选项(如-static-libgcc或-shared-libgcc)将被忽略。 标准启动文件正常使用,除非使用-nostartfiles 。编译器可以生成对memcmp,memset,memcpy和memmove的调用。这些条目通常由libc中的条目解决。当指定此选项时,应通过其他机制提供这些入口点。 -Wl,-static

-Wl将下一个命令传递给链接器
-static在支持动态链接的系统上,这样可以避免与共享库链接到
。在其他系统上,此选项不起作用。


Compiling / linking with -nostdlib seems to prevent static initialization, even if I add my own crti.s and crtn.s with .init/.fini sections.

Are there workarounds to make g++ generate static initialization code that is inserted in .init or that I can call manually?

This is what I tried:

g++ -o test.o -c -fno-use-cxa-atexit test.cc  # has _start (entry point) 
                                              #   that calls _init and _main
as -o crti.o crti.s      # has _init in section .init
as -o crtn.o crtn.s
g++ -o test ./crti.o test.o -nodefaultlibs -nostartfiles ./crtn.o

-nodefaultlibs alone includes static initialization code and call, but forces use of libc-_start/_init.

-nodefaultlibs -nostartfiles allows me to use my own _start / _init, but does not include code or call to static initialization.

解决方案

From gcc linker docs,

-nostdlib

Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify will be passed to the linker, and options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, are ignored.

Hence use,

-nodefaultlibs

Do not use the standard system libraries when linking. Only the libraries you specify will be passed to the linker, options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, will be ignored. The standard startup files are used normally, unless -nostartfiles is used. The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified.

Also try,

g++ -Wl, -static

-Wl      passes the next command on to the linker
-static  On systems that support dynamic linking, this prevents linking with 
         the shared libraries. On other systems, this option has no effect.

这篇关于g ++,静态初始化和-nostdlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆