Mavericks上使用Clang设置自定义C入口点

Mavericks上使用Clang设置自定义C入口点

本文介绍了如何在OS X Mavericks上使用Clang设置自定义C入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有以下程序:

#include <stdio.h>

int bob() {
    printf("bob\n");
    return 0;
}

int main() {
    printf("main\n");
    return 0;
}

在Linux上,我可以通过以下方式启用自定义入口点:

On Linux, I can enable a custom entry point via:

gcc test.c -Wl,-e,bob

运行生成的程序时,我得到:

When I run the resulting program, I get:

./a.out
bob

在OS X上,这不起作用:

On OS X, however, this doesn't work:

clang test.c -Wl,-e,bob
./a.out
main

我已经尽一切努力使它起作用.我认为这可能是一个错误.这是带有-v选项的输出:

I've tried everything to get this to work. I think it might be a bug. Here's the output with the -v option:

clang test.c -Wl,-e,bob -v

Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test.c -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 236.3 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1 -fdebug-compilation-dir /Users/mfichman/jogo -ferror-limit 19 -fmessage-length 125 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fencode-extended-block-signature -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /var/folders/4z/q41by0256hjc7s6v8ljmfpw8lywh5g/T/test-9b80a6.o -x c test.c
clang -cc1 version 5.1 based upon LLVM 3.4svn default target x86_64-apple-darwin13.3.0
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -e bob -o a.out /var/folders/4z/q41by0256hjc7s6v8ljmfpw8lywh5g/T/test-9b80a6.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a

您可以看到clang正确地将-e传递给ld,所以这可能是Apple的ld问题.如果是这样,我会对解决方法感兴趣.

You can see that clang is correctly passing -e to ld, so maybe this is a problem with Apple's ld. If that's the case, I'd be interested in workaround solutions.

推荐答案

-e参数覆盖的默认入口点不是_main而是start,它负责设置和调用_main ,然后将_main的返回值传递给_exit.如果您指定自己的入口点,则需要自己执行这些步骤.当前没有办法为您执行此初始化,而是使用其他主要功能,因为_main的使用已硬编码到工具中.

The default entry point overridden by the -e argument is not _main but rather start, which is responsible for setting up and calling _main, then passing the return value of _main to _exit. If you specify your own entry point you'll need to perform these steps yourself. There's currently no way to have this initialization performed for you but use a different main function as use of _main is hard-coded into the tools.

-e参数被忽略的原因是由于10.8的更改.在此版本之前,start的实现通过 crt1.o .在10.8中,可以通过dyld执行start处理,并且LC_MAIN加载命令指定程序中主要功能的偏移量.更改此偏移量似乎是您想要的,但是当前无法实现,因为使用LC_MAIN启动方法时,ld始终使用_main而不理会-e参数.要指定您自己的入口点,您需要告诉ld使用旧的程序启动方法,您可以通过将-no_new_main传递给链接程序来对部署目标为10.8或更高版本的应用程序执行此操作.对于部署目标早于10.8的应用程序,这是默认行为.

The reason your -e argument is ignored is due to a change in 10.8. Prior to this release the implementation of start was linked into each application via crt1.o. In 10.8 the start processing can be performed by dyld and the LC_MAIN load command specifies the offset to the main function within the program. Changing this offset appears to be what you want, but this is not currently possible as when the LC_MAIN startup method is used ld always uses _main and disregards the -e argument. To specify your own entry point you need to tell ld to use the old method of program startup, which you can do for an application with a deployment target of 10.8 or later by passing -no_new_main to the linker. This is the default behavior for applications with a deployment target earlier than 10.8.

这篇关于如何在OS X Mavericks上使用Clang设置自定义C入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:24