Linux中验证CPU架构模式及变量类型段的查找
readelf -S 可执行文件 //查看段
vi 进入vi编辑器,三种模式:
命令模式
输入模式:底部有-insert-,按ESC回到命令模式
末行模式:在命令模式中,输入:进入末行模式。
:w 文件名 ---保存文件
:q! ---退出vi编辑器
:wq 文件名---保存强行退出
-bss 未初始化数据段
-data 可读数据段
-rodata 只读数据段
/*验证用户名与密码程序*/
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
if(argc!=3){
printf("Usage: %s Username Password \n",argv[0]);
return -1;
}
if((strcmp(argv[1],"farsight")==0)&&(strcmp(argv[2],"1")==0)){
printf("login success\n");
return 0;
}else{
printf("login failed\n");
return -1;
}
return 0;
}
///////////////////////////////////////////////////////////////////
/*查看C中变量类型的段*/
farsight@ubuntu:~$ cd /home/farsight/dir
farsight@ubuntu:~/dir$ touch test4.c
farsight@ubuntu:~/dir$ gedit test4.c
farsight@ubuntu:~/dir$ gcc -o test4elf test4.c
farsight@ubuntu:~/dir$ sudo ./test4elf
[sudo] password for farsight:
&a=0xbfdcde14
&b=0x804a028
&c=0x804a024
&d=0x80485d0
p=0xbfdcde18
m=0xbfdcde1c
farsight@ubuntu:~/dir$ readelf -S test4elf
There are 30 section headers, starting at offset 0x1144:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg
Lk Inf Al
[ 0] NULL 00000000 000000 000000 00
0 0 0
[ 1] .interp PROGBITS 08048154 000154 000013 00 A
0 0 1
[ 2] .note.ABI-tag NOTE 08048168 000168 000020 00 A
0 0 4
[ 3] .note.gnu.build-i NOTE 08048188 000188 000024 00 A
0 0 4
[ 4] .gnu.hash GNU_HASH 080481ac 0001ac 000020 04 A
5 0 4
[ 5] .dynsym DYNSYM 080481cc 0001cc 000070 10 A
6 1 4
[ 6] .dynstr STRTAB 0804823c 00023c 000058 00 A
0 0 1
[ 7] .gnu.version VERSYM 08048294 000294 00000e 02 A
5 0 2
[ 8] .gnu.version_r VERNEED 080482a4 0002a4 000020 00 A
6 1 4
[ 9] .rel.dyn REL 080482c4 0002c4 000008 08 A
5 0 4
[10] .rel.plt REL 080482cc 0002cc 000028 08 A
5 12 4
[11] .init PROGBITS 080482f4 0002f4 00002e 00 AX
0 0 4
[12] .plt PROGBITS 08048330 000330 000060 04 AX
0 0 16
[13] .text PROGBITS 08048390 000390 00021c 00 AX
0 0 16
[14] .fini PROGBITS 080485ac 0005ac 00001a 00 AX
0 0 4
[15] .rodata PROGBITS 080485c8 0005c8 000038 00 A
0 0 4
[16] .eh_frame_hdr PROGBITS 08048600 000600 000034 00 A
0 0 4
[17] .eh_frame PROGBITS 08048634 000634 0000c4 00 A
0 0 4
[18] .ctors PROGBITS 08049f14 000f14 000008 00 WA
0 0 4
[19] .dtors PROGBITS 08049f1c 000f1c 000008 00 WA
0 0 4
[20] .jcr PROGBITS 08049f24 000f24 000004 00 WA
0 0 4
[21] .dynamic DYNAMIC 08049f28 000f28 0000c8 08 WA
6 0 4
[22] .got PROGBITS 08049ff0 000ff0 000004 04 WA
0 0 4
[23] .got.plt PROGBITS 08049ff4 000ff4 000020 04 WA
0 0 4
[24] .data PROGBITS 0804a014 001014 000008 00 WA
0 0 4
[25] .bss NOBITS 0804a01c 00101c 000010 00 WA
0 0 4
[26] .comment PROGBITS 00000000 00101c 00002a 01 MS
0 0 1
[27] .shstrtab STRTAB 00000000 001046 0000fc 00
0 0 1
[28] .symtab SYMTAB 00000000 0015f4 000460 10
29 45 4
[29] .strtab STRTAB 00000000 001a54 000225 00
0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor
specific)
farsight@ubuntu:~/dir$
CPU架构模式:
小端:低字节存放至低地址
大端:低字节存放至高地址
x86CPU是小端模式
/*判断计算机CPU的架构模式*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char argv[])
{
int i;
int a=0x12345678;
char *p=(char*)&a;
if(*p==0x78)
printf("This is a little-endianness cup\n");
else
printf("This is a big-endianness cup\n");
return 0;
}
大部分的笔记本的cpu都是小端模式
vi 进入vi编辑器,三种模式:
命令模式
输入模式:底部有-insert-,按ESC回到命令模式
末行模式:在命令模式中,输入:进入末行模式。
:w 文件名 ---保存文件
:q! ---退出vi编辑器
:wq 文件名---保存强行退出
-bss 未初始化数据段
-data 可读数据段
-rodata 只读数据段
/*验证用户名与密码程序*/
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
if(argc!=3){
printf("Usage: %s Username Password \n",argv[0]);
return -1;
}
if((strcmp(argv[1],"farsight")==0)&&(strcmp(argv[2],"1")==0)){
printf("login success\n");
return 0;
}else{
printf("login failed\n");
return -1;
}
return 0;
}
///////////////////////////////////////////////////////////////////
/*查看C中变量类型的段*/
farsight@ubuntu:~$ cd /home/farsight/dir
farsight@ubuntu:~/dir$ touch test4.c
farsight@ubuntu:~/dir$ gedit test4.c
farsight@ubuntu:~/dir$ gcc -o test4elf test4.c
farsight@ubuntu:~/dir$ sudo ./test4elf
[sudo] password for farsight:
&a=0xbfdcde14
&b=0x804a028
&c=0x804a024
&d=0x80485d0
p=0xbfdcde18
m=0xbfdcde1c
farsight@ubuntu:~/dir$ readelf -S test4elf
There are 30 section headers, starting at offset 0x1144:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg
Lk Inf Al
[ 0] NULL 00000000 000000 000000 00
0 0 0
[ 1] .interp PROGBITS 08048154 000154 000013 00 A
0 0 1
[ 2] .note.ABI-tag NOTE 08048168 000168 000020 00 A
0 0 4
[ 3] .note.gnu.build-i NOTE 08048188 000188 000024 00 A
0 0 4
[ 4] .gnu.hash GNU_HASH 080481ac 0001ac 000020 04 A
5 0 4
[ 5] .dynsym DYNSYM 080481cc 0001cc 000070 10 A
6 1 4
[ 6] .dynstr STRTAB 0804823c 00023c 000058 00 A
0 0 1
[ 7] .gnu.version VERSYM 08048294 000294 00000e 02 A
5 0 2
[ 8] .gnu.version_r VERNEED 080482a4 0002a4 000020 00 A
6 1 4
[ 9] .rel.dyn REL 080482c4 0002c4 000008 08 A
5 0 4
[10] .rel.plt REL 080482cc 0002cc 000028 08 A
5 12 4
[11] .init PROGBITS 080482f4 0002f4 00002e 00 AX
0 0 4
[12] .plt PROGBITS 08048330 000330 000060 04 AX
0 0 16
[13] .text PROGBITS 08048390 000390 00021c 00 AX
0 0 16
[14] .fini PROGBITS 080485ac 0005ac 00001a 00 AX
0 0 4
[15] .rodata PROGBITS 080485c8 0005c8 000038 00 A
0 0 4
[16] .eh_frame_hdr PROGBITS 08048600 000600 000034 00 A
0 0 4
[17] .eh_frame PROGBITS 08048634 000634 0000c4 00 A
0 0 4
[18] .ctors PROGBITS 08049f14 000f14 000008 00 WA
0 0 4
[19] .dtors PROGBITS 08049f1c 000f1c 000008 00 WA
0 0 4
[20] .jcr PROGBITS 08049f24 000f24 000004 00 WA
0 0 4
[21] .dynamic DYNAMIC 08049f28 000f28 0000c8 08 WA
6 0 4
[22] .got PROGBITS 08049ff0 000ff0 000004 04 WA
0 0 4
[23] .got.plt PROGBITS 08049ff4 000ff4 000020 04 WA
0 0 4
[24] .data PROGBITS 0804a014 001014 000008 00 WA
0 0 4
[25] .bss NOBITS 0804a01c 00101c 000010 00 WA
0 0 4
[26] .comment PROGBITS 00000000 00101c 00002a 01 MS
0 0 1
[27] .shstrtab STRTAB 00000000 001046 0000fc 00
0 0 1
[28] .symtab SYMTAB 00000000 0015f4 000460 10
29 45 4
[29] .strtab STRTAB 00000000 001a54 000225 00
0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor
specific)
farsight@ubuntu:~/dir$
CPU架构模式:
小端:低字节存放至低地址
大端:低字节存放至高地址
x86CPU是小端模式
/*判断计算机CPU的架构模式*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char argv[])
{
int i;
int a=0x12345678;
char *p=(char*)&a;
if(*p==0x78)
printf("This is a little-endianness cup\n");
else
printf("This is a big-endianness cup\n");
return 0;
}
大部分的笔记本的cpu都是小端模式