先搭配好环境
sudo apt-get install nasm
创建一个asm文件
section .datatext db "Hello World", 10section .textglobal _start_start:mov rax, 1mov rdi, 1mov rsi, textmov rdx, 14syscallmov rax, 60mov rdi, 0syscall
nasm生成对象文件
nasm -f elf64 -o one.o one.asm
链接生成可执行文件
ld one.o -o one
执行文件
➜ asm ./one
Hello World
查看文件信息
➜ asm file one
one: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
➜ asm objdump -d one one: file format elf64-x86-64Disassembly of section .text:0000000000401000 <_start>:401000: b8 01 00 00 00 mov $0x1,%eax401005: bf 01 00 00 00 mov $0x1,%edi40100a: 48 be 00 20 40 00 00 movabs $0x402000,%rsi401011: 00 00 00 401014: ba 0e 00 00 00 mov $0xe,%edx401019: 0f 05 syscall 40101b: b8 3c 00 00 00 mov $0x3c,%eax401020: bf 00 00 00 00 mov $0x0,%edi401025: 0f 05 syscall
可知其为可执行文件&#xff08;executable&#xff09;
来看看具体的文件架构
➜ C readelf -a ../asm/one
ELF Header:Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64Data: 2&#39;s complement, little endianVersion: 1 (current)OS/ABI: UNIX - System VABI Version: 0Type: EXEC (Executable file)Machine: Advanced Micro Devices X86-64Version: 0x1Entry point address: 0x401000Start of program headers: 64 (bytes into file)Start of section headers: 8504 (bytes into file)Flags: 0x0Size of this header: 64 (bytes)Size of program headers: 56 (bytes)Number of program headers: 3Size of section headers: 64 (bytes)Number of section headers: 6Section header string table index: 5Section Headers:[Nr] Name Type Address OffsetSize EntSize Flags Link Info Align[ 0] NULL 0000000000000000 000000000000000000000000 0000000000000000 0 0 0[ 1] .text PROGBITS 0000000000401000 000010000000000000000027 0000000000000000 AX 0 0 16[ 2] .data PROGBITS 0000000000402000 00002000000000000000000c 0000000000000000 WA 0 0 4[ 3] .symtab SYMTAB 0000000000000000 0000201000000000000000d8 0000000000000018 4 5 8[ 4] .strtab STRTAB 0000000000000000 000020e80000000000000026 0000000000000000 0 0 1[ 5] .shstrtab STRTAB 0000000000000000 0000210e0000000000000027 0000000000000000 0 0 1
Key to Flags:W (write), A (alloc), X (execute), M (merge), S (strings), I (info),L (link order), O (extra OS processing required), G (group), T (TLS),C (compressed), x (unknown), o (OS specific), E (exclude),l (large), p (processor specific)There are no section groups in this file.Program Headers:Type Offset VirtAddr PhysAddrFileSiz MemSiz Flags AlignLOAD 0x0000000000000000 0x0000000000400000 0x00000000004000000x00000000000000e8 0x00000000000000e8 R 0x1000LOAD 0x0000000000001000 0x0000000000401000 0x00000000004010000x0000000000000027 0x0000000000000027 R E 0x1000LOAD 0x0000000000002000 0x0000000000402000 0x00000000004020000x000000000000000c 0x000000000000000c RW 0x1000Section to Segment mapping:Segment Sections...00 01 .text 02 .data There is no dynamic section in this file.There are no relocations in this file.The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.Symbol table &#39;.symtab&#39; contains 9 entries:Num: Value Size Type Bind Vis Ndx Name0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000401000 0 SECTION LOCAL DEFAULT 1 2: 0000000000402000 0 SECTION LOCAL DEFAULT 2 3: 0000000000000000 0 FILE LOCAL DEFAULT ABS one.asm4: 0000000000402000 0 NOTYPE LOCAL DEFAULT 2 text5: 0000000000401000 0 NOTYPE GLOBAL DEFAULT 1 _start6: 000000000040200c 0 NOTYPE GLOBAL DEFAULT 2 __bss_start7: 000000000040200c 0 NOTYPE GLOBAL DEFAULT 2 _edata8: 0000000000402010 0 NOTYPE GLOBAL DEFAULT 2 _endNo version information found in this file.
慢慢来吧&#xff01;