当C程式所依赖的汇编函数来自多个目标文件时,可以先将这些目标文件编译成静态库的形式,这样创建可执行文件时,就只需向编译器提供静态库文件名即可,编译器会自动从该库里将依赖的汇编目标文件提取出来...
|
|
ar [-]p[mod [relpos] [count]] archive [member...] |
libx.a |
# square.s - An example of a function that returns an integer value
.type square, @function
.globl square
square:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
imull %eax, %eax
movl %ebp, %esp
popl %ebp
ret
|
# areafunc.s - An example of a floating point return value
.section .text
.type areafunc, @function
.globl areafunc
areafunc:
pushl %ebp
movl %esp, %ebp
fldpi
filds 8(%ebp)
fmul %st(0), %st(0)
fmul %st(1), %st(0)
movl %ebp, %esp
popl %ebp
ret
|
# cpuidfunc.s - An example of returning a string value
.section .bss
.comm output, 13
.section .text
.type cpuidfunc, @function
.globl cpuidfunc
cpuidfunc:
pushl %ebp
movl %esp, %ebp
pushl %ebx
movl $0, %eax
cpuid
movl $output, %edi
movl %ebx, (%edi)
movl %edx, 4(%edi)
movl %ecx, 8(%edi)
movl $output, %eax
popl %ebx
movl %ebp, %esp
popl %ebp
ret
|
/* externtest.cpp - An example of using assembly language functions with C++ */ #include <iostream> using namespace std; extern "C" { int square(int); float areafunc(int); char *cpuidfunc(void); } int main() { int radius = 10; int radsquare = square(radius); cout << "The radius squared is "<< radsquare << endl; float result; result = areafunc(radius); cout << "The area is " << result << endl; cout << "The CPUID is " << cpuidfunc() << endl; return 0; } |
$ as -o square.o square.s $ as -o areafunc.o areafunc.s $ as -o cpuidfunc.o cpuidfunc.s $ g++ -o externtest externtest.cpp square.o areafunc.o cpuidfunc.o $ ./externtest The radius squared is 100 The area is 314.159 The CPUID is GenuineIntel $ |
$ as -o square.o square.s $ as -o areafunc.o areafunc.s $ as -o cpuidfunc.o cpuidfunc.s $ ar r libchap14.a square.o areafunc.o cpuidfunc.o ar: creating libchap14.a $ g++ -o externtest externtest.cpp libchap14.a $ ./externtest The radius squared is 100 The area is 314.159 The CPUID is GenuineIntel $ |
$ ar t libchap14.a square.o areafunc.o cpuidfunc.o $ ar d libchap14.a square.o $ ar t libchap14.a areafunc.o cpuidfunc.o $ |
$ ar t libchap14.a areafunc.o cpuidfunc.o $ ar r libchap14.a square.o $ ar t libchap14.a areafunc.o cpuidfunc.o square.o $ ar m libchap14.a square.o cpuidfunc.o areafunc.o $ ar t libchap14.a square.o cpuidfunc.o areafunc.o $ |
$ ar q libchap14.a square.o $ ar t libchap14.a square.o cpuidfunc.o areafunc.o square.o $ |
$ ar x libchap14.a square.o $ ls libchap14.a square.o $ ar x libchap14.a $ ls areafunc.o cpuidfunc.o libchap14.a square.o $ |
$ ar t libchap14.a cpuidfunc.o areafunc.o square.o $ ar ra cpuidfunc.o libchap14.a testfunc.o $ ar t libchap14.a cpuidfunc.o testfunc.o areafunc.o square.o $ |
$ ar tv libchap14.a rw-r--r-- 0/0 588 May 3 20:08 2014 cpuidfunc.o rw-r--r-- 0/0 482 May 3 20:57 2014 testfunc.o rw-r--r-- 0/0 482 May 3 20:08 2014 areafunc.o rw-r--r-- 0/0 480 May 3 20:08 2014 square.o $ ar s libchap14.a $ ranlib libchap14.a $ nm -s libchap14.a Archive index: output in cpuidfunc.o cpuidfunc in cpuidfunc.o testfunc in testfunc.o areafunc in areafunc.o square in square.o cpuidfunc.o: 00000000 T cpuidfunc 0000000d C output testfunc.o: 00000000 T testfunc areafunc.o: 00000000 T areafunc square.o: 00000000 T square $ |
libx.so |
$ gcc -shared -o libchap14.so square.o areafunc.o cpuidfunc.o $ ls libchap14.so libchap14.so* $ |
$ g++ -o externtest externtest.cpp -lchap14 /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../i486-slackware-linux/bin/ld: cannot find -lchap14 collect2: ld 返回 1 $ |
$ g++ -print-search-dirs ...................................... ...................................... 库:=/usr/lib/gcc/i486-slackware-linux/4.5.2/:/usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../i486-slackware-linux/lib/i486-slackware-linux/4.5.2/:/usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../i486-slackware-linux/lib/:/usr/lib/gcc/i486-slackware-linux/4.5.2/../../../i486-slackware-linux/4.5.2/:/usr/lib/gcc/i486-slackware-linux/4.5.2/../../../:/lib/i486-slackware-linux/4.5.2/:/lib/:/usr/lib/i486-slackware-linux/4.5.2/:/usr/lib/ $ |
# cp libchap14.so /usr/lib/ # g++ -o externtest externtest.cpp -lchap14 # ls -l externtest -rwxr-xr-x 1 root root 8057 5月 4 20:15 externtest* # |
$ g++ -o externtest externtest.cpp -L. -lchap14 $ ls -l externtest -rwxr-xr-x 1 zengl zengl 8057 5月 4 20:15 externtest* $ |
$ ./externtest ./externtest: error while loading shared libraries: libchap14.so: cannot open shared object file: No such file or directory $ ldd externtest linux-gate.so.1 => (0xffffe000) libchap14.so => not found libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7752000) libm.so.6 => /lib/libm.so.6 (0xb772c000) libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0xb7710000) libc.so.6 => /lib/libc.so.6 (0xb75ac000) /lib/ld-linux.so.2 (0xb7864000) $ |
$ LD_DEBUG=libs ./externtest 2326: find library=libchap14.so [0]; searching 2326: search cache=/etc/ld.so.cache 2326: trying file=/usr/local/mylib/test/libchap14.so 2326: search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path) 2326: trying file=/lib/tls/i686/sse2/libchap14.so 2326: trying file=/lib/tls/i686/libchap14.so 2326: trying file=/lib/tls/sse2/libchap14.so 2326: trying file=/lib/tls/libchap14.so 2326: trying file=/lib/i686/sse2/libchap14.so 2326: trying file=/lib/i686/libchap14.so 2326: trying file=/lib/sse2/libchap14.so 2326: trying file=/lib/libchap14.so 2326: trying file=/usr/lib/tls/i686/sse2/libchap14.so 2326: trying file=/usr/lib/tls/i686/libchap14.so 2326: trying file=/usr/lib/tls/sse2/libchap14.so 2326: trying file=/usr/lib/tls/libchap14.so 2326: trying file=/usr/lib/i686/sse2/libchap14.so 2326: trying file=/usr/lib/i686/libchap14.so 2326: trying file=/usr/lib/sse2/libchap14.so 2326: trying file=/usr/lib/libchap14.so 2326: ./externtest: error while loading shared libraries: libchap14.so: cannot open shared object file: No such file or directory $ |
$ LD_LIBRARY_PATH=. ./externtest The radius squared is 100 The area is 314.159 The CPUID is GenuineIntel $ |
$ export LD_LIBRARY_PATH=. $ ./externtest The radius squared is 100 The area is 314.159 The CPUID is GenuineIntel $ |
# vim /etc/ld.so.conf # cat /etc/ld.so.conf /usr/local/lib /usr/i486-slackware-linux/lib /usr/lib/seamonkey /home/zengl/asm_example/asmfunc # ./externtest ./externtest: error while loading shared libraries: libchap14.so: cannot open shared object file: No such file or directory # ldconfig # ./externtest The radius squared is 100 The area is 314.159 The CPUID is GenuineIntel # |
$ gcc -shared -o libchap14.so.1.0.1 square.o areafunc.o cpuidfunc.o $ ln -s libchap14.so.1.0.1 libchap14.so $ ls -l .................................... .................................... lrwxrwxrwx 1 zengl zengl 18 5月 4 21:07 libchap14.so -> libchap14.so.1.0.1* -rwxr-xr-x 1 zengl zengl 3900 5月 4 21:07 libchap14.so.1.0.1* .................................... .................................... $ g++ -o externtest externtest.cpp -L. -lchap14 $ LD_LIBRARY_PATH=. ldd externtest linux-gate.so.1 => (0xffffe000) libchap14.so => ./libchap14.so (0xb7816000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7705000) libm.so.6 => /lib/libm.so.6 (0xb76df000) libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0xb76c3000) libc.so.6 => /lib/libc.so.6 (0xb755f000) /lib/ld-linux.so.2 (0xb7819000) $ |
$ gcc -shared -Wl,-soname,libchap14.so.1.0.1 -o libchap14.so.1.0.1 square.o areafunc.o cpuidfunc.o $ readelf -d libchap14.so.1.0.1 Dynamic section at offset 0x4bc contains 22 entries: Tag Type Name/Value 0x00000001 (NEEDED) Shared library: [libc.so.6] 0x0000000e (SONAME) Library soname: [libchap14.so.1.0.1] ......................................... ......................................... $ g++ -o externtest externtest.cpp -L. -lchap14 $ LD_LIBRARY_PATH=. ldd externtest linux-gate.so.1 => (0xffffe000) libchap14.so.1.0.1 => ./libchap14.so.1.0.1 (0xb77dd000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb76cc000) libm.so.6 => /lib/libm.so.6 (0xb76a6000) libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0xb768a000) libc.so.6 => /lib/libc.so.6 (0xb7526000) /lib/ld-linux.so.2 (0xb77e0000) $ readelf -d externtest Dynamic section at offset 0xb20 contains 24 entries: Tag Type Name/Value 0x00000001 (NEEDED) Shared library: [libchap14.so.1.0.1] 0x00000001 (NEEDED) Shared library: [libstdc++.so.6] 0x00000001 (NEEDED) Shared library: [libm.so.6] 0x00000001 (NEEDED) Shared library: [libgcc_s.so.1] 0x00000001 (NEEDED) Shared library: [libc.so.6] ......................................... ......................................... $ LD_LIBRARY_PATH=. LD_DEBUG=libs ./externtest 2379: find library=libchap14.so.1.0.1 [0]; searching .............................. .............................. 2379: search cache=/etc/ld.so.cache 2379: trying file=./libchap14.so.1.0.1 2379: 2379: find library=libstdc++.so.6 [0]; searching .............................. .............................. 2379: search cache=/etc/ld.so.cache 2379: trying file=/usr/lib/libstdc++.so.6 2379: 2379: find library=libc.so.6 [0]; searching .............................. .............................. 2379: search cache=/etc/ld.so.cache 2379: trying file=/lib/libc.so.6 2379: .............................. .............................. The radius squared is 100 The area is 314.159 The CPUID is GenuineIntel .............................. .............................. $ |
$ gcc -shared -Wl,-soname,libchap14.so.1 -o libchap14.so.1.0.1 square.o areafunc.o cpuidfunc.o $ readelf -d libchap14.so.1.0.1 Dynamic section at offset 0x4bc contains 22 entries: Tag Type Name/Value 0x00000001 (NEEDED) Shared library: [libc.so.6] 0x0000000e (SONAME) Library soname: [libchap14.so.1] ......................................... ......................................... $ ln -s libchap14.so.1.0.1 libchap14.so.1 $ rm libchap14.so -v 已删除"libchap14.so" $ ln -s libchap14.so.1.0.1 libchap14.so $ ls -l 总用量 152 ......................................... ......................................... lrwxrwxrwx 1 root root 18 5月 4 21:07 libchap14.so -> libchap14.so.1.0.1* lrwxrwxrwx 1 root root 18 5月 4 23:08 libchap14.so.1 -> libchap14.so.1.0.1* -rwxr-xr-x 1 root root 3924 5月 4 23:08 libchap14.so.1.0.1* ......................................... ......................................... $ g++ -o externforV1 externtest.cpp -L. -lchap14 $ LD_LIBRARY_PATH=. ldd externforV1 linux-gate.so.1 => (0xffffe000) libchap14.so.1 => ./libchap14.so.1 (0xb76e8000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb75d7000) libm.so.6 => /lib/libm.so.6 (0xb75b1000) libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0xb7595000) libc.so.6 => /lib/libc.so.6 (0xb7431000) /lib/ld-linux.so.2 (0xb76eb000) $ LD_LIBRARY_PATH=. ./externforV1 The radius squared is 100 The area is 314.159 The CPUID is GenuineIntel $ gcc -shared -Wl,-soname,libchap14.so.2 -o libchap14.so.2.0.1 square.o areafunc.o cpuidfunc.o $ ln -s libchap14.so.2.0.1 libchap14.so.2 $ rm libchap14.so -v 已删除"libchap14.so" $ ln -s libchap14.so.2.0.1 libchap14.so $ ls -l 总用量 164 ......................................... ......................................... lrwxrwxrwx 1 root root 18 5月 4 23:12 libchap14.so -> libchap14.so.2.0.1* lrwxrwxrwx 1 root root 18 5月 4 23:08 libchap14.so.1 -> libchap14.so.1.0.1* -rwxr-xr-x 1 root root 3924 5月 4 23:08 libchap14.so.1.0.1* lrwxrwxrwx 1 root root 18 5月 4 23:11 libchap14.so.2 -> libchap14.so.2.0.1* -rwxr-xr-x 1 root root 3924 5月 4 23:11 libchap14.so.2.0.1* ......................................... ......................................... $ g++ -o externforV2 externtest.cpp -L. -lchap14 $ LD_LIBRARY_PATH=. ldd externforV2 linux-gate.so.1 => (0xffffe000) libchap14.so.2 => ./libchap14.so.2 (0xb78cb000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb77ba000) libm.so.6 => /lib/libm.so.6 (0xb7794000) libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0xb7778000) libc.so.6 => /lib/libc.so.6 (0xb7614000) /lib/ld-linux.so.2 (0xb78ce000) $ LD_LIBRARY_PATH=. ./externforV2 The radius squared is 100 The area is 314.159 The CPUID is GenuineIntel $ |
# ls -l /lib/libc.so.6 lrwxrwxrwx 1 root root 12 8月 10 2013 /lib/libc.so.6 -> libc-2.13.so* # readelf -d /lib/libc-2.13.so Dynamic section at offset 0x15dd7c contains 26 entries: Tag Type Name/Value 0x00000001 (NEEDED) Shared library: [ld-linux.so.2] 0x0000000e (SONAME) Library soname: [libc.so.6] |
# square.s - An example of a function that returns an integer value
.type square, @function
.globl square
square:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
imull %eax, %eax
movl %ebp, %esp
popl %ebp
ret
|
/* inttest.c - An example of returning an integer value */
#include <stdio.h>
int main()
{
int i = 2;
int j = square(i);
printf("The square of %d is %d\n", i, j);
j = square(10);
printf("The square of 10 is %d\n", j);
return 0;
}
|
$ gcc -gstabs -o inttest inttest.c square.s $ ./inttest The square of 2 is 4 The square of 10 is 100 $ |
$ gdb -q inttest Reading symbols from /root/asm_example/asmfunc/inttest...done. (gdb) l 1 /* inttest.c - An example of returning an integer value */ 2 #include <stdio.h> 3 4 int main() 5 { 6 int i = 2; 7 int j = square(i); 8 printf("The square of %d is %d\n", i, j); 9 10 j = square(10); (gdb) l 11 printf("The square of 10 is %d\n", j); 12 return 0; 13 } 14 (gdb) |
(gdb) break *main Breakpoint 1 at 0x8048384: file inttest.c, line 5. (gdb) info b Num Type Disp Enb Address What 1 breakpoint keep y 0x08048384 in main at inttest.c:5 (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x08048384 in main at inttest.c:5 (gdb) |
(gdb) run Starting program: /root/asm_example/asmfunc/inttest Breakpoint 1, main () at inttest.c:5 5 { (gdb) s main () at inttest.c:6 6 int i = 2; (gdb) s 7 int j = square(i); (gdb) s square () at square.s:5 5 pushl %ebp (gdb) s 6 movl %esp, %ebp (gdb) finish Run till exit from #0 square () at square.s:6 0x080483a7 in main () at inttest.c:7 7 int j = square(i); (gdb) s 8 printf("The square of %d is %d\n", i, j); (gdb) print j $2 = 4 (gdb) c Continuing. The square of 2 is 4 The square of 10 is 100 Program exited normally. (gdb) q $ |
(gdb) run Starting program: /root/asm_example/asmfunc/inttest Breakpoint 1, main () at inttest.c:5 5 { (gdb) s main () at inttest.c:6 6 int i = 2; (gdb) s 7 int j = square(i); (gdb) n 8 printf("The square of %d is %d\n", i, j); (gdb) n The square of 2 is 4 10 j = square(10); (gdb) next 11 printf("The square of 10 is %d\n", j); (gdb) |