vim maximum.s
#
.section .data
data_items:
.long 3,67,222,55,43,65,58,7,25,90,230,77,88,0
.section .text
.globl _start
_start:
movl $0, %edi
movl data_items(,%edi,4), %eax
movl %eax, %ebx
start_loop:
cmpl $0, %eax
je loop_exit
incl %edi
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax
jle start_loop
movl %eax, %ebx
jmp start_loop
loop_exit:
movl $1, %eax
int $0x80
as -o maximum.o maximum.s
ld -o maximum maximum.o
sun@sun-virtual-machine:~/assembly0x$ ./maximum
sun@sun-virtual-machine:~/assembly0x$ echo $?
230
objdump -d maximum.o
maximum.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_start>:
0: bf 00 00 00 00 mov $0x0,%edi
5: 67 8b 04 bd 00 00 00 mov 0x0(,%edi,4),%eax
c: 00
d: 89 c3 mov %eax,%ebx
000000000000000f <start_loop>:
f: 83 f8 00 cmp $0x0,%eax
12: 74 12 je 26 <loop_exit>
14: ff c7 inc %edi
16: 67 8b 04 bd 00 00 00 mov 0x0(,%edi,4),%eax
1d: 00
1e: 39 d8 cmp %ebx,%eax
20: 7e ed jle f <start_loop>
22: 89 c3 mov %eax,%ebx
24: eb e9 jmp f <start_loop>
0000000000000026 <loop_exit>:
26: b8 01 00 00 00 mov $0x1,%eax
2b: cd 80 int $0x80
objdump -d maximum
maximum: file format elf64-x86-64
Disassembly of section .text:
00000000004000b0 <_start>:
4000b0: bf 00 00 00 00 mov $0x0,%edi
4000b5: 67 8b 04 bd dd 00 60 mov 0x6000dd(,%edi,4),%eax
4000bc: 00
4000bd: 89 c3 mov %eax,%ebx
00000000004000bf <start_loop>:
4000bf: 83 f8 00 cmp $0x0,%eax
4000c2: 74 12 je 4000d6 <loop_exit>
4000c4: ff c7 inc %edi
4000c6: 67 8b 04 bd dd 00 60 mov 0x6000dd(,%edi,4),%eax
4000cd: 00
4000ce: 39 d8 cmp %ebx,%eax
4000d0: 7e ed jle 4000bf <start_loop>
4000d2: 89 c3 mov %eax,%ebx
4000d4: eb e9 jmp 4000bf <start_loop>
00000000004000d6 <loop_exit>:
4000d6: b8 01 00 00 00 mov $0x1,%eax
4000db: cd 80 int $0x80
- linux下使用
file
命令查看相应的文件格式
root@vultr:~# file /bin/bash
/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=04eca96c5bf3e9a300952a29ef3218f00487d37b, stripped
root@vultr:~# file /bin/which
/bin/which: POSIX shell script, ASCII text executable
root@vultr:~# file /bin/cp
/bin/cp: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=5a9675e90d7775415eb5e406b7d7602490ea8941, stripped
root@vultr:~# file /lib/x86_64-linux-gnu/ld-2.23.so
/lib/x86_64-linux-gnu/ld-2.23.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=c0adbad6f9a33944f2b3567c078ec472a1dae98e, stripped
root@vultr:~#
- 使用
ldd
命令来查看一个应用需要哪些依赖的动态库
很多现代应用都是通过动态编译链接的,当一个需要动态链接的应用被操作系统加载时,系统必须要定位然后加载它所需要的所有动态库文件。在Linux环境下,这项工作是由ld-linux.so.2来负责完成的,我们可以通过 ldd 命令来查看一个应用需要哪些依赖的动态库。
root@vultr:~# which ls
/bin/ls
root@vultr:~# ldd `which ls`
linux-vdso.so.1 => (0x00007ffd32346000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007fa694d39000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa69496f000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fa6946ff000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa6944fb000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa694f5b000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa6942de000)
vi 001.c
//"001.c"
#include<stdio.h>
int main(){
printf("Hello World!\n");
return 0;
}
gcc 001.c
root@vultr:~/clang# gcc 001.c
root@vultr:~/clang# ls
001.c a.out
root@vultr:~/clang# ./a.out
Hello World!
gcc -c 001.c
root@vultr:~/clang# gcc -c 001.c
root@vultr:~/clang# ls
001.c 001.o a.out
root@vultr:~/clang# file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=6e360439ae7f280e49023be8aa45440b57f44f40, not stripped
root@vultr:~/clang# file 001.c
001.c: C source, ASCII text
root@vultr:~/clang# file 001.o
001.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
用file命令看文件的格式,stripped和not stripped是什么意思?
表示符号表是否被清除.
root@vultr:~# man
What manual page do you want?
root@vultr:~# man strip
使用 ldd
命令查看应用需要哪些依赖的动态库:
root@vultr:~/clang# ldd 001.o
not a dynamic executable
root@vultr:~/clang# ldd a.out
linux-vdso.so.1 => (0x00007ffc35df8000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007efe43879000)
/lib64/ld-linux-x86-64.so.2 (0x00007efe43c43000)
- readelf命令和ELF文件详解
//"elf.c"
int printf(const char* format, ...);
int global_init_var = 84; //已初始化的全局变量
int global_uninit_var; //未初始化的全局变量
char *str1 = "hello world!"; //字符串常量
void func1(int i)
{
printf("%d\n", i);
}
int main(void)
{
static int static_var = 85; //已初始化的静态局部变量
static int static_var2; //未初始化的静态局部变量
char *str2 = "22222"; //字符串常量
int a = 1;
int b;
func1(static_var+static_var2+a+b);
return a;
}
Relevant Link:
LINUX下目标文件的BSS段、数据段、代码段-0n10rz1r0-ChinaUnix博客
readelf命令和ELF文件详解 - Be The Best! - CSDN博客