分页: 1 / 1

为什么gcc编译出来的ELF文件是“shared object”格式

发表于 : 2023-01-31 11:14
jiandan23
如果是Mint19.2系统(对应Ubuntu 18.04):
user@mint19:~$ cat hello.c
#include<stdio.h>
int main(){
printf("hello!\n");
return 0;
}
user@mint19:~$ gcc -o hello hello.c
user@mint19:~$ ./hello
hello!
user@mint19:~$ file hello
hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=5b58c0bfb8b27791cf9c628c861e613a41b0bca7, not stripped
如果是Mint21.1系统(对应Ubuntu 22.04):
user@mint21:~$ cat hello.c
#include<stdio.h>
int main(){
printf("hello!\n");
return 0;
}
user@mint21:~$ gcc -o hello hello.c
user@mint21:~$ ./hello
hello!
user@mint21:~$ file hello
hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=9b3d52481c82082ae41d7596191ffb3e8d941c98, for GNU/Linux 3.2.0, not stripped
为什么会有这种不同,这个是在哪里设置的?

Re: 为什么gcc编译出来的ELF文件是“shared object”格式

发表于 : 2023-01-31 12:10
onlylove
之前哪个版本的gcc的默认设置不一样好像?需要加个参数还是咋回事来着,忘了(之前遇到过一次,这里好像也有人问过

Re: 为什么gcc编译出来的ELF文件是“shared object”格式

发表于 : 2023-01-31 22:57
astolia
onlylove 写了: 2023-01-31 12:10 之前哪个版本的gcc的默认设置不一样好像?需要加个参数还是咋回事来着,忘了(之前遇到过一次,这里好像也有人问过
如果半懂不懂,可以不用强行回答。这个跟gcc参数没关系。
jiandan23 写了: 2023-01-31 11:14 为什么会有这种不同,这个是在哪里设置的?
关于pie,参考 https://forum.ubuntu.com.cn/viewtopic.php?f=77&t=480688
18.04中的file是旧版本,没对用pie编译出的可执行文件和库文件做区分。22.04中的file已经更新过了,能区分这两种情况。