qemu guest-os中执行:
/ # xtest
xtest xtest-dy
/ # xtest-dy
-/bin/sh: xtest-dy: not found
/ # xtest
hello world.
[ 19.907787] xtest (1074) used greatest stack depth: 14048 bytes left
/ #
原因:带上static选项,qemu启动的busybox中才能执行,不然会显示命令找不到。
#gcc -o xtest -static test.c
# file xtest
test: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=c047b2086518d0231bf8abe1030293ba0b619de3, not stripped, too many notes (256)
# file xtest-dy
xtest: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=42baae3724e7e8bd2f70816e665842efb99d35ac, not stripped
- 下载代码: The PCI Utilities
- make SHARED=no ZLIB=no
//SHARED表示编译成静态库,ZLIB表示不编译zlib(应该是不使用pci.id)
- 修改顶层makefile,添加static
CFLAGS=$(OPT) -Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -static
- make
这个时候编译出来的还是dynamic
# make SHARED=no ZLIB=no
…
cc -O2 -Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -static -c -o common.o common.c
cc lspci.o ls-vpd.o ls-caps.o ls-caps-vendor.o ls-ecaps.o ls-kernel.o ls-tree.o ls-map.o common.o lib/libpci.a -lresolv -o lspci //这一行是链接生成lspci的命令
cc -O2 -Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -static -c -o setpci.o setpci.c
cc setpci.o common.o lib/libpci.a -lresolv -o setpci
…
# file lspci
lspci: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=7c84ad414e41b2c1735a877a0178f5ba195b3699, not stripped
- 手动重新生成静态lspci
# gcc lspci.o ls-vpd.o ls-caps.o ls-caps-vendor.o ls-ecaps.o ls-kernel.o ls-tree.o ls-map.o common.o lib/libpci.a -lresolv -static -o lspci
# file lspci
lspci: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=1ae7613223d0f602d9ddb1b5939ec799113bffab, not stripped, too many notes (256)
- 这个文件拷贝到busybox中,可以直接使用(好像没有pci.ids)
qemu的guest OS中执行例子:
# xlspci -vvvv -s 00:03.0 // 故意重命名成xlspci,与busybox中原始的lspci区分
00:03.0 Class 0200: Device 8086:100e (rev 03)
Subsystem: Device 1af4:1100
Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR+ FastB2B- DisINTx-
Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx-
Latency: 0
Interrupt: pin A routed to IRQ 11
Region 0: Memory at febc0000 (32-bit, non-prefetchable) [size=128K]
Region 1: I/O ports at c000 [size=64]
Expansion ROM at feb80000 [disabled] [size=256K]
Kernel driver in use: e1000