在移植和处理兼容性问题时,就必须关心操作系统的类型以及硬件的架构,典型的应用是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