readelf -a main elf文件头:各个字段 ELF Header: Magic: 7f454c 46020101000000000000000000 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0x400520 Start of program headers: 64 (bytes into file) Start of section headers: 4544 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 9 Size of section headers: 64 (bytes) Number of section headers: 30 Section header string table index: 27
各个sections Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL000000000000000000000000 00000000000000000000000000000000000 [ 1] .interp PROGBITS 000000000040023800000238 000000000000001c 0000000000000000 A 001 [ 2] .note.ABI-tag NOTE 000000000040025400000254 00000000000000200000000000000000 A 004 [ 3] .note.gnu.build-i NOTE 000000000040027400000274 00000000000000240000000000000000 A 004 [ 4] .gnu.hash GNU_HASH 000000000040029800000298 000000000000001c 0000000000000000 A 508 [ 5] .dynsym DYNSYM 00000000004002b8 000002b8 00000000000000a8 0000000000000018 A 618 [ 6] .dynstr STRTAB 000000000040036000000360 00000000000000520000000000000000 A 001 [ 7] .gnu.version VERSYM 00000000004003b2 000003b2 000000000000000e 0000000000000002 A 502 [ 8] .gnu.version_r VERNEED 00000000004003c0 000003c0 00000000000000200000000000000000 A 618 [ 9] .rela.dyn RELA 00000000004003e0000003e0 00000000000000180000000000000018 A 508 [10] .rela.plt RELA 00000000004003f8 000003f8 00000000000000900000000000000018 A 5128 [11] .init PROGBITS 000000000040048800000488 000000000000001a 0000000000000000 AX 004 [12] .plt PROGBITS 00000000004004b0 000004b0 00000000000000700000000000000010 AX 0016 [13] .text PROGBITS 000000000040052000000520 00000000000002020000000000000000 AX 0016 [14] .fini PROGBITS 000000000040072400000724 00000000000000090000000000000000 AX 004 [15] .rodata PROGBITS 000000000040073000000730 00000000000000130000000000000000 A 004 [16] .eh_frame_hdr PROGBITS 000000000040074400000744 000000000000003c 0000000000000000 A 004 [17] .eh_frame PROGBITS 000000000040078000000780 00000000000001140000000000000000 A 008 [18] .init_array INIT_ARRAY 0000000000600e1000000e10 00000000000000080000000000000000 WA 008 [19] .fini_array FINI_ARRAY 0000000000600e1800000e18 00000000000000080000000000000000 WA 008 [20] .jcr PROGBITS 0000000000600e2000000e20 00000000000000080000000000000000 WA 008 [21] .dynamic DYNAMIC 0000000000600e2800000e28 00000000000001d0 0000000000000010 WA 608 [22] .got PROGBITS 0000000000600ff8 00000ff8 00000000000000080000000000000008 WA 008 [23] .got.plt PROGBITS 000000000060100000001000 00000000000000480000000000000008 WA 008 [24] .data PROGBITS 000000000060104800001048 00000000000000180000000000000000 WA 008 [25] .bss NOBITS 000000000060106000001060 00000000000000080000000000000000 WA 001 [26] .comment PROGBITS 000000000000000000001060 00000000000000560000000000000001 MS 001 [27] .shstrtab STRTAB 0000000000000000000010b6 00000000000001080000000000000000001 [28] .symtab SYMTAB 000000000000000000001940 00000000000006c0 000000000000001829468 [29] .strtab STRTAB 000000000000000000002000 000000000000028f0000000000000000001 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings), l (large) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific)
There are no section groups in this file.
Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align PHDR 0x00000000000000400x00000000004000400x0000000000400040 0x00000000000001f80x00000000000001f8 R E 8 INTERP 0x00000000000002380x00000000004002380x0000000000400238 0x000000000000001c0x000000000000001c R 1 [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2] LOAD 0x00000000000000000x00000000004000000x0000000000400000 0x00000000000008940x0000000000000894 R E 200000 LOAD 0x0000000000000e100x0000000000600e100x0000000000600e10 0x00000000000002500x0000000000000258 RW 200000 DYNAMIC 0x0000000000000e280x0000000000600e280x0000000000600e28 0x00000000000001d00x00000000000001d0 RW 8 NOTE 0x00000000000002540x00000000004002540x0000000000400254 0x00000000000000440x0000000000000044 R 4 GNU_EH_FRAME 0x00000000000007440x00000000004007440x0000000000400744 0x000000000000003c0x000000000000003c R 4 GNU_STACK 0x00000000000000000x00000000000000000x0000000000000000 0x00000000000000000x0000000000000000 RW 10 GNU_RELRO 0x0000000000000e100x0000000000600e100x0000000000600e10 0x00000000000001f00x00000000000001f0 R 1
Relocation section '.rela.dyn' at offset 0x3e0 contains 1 entries: Offset Info Type Sym. Value Sym. Name + Addend 000000600ff8 000400000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.
Symbol table '.dynsym' contains 7 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000000000000 NOTYPE LOCAL DEFAULT UND 1: 00000000000000000 FUNC GLOBAL DEFAULT UND free@GLIBC_2.2.5 (2) 2: 00000000000000000 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.2.5 (2) 3: 00000000000000000 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.2.5 (2) 4: 00000000000000000 NOTYPE WEAK DEFAULT UND __gmon_start__ 5: 00000000000000000 FUNC GLOBAL DEFAULT UND malloc@GLIBC_2.2.5 (2) 6: 00000000000000000 FUNC GLOBAL DEFAULT UND setuid@GLIBC_2.2.5 (2)
Symbol table '.symtab' contains 72 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000000000000 NOTYPE LOCAL DEFAULT UND 1: 00000000004002380 SECTION LOCAL DEFAULT 1 2: 00000000004002540 SECTION LOCAL DEFAULT 2 3: 00000000004002740 SECTION LOCAL DEFAULT 3 4: 00000000004002980 SECTION LOCAL DEFAULT 4 5: 00000000004002b8 0 SECTION LOCAL DEFAULT 5 6: 00000000004003600 SECTION LOCAL DEFAULT 6 7: 00000000004003b2 0 SECTION LOCAL DEFAULT 7 8: 00000000004003c0 0 SECTION LOCAL DEFAULT 8 9: 00000000004003e00 SECTION LOCAL DEFAULT 9 10: 00000000004003f8 0 SECTION LOCAL DEFAULT 10 11: 00000000004004880 SECTION LOCAL DEFAULT 11 12: 00000000004004b0 0 SECTION LOCAL DEFAULT 12 13: 00000000004005200 SECTION LOCAL DEFAULT 13 14: 00000000004007240 SECTION LOCAL DEFAULT 14 15: 00000000004007300 SECTION LOCAL DEFAULT 15 16: 00000000004007440 SECTION LOCAL DEFAULT 16 17: 00000000004007800 SECTION LOCAL DEFAULT 17 18: 0000000000600e100 SECTION LOCAL DEFAULT 18 19: 0000000000600e180 SECTION LOCAL DEFAULT 19 20: 0000000000600e200 SECTION LOCAL DEFAULT 20 21: 0000000000600e280 SECTION LOCAL DEFAULT 21 22: 0000000000600ff8 0 SECTION LOCAL DEFAULT 22 23: 00000000006010000 SECTION LOCAL DEFAULT 23 24: 00000000006010480 SECTION LOCAL DEFAULT 24 25: 00000000006010600 SECTION LOCAL DEFAULT 25 26: 00000000000000000 SECTION LOCAL DEFAULT 26 27: 00000000000000000 FILE LOCAL DEFAULT ABS crtstuff.c 28: 0000000000600e200 OBJECT LOCAL DEFAULT 20 __JCR_LIST__ 29: 00000000004005500 FUNC LOCAL DEFAULT 13 deregister_tm_clones 30: 00000000004005800 FUNC LOCAL DEFAULT 13 register_tm_clones 31: 00000000004005c0 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux 32: 00000000006010601 OBJECT LOCAL DEFAULT 25 completed.6982 33: 0000000000600e180 OBJECT LOCAL DEFAULT 19 __do_global_dtors_aux_fin 34: 00000000004005e00 FUNC LOCAL DEFAULT 13 frame_dummy 35: 0000000000600e100 OBJECT LOCAL DEFAULT 18 __frame_dummy_init_array_ 36: 00000000000000000 FILE LOCAL DEFAULT ABS main.cpp 37: 00000000000000000 FILE LOCAL DEFAULT ABS func.cpp 38: 00000000000000000 FILE LOCAL DEFAULT ABS crtstuff.c 39: 00000000004008900 OBJECT LOCAL DEFAULT 17 __FRAME_END__ 40: 0000000000600e200 OBJECT LOCAL DEFAULT 20 __JCR_END__ 41: 00000000000000000 FILE LOCAL DEFAULT ABS 42: 0000000000600e180 NOTYPE LOCAL DEFAULT 18 __init_array_end 43: 0000000000600e280 OBJECT LOCAL DEFAULT 21 _DYNAMIC 44: 0000000000600e100 NOTYPE LOCAL DEFAULT 18 __init_array_start 45: 00000000006010000 OBJECT LOCAL DEFAULT 23 _GLOBAL_OFFSET_TABLE_ 46: 00000000004007202 FUNC GLOBAL DEFAULT 13 __libc_csu_fini 47: 00000000000000000 FUNC GLOBAL DEFAULT UND free@@GLIBC_2.2.5 48: 00000000000000000 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTab 49: 00000000006010480 NOTYPE WEAK DEFAULT 24 data_start 50: 000000000040068e 29 FUNC GLOBAL DEFAULT 13 _Z5funcci 51: 00000000006010600 NOTYPE GLOBAL DEFAULT 24 _edata 52: 00000000004007240 FUNC GLOBAL DEFAULT 14 _fini 53: 00000000000000000 FUNC GLOBAL DEFAULT UND printf@@GLIBC_2.2.5 54: 00000000006010584 OBJECT GLOBAL DEFAULT 24 g_n 55: 00000000000000000 FUNC GLOBAL DEFAULT UND __libc_start_main@@GLIBC_ 56: 00000000006010480 NOTYPE GLOBAL DEFAULT 24 __data_start 57: 00000000000000000 NOTYPE WEAK DEFAULT UND __gmon_start__ 58: 00000000006010500 OBJECT GLOBAL HIDDEN 24 __dso_handle 59: 00000000004007304 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used 60: 00000000004006b0 101 FUNC GLOBAL DEFAULT 13 __libc_csu_init 61: 00000000000000000 FUNC GLOBAL DEFAULT UND malloc@@GLIBC_2.2.5 62: 00000000006010680 NOTYPE GLOBAL DEFAULT 25 _end 63: 00000000004005200 FUNC GLOBAL DEFAULT 13 _start 64: 00000000006010600 NOTYPE GLOBAL DEFAULT 25 __bss_start 65: 000000000040060d 129 FUNC GLOBAL DEFAULT 13 main 66: 00000000000000000 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses 67: 000000000060105c 4 OBJECT GLOBAL DEFAULT 24 g_m 68: 00000000006010600 OBJECT GLOBAL HIDDEN 24 __TMC_END__ 69: 00000000000000000 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable 70: 00000000000000000 FUNC GLOBAL DEFAULT UND setuid@@GLIBC_2.2.5 71: 00000000004004880 FUNC GLOBAL DEFAULT 11 _init
Displaying notes found at file offset 0x00000254 with length 0x00000020: Owner Data size Description GNU 0x00000010NT_GNU_ABI_TAG (ABI version tag) OS: Linux, ABI: 2.6.24
Displaying notes found at file offset 0x00000274 with length 0x00000024: Owner Data size Description GNU 0x00000014NT_GNU_BUILD_ID (unique build ID bitstring) Build ID: 2110927a3d6ecf4dd0f70085d18bc49c3b61ba41
keshixing@ubuntu:~/testld$ g++ -o main main.cpp hello.o /usr/bin/ld: hello.o: in function `hello()': hello.cpp:(.text+0x9): undefined reference to `def()' collect2: error: ld returned 1 exit status
这里没有实现def()所以报错了;
编译过程中汇编器将汇编文件翻译为.o中间文件,这里汇编器其实已经包含到gcc中了,也可以自己下载一个特定平台的比如:NASM 输出可执行程序时,必须实现入口函数,比如main; gcc -o main main.s
在编译时除了可以生成可执行文件,还可以生产静态库,动态库,中间文件,汇编文件等,这里看静态库和动态库的生成规则 生成静态库文件: ar rvs libfunc.a func.o func2.o man ar 查看更多 生成动态库文件: gcc func.cpp -fPIC -shared -o libfunc.so -shared 该选项指定生成动态连接库(让连接器生成T类型的导出符号表,有时候也生成弱连接W类型的导出符号),不用该标志外部程序无法连接 Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. For predictable results, you must also specify the same
-fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。 更多见man gcc 5) 总: 一个完整的gcc/g++编译命令,除了指定上面的内容,还需要指定头文件的位置,默认目录有哪些?,优化选项,-c/-o/-S/-E其他选项等,这里把这些汇总下,并给上参考地址方便查阅;man gcc -c: -c Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file. -I: 指定头文件的路径 gcc -I. -I /home/include/ .. 指定了头文件路径后,在#include时可以省略路径;