我想在没有(g)libc的情况下编译我的C代码.如何停用它以及哪些功能依赖于它?
我尝试了-nostdlib但它没有帮助:代码是可编译的并运行,但我仍然可以在我的可执行文件的hexdump中找到libc的名称.
你能帮我理解一下吗?
__start
Run Code Online (Sandbox Code Playgroud)
在内部用于C?
它是main函数的精确副本还是编译程序的入口点?
只是想知道它是如何被使用的?
主要是C程序中的第一个函数还是第一个可执行语句?如果有全局变量int a=0;怎么办?
我一直被告知主要是一个程序的起点.但是在我看来,赋予某些价值的全局变量又是一个可执行的陈述呢?
每个程序都有一个main()程序执行从那里开始.是否可以编写一个程序而不是main()另一个函数作为入口点?如果是这样,任何人都可以告诉我该怎么办?我在使用Linux?
我正在尝试编写一个没有 main 的 C++ 程序。是否可以将 mach-o 可执行文件的入口点更改为自定义函数(除了main())?
如果没有,那么,是否可以在main调用实际的 C 之前包装以调用我的 main 版本main?
编辑:
我想我的自定义函数调用 C main。如果我给它一个构造函数属性或将它添加到 ctor 列表,那么main将被调用两次。我不希望这种情况发生。
PS 我正在 Mac OS X High Sierra 中使用 clang 版本 9.1.0 构建可执行文件
我试图在这个问题上帮助OP .
我发现下面的代码会导致分段错误,即使堆栈设置为2000 KB也是如此.
int main ()
{
int a[510000];
a[509999] = 1;
printf("%d", a[509999]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,数组是510000 x 4bytes = 2040000bytes.
使用ulimit命令将堆栈设置为2000 KB(2048000字节):
根据这些数字,应用程序有空间存储数组,但随机返回分段错误.
有任何想法吗?
如何使用没有Glibc的C中的内联汇编来获取参数值?
我需要这个代码用于Linuxarchecture x86_64和i386.如果你知道MAC OS X或者Windows,也提交并请指导.
void exit(int code)
{
//This function not important!
//...
}
void _start()
{
//How Get arguments value using inline assembly
//in C without Glibc?
//argc
//argv
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
https://gist.github.com/apsun/deccca33244471c1849d29cc6bb5c78e
和
#define ReadRdi(To) asm("movq %%rdi,%0" : "=r"(To));
#define ReadRsi(To) asm("movq %%rsi,%0" : "=r"(To));
long argcL;
long argvL;
ReadRdi(argcL);
ReadRsi(argvL);
int argc = (int) argcL;
//char **argv = (char **) argvL;
exit(argc);
Run Code Online (Sandbox Code Playgroud)
但它仍然返回0.所以这段代码错了!请帮忙.
编译以下代码时:
global main
extern printf, scanf
section .data
msg: db "Enter a number: ",10,0
format:db "%d",0
section .bss
number resb 4
section .text
main:
mov rdi, msg
mov al, 0
call printf
mov rsi, number
mov rdi, format
mov al, 0
call scanf
mov rdi,format
mov rsi,[number]
inc rsi
mov rax,0
call printf
ret
Run Code Online (Sandbox Code Playgroud)
使用:
nasm -f elf64 example.asm -o example.o
gcc -no-pie -m64 example.o -o example
Run Code Online (Sandbox Code Playgroud)
然后运行
./example
Run Code Online (Sandbox Code Playgroud)
它运行,打印:输入数字: 但随后崩溃并打印: 分段错误(核心已转储)
因此,printf可以正常工作,而scanf则不能。我对scanf有什么错呢?
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov edx, len ;message length
mov ecx, msg ;message to write
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string
Run Code Online (Sandbox Code Playgroud)
这是打印"Hello,World!"的基本汇编代码.屏幕上.现在我想问一下这段代码是如何在幕后工作的.就像所有这些指令的需要一样
nasm -felf -g -Fdwarf hello.asm
gcc …Run Code Online (Sandbox Code Playgroud)