1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /* hello.c */ #include <stdio.h> #include <string.h> #define __copy_user(to,from,size) \ do { \ int __d0, __d1; \ __asm__ __volatile__( \ "0: rep; movsl\n" \ " movl %3,%0\n" \ "1: rep; movsb\n" \ "2:\n" \ ".section .fixup,\"ax\"\n" \ "3: lea 0(%3,%0,4),%0\n" \ " jmp 2b\n" \ ".previous\n" \ ".section __ex_table,\"a\"\n" \ " .align 4\n" \ " .long 0b,3b\n" \ " .long 1b,2b\n" \ ".previous" \ : "=&c"(size), "=&D" (__d0), "=&S" (__d1) \ : "r"(size & 3), "0"(size / 4), "1"(to), "2"(from) \ : "memory"); \ } while (0) int main(void) { const char *string = "Hello, world!"; char buf[20]; unsigned long n, m; m = n = strlen(string); __copy_user(buf, string, n); buf[m] = '\0'; printf("%s\n", buf); exit(0); } |
1 2 3 | $ gcc hello.c -o hello $ ./hello Hello, world! |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $ gcc -S hello.c /* hello.s */ movl -60(%ebp), %eax andl $3, %eax movl -60(%ebp), %edx movl %edx, %ecx shrl $2, %ecx leal -56(%ebp), %edi movl -12(%ebp), %esi #APP 0: rep; movsl movl %eax,%ecx 1: rep; movsb 2: .section .fixup,"ax" 3: lea 0(%eax,%ecx,4),%ecx jmp 2b .previous .section __ex_table,"a" .align 4 .long 0b,3b .long 1b,2b .previous #NO_APP movl %ecx, %eax |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | $ objdump --section-headers hello hello: file format elf32-i386 Sections: Idx Name Size VMA LMA File off Algn 0 .interp 00000013 080480f4 080480f4 000000f4 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA ……………………………… 9 .init 00000018 080482e0 080482e0 000002e0 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 10 .plt 00000070 080482f8 080482f8 000002f8 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 11 .text 000001c0 08048370 08048370 00000370 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE 12 .fixup 00000009 08048530 08048530 00000530 2**0 CONTENTS, ALLOC, LOAD, READONLY, CODE 13 .fini 0000001e 0804853c 0804853c 0000053c 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 14 .rodata 00000019 0804855c 0804855c 0000055c 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 15 __ex_table 00000010 08048578 08048578 00000578 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 16 .data 00000010 08049588 08049588 00000588 2**2 CONTENTS, ALLOC, LOAD, DATA CONTENTS, READONLY ……………………………… 26 .note 00000078 00000000 00000000 0000290d 2**0 CONTENTS, READONLY |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |