[原创]检测操作系统类型与硬件架构_VMware, Unix及操作系统讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  VMware, Unix及操作系统讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2457 | 回复: 0   主题: [原创]检测操作系统类型与硬件架构        下一篇 
wulcan
版主
等级:中校
经验:1795
发帖:124
精华:0
注册:2014-3-19
状态:离线
发送短消息息给wulcan 加好友    发送短消息息给wulcan 发消息
发表于: IP:您无权察看 2014-11-26 19:51:40 | [全部帖] [楼主帖] 楼主

在移植和处理兼容性问题时,就必须关心操作系统的类型以及硬件的架构,典型的应用是configure,要根据不同的系统和硬件做相应的优化工作。

大多数程序是通过读取/bin/sh的ABI来进行判断操作系统和相应的硬件架构。

怎么样查看某个程序(文件)所用的ABI和操作系统类型呢?

最常用的是用readelf,ELF格式基本上是目前的标准格式,所以大多数时候是准确的:

FreeBSD,64位系统

% readelf -h /bin/ls
ELF Header:
Magic: 7f 45 4c 46 02 01 01 09 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - FreeBSD
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x401d40
Start of program headers: 64 (bytes into file)
Start of section headers: 28416 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 8
Size of section headers: 64 (bytes)
Number of section headers: 28
Section header string table index: 27


Linux,32位系统:

# readelf -h /bin/ls
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x8049a30
Start of program headers: 52 (bytes into file)
Start of section headers: 92320 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 8
Size of section headers: 40 (bytes)
Number of section headers: 31
Section header string table index: 30


FreeBSD还有一个专用的命令,可以方便查看ABI信息:

% brandelf -l
known ELF types are: FreeBSD(9) Linux(3) Solaris(6) SVR4(0)


上面的命令列出支持的elf格式。

% brandelf /bin/ls
File '/bin/ls' is of brand 'FreeBSD' (9).


可以看出,当然的ls是FreeBSD的格式。

一个系统的objdump,会给出ABI和相应的硬件架构,用help子命令可以查看所支持的类型:

FreeBSD:
% objdump -f /bin/ls
/bin/ls: file format elf64-x86-64-freebsd
architecture: i386:x86-64, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x0000000000401d40
Linux:
# objdump -f /bin/ls
/bin/ls: file format elf32-i386
architecture: i386, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x08049a30


查看支持的架构:

hu@fw2:~ % objdump –help
.......
objdump: supported targets: elf64-x86-64-freebsd elf64-x86-64 efi-app-x86_64 elf32-i386-freebsd elf32-i386 efi-app-ia32 srec symbolsrec tekhex binary ihex
objdump: supported architectures: i386 i386:x86-64 i8086 i386:intel i386:x86-64:intel
The following i386/x86-64 specific disassembler options are supported for use
with the -M switch (multiple options should be separated by commas):
x86-64 Disassemble in 64bit mode
i386 Disassemble in 32bit mode
i8086 Disassemble in 16bit mode
att Display instruction in AT&T syntax
intel Display instruction in Intel syntax
addr64 Assume 64bit address size
addr32 Assume 32bit address size
addr16 Assume 16bit address size
data32 Assume 32bit data size
data16 Assume 16bit data size
suffix Always display instruction suffix in AT&T syntax


当然别忘了非常基础的file命令,这个信息虽然少一些,但能就会大多数的场合:

FreeBSD:
% file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 10.0 (1000510), stripped
Linux:
# file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped


请注意对比上面的两个命令结果,FreeBSD没有显示内核版本,因为内核版本跟发行版本一致。

该贴由hui.chen转至本版2014-11-27 18:24:24




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论