一、源码编辑:
使用vim输入以下内容:hello.c
1 #include <stdio.h> 2 3 int main(int argc, char **argv) 4 { 5 printf("Hello world! "); 6 7 return 0; 8 }
使用hexdump工具查看内容,就是ascii内容为:共0x5f个字节
1 ~ � WORKM � studyCode � gnuC � hexdump -C hello.c 2 00000000 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e |#include <stdio.| 3 00000010 68 3e 0a 0a 69 6e 74 20 6d 61 69 6e 28 69 6e 74 |h>..int main(int| 4 00000020 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 | argc, char **ar| 5 00000030 67 76 29 0a 7b 0a 09 70 72 69 6e 74 66 28 22 48 |gv).{..printf("H| 6 00000040 65 6c 6c 6f 20 77 6f 72 6c 64 21 5c 6e 22 29 3b |ello world! ");| 7 00000050 0a 0a 09 72 65 74 75 72 6e 20 30 3b 0a 7d 0a |...return 0;.}.| 8 0000005f
二、源码的预处理:
gcc -E hello.c结果为:
1 ~ � WORKM � studyCode � gnuC � gcc -E hello.c 2 # 1 "hello.c" 3 # 1 "<built-in>" 4 # 1 "<命令行>" 5 # 31 "<命令行>" 6 # 1 "/usr/include/stdc-predef.h" 1 3 4 7 # 32 "<命令行>" 2 8 # 1 "hello.c" 9 # 1 "/usr/include/stdio.h" 1 3 4 10 # 27 "/usr/include/stdio.h" 3 4 11 # 1 "/usr/include/bits/libc-header-start.h" 1 3 4 12 # 33 "/usr/include/bits/libc-header-start.h" 3 4 13 # 1 "/usr/include/features.h" 1 3 4 14 # 465 "/usr/include/features.h" 3 4 15 # 1 "/usr/include/sys/cdefs.h" 1 3 4 16 # 452 "/usr/include/sys/cdefs.h" 3 4 17 # 1 "/usr/include/bits/wordsize.h" 1 3 4 18 # 453 "/usr/include/sys/cdefs.h" 2 3 4 19 # 1 "/usr/include/bits/long-double.h" 1 3 4 20 # 454 "/usr/include/sys/cdefs.h" 2 3 4 21 # 466 "/usr/include/features.h" 2 3 4 22 # 489 "/usr/include/features.h" 3 4 23 # 1 "/usr/include/gnu/stubs.h" 1 3 4 24 # 10 "/usr/include/gnu/stubs.h" 3 4 25 # 1 "/usr/include/gnu/stubs-64.h" 1 3 4 26 # 11 "/usr/include/gnu/stubs.h" 2 3 4 27 # 490 "/usr/include/features.h" 2 3 4 28 # 34 "/usr/include/bits/libc-header-start.h" 2 3 4 29 # 28 "/usr/include/stdio.h" 2 3 4 30 31 32 33 34 35 # 1 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stddef.h" 1 3 4 36 # 209 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stddef.h" 3 4 37 38 # 209 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stddef.h" 3 4 39 typedef long unsigned int size_t; 40 # 34 "/usr/include/stdio.h" 2 3 4 41 42 43 # 1 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stdarg.h" 1 3 4 44 # 40 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stdarg.h" 3 4 45 typedef __builtin_va_list __gnuc_va_list; 46 # 37 "/usr/include/stdio.h" 2 3 4 47 48 # 1 "/usr/include/bits/types.h" 1 3 4 49 # 27 "/usr/include/bits/types.h" 3 4 50 # 1 "/usr/include/bits/wordsize.h" 1 3 4 51 # 28 "/usr/include/bits/types.h" 2 3 4 52 # 1 "/usr/include/bits/timesize.h" 1 3 4 53 # 29 "/usr/include/bits/types.h" 2 3 4 54 55 56 typedef unsigned char __u_char; 57 typedef unsigned short int __u_short; 58 typedef unsigned int __u_int; 59 typedef unsigned long int __u_long; 60 61 62 typedef signed char __int8_t; 63 typedef unsigned char __uint8_t; 64 typedef signed short int __int16_t; 65 typedef unsigned short int __uint16_t; 66 typedef signed int __int32_t; 67 typedef unsigned int __uint32_t; 68 69 typedef signed long int __int64_t; 70 typedef unsigned long int __uint64_t; 71 72 73 74 75 76 77 typedef __int8_t __int_least8_t; 78 typedef __uint8_t __uint_least8_t; 79 typedef __int16_t __int_least16_t; 80 typedef __uint16_t __uint_least16_t; 81 typedef __int32_t __int_least32_t; 82 typedef __uint32_t __uint_least32_t; 83 typedef __int64_t __int_least64_t; 84 typedef __uint64_t __uint_least64_t; 85 86 87 88 typedef long int __quad_t; 89 typedef unsigned long int __u_quad_t; 90 91 92 93 94 95 96 97 typedef long int __intmax_t; 98 typedef unsigned long int __uintmax_t; 99 # 141 "/usr/include/bits/types.h" 3 4 100 # 1 "/usr/include/bits/typesizes.h" 1 3 4 101 # 142 "/usr/include/bits/types.h" 2 3 4 102 # 1 "/usr/include/bits/time64.h" 1 3 4 103 # 143 "/usr/include/bits/types.h" 2 3 4 104 105 106 typedef unsigned long int __dev_t; 107 typedef unsigned int __uid_t; 108 typedef unsigned int __gid_t; 109 typedef unsigned long int __ino_t; 110 typedef unsigned long int __ino64_t; 111 typedef unsigned int __mode_t; 112 typedef unsigned long int __nlink_t; 113 typedef long int __off_t; 114 typedef long int __off64_t; 115 typedef int __pid_t; 116 typedef struct { int __val[2]; } __fsid_t; 117 typedef long int __clock_t; 118 typedef unsigned long int __rlim_t; 119 typedef unsigned long int __rlim64_t; 120 typedef unsigned int __id_t; 121 typedef long int __time_t; 122 typedef unsigned int __useconds_t; 123 typedef long int __suseconds_t; 124 typedef long int __suseconds64_t; 125 126 typedef int __daddr_t; 127 typedef int __key_t; 128 129 130 typedef int __clockid_t; 131 132 133 typedef void * __timer_t; 134 135 136 typedef long int __blksize_t; 137 138 139 140 141 typedef long int __blkcnt_t; 142 typedef long int __blkcnt64_t; 143 144 145 typedef unsigned long int __fsblkcnt_t; 146 typedef unsigned long int __fsblkcnt64_t; 147 148 149 typedef unsigned long int __fsfilcnt_t; 150 typedef unsigned long int __fsfilcnt64_t; 151 152 153 typedef long int __fsword_t; 154 155 typedef long int __ssize_t; 156 157 158 typedef long int __syscall_slong_t; 159 160 typedef unsigned long int __syscall_ulong_t; 161 162 163 164 typedef __off64_t __loff_t; 165 typedef char *__caddr_t; 166 167 168 typedef long int __intptr_t; 169 170 171 typedef unsigned int __socklen_t; 172 173 174 175 176 typedef int __sig_atomic_t; 177 # 39 "/usr/include/stdio.h" 2 3 4 178 # 1 "/usr/include/bits/types/__fpos_t.h" 1 3 4 179 180 181 182 183 # 1 "/usr/include/bits/types/__mbstate_t.h" 1 3 4 184 # 13 "/usr/include/bits/types/__mbstate_t.h" 3 4 185 typedef struct 186 { 187 int __count; 188 union 189 { 190 unsigned int __wch; 191 char __wchb[4]; 192 } __value; 193 } __mbstate_t; 194 # 6 "/usr/include/bits/types/__fpos_t.h" 2 3 4 195 196 197 198 199 typedef struct _G_fpos_t 200 { 201 __off_t __pos; 202 __mbstate_t __state; 203 } __fpos_t; 204 # 40 "/usr/include/stdio.h" 2 3 4 205 # 1 "/usr/include/bits/types/__fpos64_t.h" 1 3 4 206 # 10 "/usr/include/bits/types/__fpos64_t.h" 3 4 207 typedef struct _G_fpos64_t 208 { 209 __off64_t __pos; 210 __mbstate_t __state; 211 } __fpos64_t; 212 # 41 "/usr/include/stdio.h" 2 3 4 213 # 1 "/usr/include/bits/types/__FILE.h" 1 3 4 214 215 216 217 struct _IO_FILE; 218 typedef struct _IO_FILE __FILE; 219 # 42 "/usr/include/stdio.h" 2 3 4 220 # 1 "/usr/include/bits/types/FILE.h" 1 3 4 221 222 223 224 struct _IO_FILE; 225 226 227 typedef struct _IO_FILE FILE; 228 # 43 "/usr/include/stdio.h" 2 3 4 229 # 1 "/usr/include/bits/types/struct_FILE.h" 1 3 4 230 # 35 "/usr/include/bits/types/struct_FILE.h" 3 4 231 struct _IO_FILE; 232 struct _IO_marker; 233 struct _IO_codecvt; 234 struct _IO_wide_data; 235 236 237 238 239 typedef void _IO_lock_t; 240 241 242 243 244 245 struct _IO_FILE 246 { 247 int _flags; 248 249 250 char *_IO_read_ptr; 251 char *_IO_read_end; 252 char *_IO_read_base; 253 char *_IO_write_base; 254 char *_IO_write_ptr; 255 char *_IO_write_end; 256 char *_IO_buf_base; 257 char *_IO_buf_end; 258 259 260 char *_IO_save_base; 261 char *_IO_backup_base; 262 char *_IO_save_end; 263 264 struct _IO_marker *_markers; 265 266 struct _IO_FILE *_chain; 267 268 int _fileno; 269 int _flags2; 270 __off_t _old_offset; 271 272 273 unsigned short _cur_column; 274 signed char _vtable_offset; 275 char _shortbuf[1]; 276 277 _IO_lock_t *_lock; 278 279 280 281 282 283 284 285 __off64_t _offset; 286 287 struct _IO_codecvt *_codecvt; 288 struct _IO_wide_data *_wide_data; 289 struct _IO_FILE *_freeres_list; 290 void *_freeres_buf; 291 size_t __pad5; 292 int _mode; 293 294 char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)]; 295 }; 296 # 44 "/usr/include/stdio.h" 2 3 4 297 # 52 "/usr/include/stdio.h" 3 4 298 typedef __gnuc_va_list va_list; 299 # 63 "/usr/include/stdio.h" 3 4 300 typedef __off_t off_t; 301 # 77 "/usr/include/stdio.h" 3 4 302 typedef __ssize_t ssize_t; 303 304 305 306 307 308 309 typedef __fpos_t fpos_t; 310 # 133 "/usr/include/stdio.h" 3 4 311 # 1 "/usr/include/bits/stdio_lim.h" 1 3 4 312 # 134 "/usr/include/stdio.h" 2 3 4 313 314 315 316 extern FILE *stdin; 317 extern FILE *stdout; 318 extern FILE *stderr; 319 320 321 322 323 324 325 extern int remove (const char *__filename) __attribute__ ((__nothrow__ , __leaf__)); 326 327 extern int rename (const char *__old, const char *__new) __attribute__ ((__nothrow__ , __leaf__)); 328 329 330 331 extern int renameat (int __oldfd, const char *__old, int __newfd, 332 const char *__new) __attribute__ ((__nothrow__ , __leaf__)); 333 # 173 "/usr/include/stdio.h" 3 4 334 extern FILE *tmpfile (void) ; 335 # 187 "/usr/include/stdio.h" 3 4 336 extern char *tmpnam (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ; 337 338 339 340 341 extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ; 342 # 204 "/usr/include/stdio.h" 3 4 343 extern char *tempnam (const char *__dir, const char *__pfx) 344 __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) ; 345 346 347 348 349 350 351 352 extern int fclose (FILE *__stream); 353 354 355 356 357 extern int fflush (FILE *__stream); 358 # 227 "/usr/include/stdio.h" 3 4 359 extern int fflush_unlocked (FILE *__stream); 360 # 246 "/usr/include/stdio.h" 3 4 361 extern FILE *fopen (const char *__restrict __filename, 362 const char *__restrict __modes) ; 363 364 365 366 367 extern FILE *freopen (const char *__restrict __filename, 368 const char *__restrict __modes, 369 FILE *__restrict __stream) ; 370 # 279 "/usr/include/stdio.h" 3 4 371 extern FILE *fdopen (int __fd, const char *__modes) __attribute__ ((__nothrow__ , __leaf__)) ; 372 # 292 "/usr/include/stdio.h" 3 4 373 extern FILE *fmemopen (void *__s, size_t __len, const char *__modes) 374 __attribute__ ((__nothrow__ , __leaf__)) ; 375 376 377 378 379 extern FILE *open_memstream (char **__bufloc, size_t *__sizeloc) __attribute__ ((__nothrow__ , __leaf__)) ; 380 381 382 383 384 385 extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __attribute__ ((__nothrow__ , __leaf__)); 386 387 388 389 extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf, 390 int __modes, size_t __n) __attribute__ ((__nothrow__ , __leaf__)); 391 392 393 394 395 extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf, 396 size_t __size) __attribute__ ((__nothrow__ , __leaf__)); 397 398 399 extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 400 401 402 403 404 405 406 407 extern int fprintf (FILE *__restrict __stream, 408 const char *__restrict __format, ...); 409 410 411 412 413 extern int printf (const char *__restrict __format, ...); 414 415 extern int sprintf (char *__restrict __s, 416 const char *__restrict __format, ...) __attribute__ ((__nothrow__)); 417 418 419 420 421 422 extern int vfprintf (FILE *__restrict __s, const char *__restrict __format, 423 __gnuc_va_list __arg); 424 425 426 427 428 extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg); 429 430 extern int vsprintf (char *__restrict __s, const char *__restrict __format, 431 __gnuc_va_list __arg) __attribute__ ((__nothrow__)); 432 433 434 435 extern int snprintf (char *__restrict __s, size_t __maxlen, 436 const char *__restrict __format, ...) 437 __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 4))); 438 439 extern int vsnprintf (char *__restrict __s, size_t __maxlen, 440 const char *__restrict __format, __gnuc_va_list __arg) 441 __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0))); 442 # 379 "/usr/include/stdio.h" 3 4 443 extern int vdprintf (int __fd, const char *__restrict __fmt, 444 __gnuc_va_list __arg) 445 __attribute__ ((__format__ (__printf__, 2, 0))); 446 extern int dprintf (int __fd, const char *__restrict __fmt, ...) 447 __attribute__ ((__format__ (__printf__, 2, 3))); 448 449 450 451 452 453 454 455 extern int fscanf (FILE *__restrict __stream, 456 const char *__restrict __format, ...) ; 457 458 459 460 461 extern int scanf (const char *__restrict __format, ...) ; 462 463 extern int sscanf (const char *__restrict __s, 464 const char *__restrict __format, ...) __attribute__ ((__nothrow__ , __leaf__)); 465 466 467 468 469 470 # 1 "/usr/include/bits/floatn.h" 1 3 4 471 # 119 "/usr/include/bits/floatn.h" 3 4 472 # 1 "/usr/include/bits/floatn-common.h" 1 3 4 473 # 24 "/usr/include/bits/floatn-common.h" 3 4 474 # 1 "/usr/include/bits/long-double.h" 1 3 4 475 # 25 "/usr/include/bits/floatn-common.h" 2 3 4 476 # 120 "/usr/include/bits/floatn.h" 2 3 4 477 # 407 "/usr/include/stdio.h" 2 3 4 478 479 480 481 extern int fscanf (FILE *__restrict __stream, const char *__restrict __format, ...) __asm__ ("" "__isoc99_fscanf") 482 483 ; 484 extern int scanf (const char *__restrict __format, ...) __asm__ ("" "__isoc99_scanf") 485 ; 486 extern int sscanf (const char *__restrict __s, const char *__restrict __format, ...) __asm__ ("" "__isoc99_sscanf") __attribute__ ((__nothrow__ , __leaf__)) 487 488 ; 489 # 435 "/usr/include/stdio.h" 3 4 490 extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, 491 __gnuc_va_list __arg) 492 __attribute__ ((__format__ (__scanf__, 2, 0))) ; 493 494 495 496 497 498 extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) 499 __attribute__ ((__format__ (__scanf__, 1, 0))) ; 500 501 502 extern int vsscanf (const char *__restrict __s, 503 const char *__restrict __format, __gnuc_va_list __arg) 504 __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__format__ (__scanf__, 2, 0))); 505 506 507 508 509 510 extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vfscanf") 511 512 513 514 __attribute__ ((__format__ (__scanf__, 2, 0))) ; 515 extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vscanf") 516 517 __attribute__ ((__format__ (__scanf__, 1, 0))) ; 518 extern int vsscanf (const char *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vsscanf") __attribute__ ((__nothrow__ , __leaf__)) 519 520 521 522 __attribute__ ((__format__ (__scanf__, 2, 0))); 523 # 489 "/usr/include/stdio.h" 3 4 524 extern int fgetc (FILE *__stream); 525 extern int getc (FILE *__stream); 526 527 528 529 530 531 extern int getchar (void); 532 533 534 535 536 537 538 extern int getc_unlocked (FILE *__stream); 539 extern int getchar_unlocked (void); 540 # 514 "/usr/include/stdio.h" 3 4 541 extern int fgetc_unlocked (FILE *__stream); 542 # 525 "/usr/include/stdio.h" 3 4 543 extern int fputc (int __c, FILE *__stream); 544 extern int putc (int __c, FILE *__stream); 545 546 547 548 549 550 extern int putchar (int __c); 551 # 541 "/usr/include/stdio.h" 3 4 552 extern int fputc_unlocked (int __c, FILE *__stream); 553 554 555 556 557 558 559 560 extern int putc_unlocked (int __c, FILE *__stream); 561 extern int putchar_unlocked (int __c); 562 563 564 565 566 567 568 extern int getw (FILE *__stream); 569 570 571 extern int putw (int __w, FILE *__stream); 572 573 574 575 576 577 578 579 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream) 580 __attribute__ ((__access__ (__write_only__, 1, 2))); 581 # 608 "/usr/include/stdio.h" 3 4 582 extern __ssize_t __getdelim (char **__restrict __lineptr, 583 size_t *__restrict __n, int __delimiter, 584 FILE *__restrict __stream) ; 585 extern __ssize_t getdelim (char **__restrict __lineptr, 586 size_t *__restrict __n, int __delimiter, 587 FILE *__restrict __stream) ; 588 589 590 591 592 593 594 595 extern __ssize_t getline (char **__restrict __lineptr, 596 size_t *__restrict __n, 597 FILE *__restrict __stream) ; 598 599 600 601 602 603 604 605 extern int fputs (const char *__restrict __s, FILE *__restrict __stream); 606 607 608 609 610 611 extern int puts (const char *__s); 612 613 614 615 616 617 618 extern int ungetc (int __c, FILE *__stream); 619 620 621 622 623 624 625 extern size_t fread (void *__restrict __ptr, size_t __size, 626 size_t __n, FILE *__restrict __stream) ; 627 628 629 630 631 extern size_t fwrite (const void *__restrict __ptr, size_t __size, 632 size_t __n, FILE *__restrict __s); 633 # 678 "/usr/include/stdio.h" 3 4 634 extern size_t fread_unlocked (void *__restrict __ptr, size_t __size, 635 size_t __n, FILE *__restrict __stream) ; 636 extern size_t fwrite_unlocked (const void *__restrict __ptr, size_t __size, 637 size_t __n, FILE *__restrict __stream); 638 639 640 641 642 643 644 645 extern int fseek (FILE *__stream, long int __off, int __whence); 646 647 648 649 650 extern long int ftell (FILE *__stream) ; 651 652 653 654 655 extern void rewind (FILE *__stream); 656 # 712 "/usr/include/stdio.h" 3 4 657 extern int fseeko (FILE *__stream, __off_t __off, int __whence); 658 659 660 661 662 extern __off_t ftello (FILE *__stream) ; 663 # 736 "/usr/include/stdio.h" 3 4 664 extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos); 665 666 667 668 669 extern int fsetpos (FILE *__stream, const fpos_t *__pos); 670 # 762 "/usr/include/stdio.h" 3 4 671 extern void clearerr (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 672 673 extern int feof (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 674 675 extern int ferror (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 676 677 678 679 extern void clearerr_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 680 extern int feof_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 681 extern int ferror_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 682 683 684 685 686 687 688 689 extern void perror (const char *__s); 690 691 692 693 694 extern int fileno (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 695 696 697 698 699 extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 700 # 799 "/usr/include/stdio.h" 3 4 701 extern FILE *popen (const char *__command, const char *__modes) ; 702 703 704 705 706 707 extern int pclose (FILE *__stream); 708 709 710 711 712 713 extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__)); 714 # 839 "/usr/include/stdio.h" 3 4 715 extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 716 717 718 719 extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 720 721 722 extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 723 # 857 "/usr/include/stdio.h" 3 4 724 extern int __uflow (FILE *); 725 extern int __overflow (FILE *, int); 726 # 874 "/usr/include/stdio.h" 3 4 727 728 # 2 "hello.c" 2 729 730 731 # 3 "hello.c" 732 int main(int argc, char **argv) 733 { 734 printf("Hello world! "); 735 736 return 0; 737 }
以上是直接输出到屏幕,也可以gcc -E hello.c -o hello.i,将预处理结果保存到文件hello.i文件,以便于后期查看(虽然并没有多大用途,但万一哪天需要了呢)
含有宏定义和条件编译的代码:直接用gcc -E hello1.c -o hello1.i
1 # 1 "hello1.c" 2 # 1 "<built-in>" 3 # 1 "<命令行>" 4 # 31 "<命令行>" 5 # 1 "/usr/include/stdc-predef.h" 1 3 4 6 # 32 "<命令行>" 2 7 # 1 "hello1.c" 8 # 1 "/usr/include/stdio.h" 1 3 4 9 # 27 "/usr/include/stdio.h" 3 4 10 # 1 "/usr/include/bits/libc-header-start.h" 1 3 4 11 # 33 "/usr/include/bits/libc-header-start.h" 3 4 12 # 1 "/usr/include/features.h" 1 3 4 13 # 465 "/usr/include/features.h" 3 4 14 # 1 "/usr/include/sys/cdefs.h" 1 3 4 15 # 452 "/usr/include/sys/cdefs.h" 3 4 16 # 1 "/usr/include/bits/wordsize.h" 1 3 4 17 # 453 "/usr/include/sys/cdefs.h" 2 3 4 18 # 1 "/usr/include/bits/long-double.h" 1 3 4 19 # 454 "/usr/include/sys/cdefs.h" 2 3 4 20 # 466 "/usr/include/features.h" 2 3 4 21 # 489 "/usr/include/features.h" 3 4 22 # 1 "/usr/include/gnu/stubs.h" 1 3 4 23 # 10 "/usr/include/gnu/stubs.h" 3 4 24 # 1 "/usr/include/gnu/stubs-64.h" 1 3 4 25 # 11 "/usr/include/gnu/stubs.h" 2 3 4 26 # 490 "/usr/include/features.h" 2 3 4 27 # 34 "/usr/include/bits/libc-header-start.h" 2 3 4 28 # 28 "/usr/include/stdio.h" 2 3 4 29 30 31 32 33 34 # 1 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stddef.h" 1 3 4 35 # 209 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stddef.h" 3 4 36 37 # 209 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stddef.h" 3 4 38 typedef long unsigned int size_t; 39 # 34 "/usr/include/stdio.h" 2 3 4 40 41 42 # 1 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stdarg.h" 1 3 4 43 # 40 "/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/stdarg.h" 3 4 44 typedef __builtin_va_list __gnuc_va_list; 45 # 37 "/usr/include/stdio.h" 2 3 4 46 47 # 1 "/usr/include/bits/types.h" 1 3 4 48 # 27 "/usr/include/bits/types.h" 3 4 49 # 1 "/usr/include/bits/wordsize.h" 1 3 4 50 # 28 "/usr/include/bits/types.h" 2 3 4 51 # 1 "/usr/include/bits/timesize.h" 1 3 4 52 # 29 "/usr/include/bits/types.h" 2 3 4 53 54 55 typedef unsigned char __u_char; 56 typedef unsigned short int __u_short; 57 typedef unsigned int __u_int; 58 typedef unsigned long int __u_long; 59 60 61 typedef signed char __int8_t; 62 typedef unsigned char __uint8_t; 63 typedef signed short int __int16_t; 64 typedef unsigned short int __uint16_t; 65 typedef signed int __int32_t; 66 typedef unsigned int __uint32_t; 67 68 typedef signed long int __int64_t; 69 typedef unsigned long int __uint64_t; 70 71 72 73 74 75 76 typedef __int8_t __int_least8_t; 77 typedef __uint8_t __uint_least8_t; 78 typedef __int16_t __int_least16_t; 79 typedef __uint16_t __uint_least16_t; 80 typedef __int32_t __int_least32_t; 81 typedef __uint32_t __uint_least32_t; 82 typedef __int64_t __int_least64_t; 83 typedef __uint64_t __uint_least64_t; 84 85 86 87 typedef long int __quad_t; 88 typedef unsigned long int __u_quad_t; 89 90 91 92 93 94 95 96 typedef long int __intmax_t; 97 typedef unsigned long int __uintmax_t; 98 # 141 "/usr/include/bits/types.h" 3 4 99 # 1 "/usr/include/bits/typesizes.h" 1 3 4 100 # 142 "/usr/include/bits/types.h" 2 3 4 101 # 1 "/usr/include/bits/time64.h" 1 3 4 102 # 143 "/usr/include/bits/types.h" 2 3 4 103 104 105 typedef unsigned long int __dev_t; 106 typedef unsigned int __uid_t; 107 typedef unsigned int __gid_t; 108 typedef unsigned long int __ino_t; 109 typedef unsigned long int __ino64_t; 110 typedef unsigned int __mode_t; 111 typedef unsigned long int __nlink_t; 112 typedef long int __off_t; 113 typedef long int __off64_t; 114 typedef int __pid_t; 115 typedef struct { int __val[2]; } __fsid_t; 116 typedef long int __clock_t; 117 typedef unsigned long int __rlim_t; 118 typedef unsigned long int __rlim64_t; 119 typedef unsigned int __id_t; 120 typedef long int __time_t; 121 typedef unsigned int __useconds_t; 122 typedef long int __suseconds_t; 123 typedef long int __suseconds64_t; 124 125 typedef int __daddr_t; 126 typedef int __key_t; 127 128 129 typedef int __clockid_t; 130 131 132 typedef void * __timer_t; 133 134 135 typedef long int __blksize_t; 136 137 138 139 140 typedef long int __blkcnt_t; 141 typedef long int __blkcnt64_t; 142 143 144 typedef unsigned long int __fsblkcnt_t; 145 typedef unsigned long int __fsblkcnt64_t; 146 147 148 typedef unsigned long int __fsfilcnt_t; 149 typedef unsigned long int __fsfilcnt64_t; 150 151 152 typedef long int __fsword_t; 153 154 typedef long int __ssize_t; 155 156 157 typedef long int __syscall_slong_t; 158 159 typedef unsigned long int __syscall_ulong_t; 160 161 162 163 typedef __off64_t __loff_t; 164 typedef char *__caddr_t; 165 166 167 typedef long int __intptr_t; 168 169 170 typedef unsigned int __socklen_t; 171 172 173 174 175 typedef int __sig_atomic_t; 176 # 39 "/usr/include/stdio.h" 2 3 4 177 # 1 "/usr/include/bits/types/__fpos_t.h" 1 3 4 178 179 180 181 182 # 1 "/usr/include/bits/types/__mbstate_t.h" 1 3 4 183 # 13 "/usr/include/bits/types/__mbstate_t.h" 3 4 184 typedef struct 185 { 186 int __count; 187 union 188 { 189 unsigned int __wch; 190 char __wchb[4]; 191 } __value; 192 } __mbstate_t; 193 # 6 "/usr/include/bits/types/__fpos_t.h" 2 3 4 194 195 196 197 198 typedef struct _G_fpos_t 199 { 200 __off_t __pos; 201 __mbstate_t __state; 202 } __fpos_t; 203 # 40 "/usr/include/stdio.h" 2 3 4 204 # 1 "/usr/include/bits/types/__fpos64_t.h" 1 3 4 205 # 10 "/usr/include/bits/types/__fpos64_t.h" 3 4 206 typedef struct _G_fpos64_t 207 { 208 __off64_t __pos; 209 __mbstate_t __state; 210 } __fpos64_t; 211 # 41 "/usr/include/stdio.h" 2 3 4 212 # 1 "/usr/include/bits/types/__FILE.h" 1 3 4 213 214 215 216 struct _IO_FILE; 217 typedef struct _IO_FILE __FILE; 218 # 42 "/usr/include/stdio.h" 2 3 4 219 # 1 "/usr/include/bits/types/FILE.h" 1 3 4 220 221 222 223 struct _IO_FILE; 224 225 226 typedef struct _IO_FILE FILE; 227 # 43 "/usr/include/stdio.h" 2 3 4 228 # 1 "/usr/include/bits/types/struct_FILE.h" 1 3 4 229 # 35 "/usr/include/bits/types/struct_FILE.h" 3 4 230 struct _IO_FILE; 231 struct _IO_marker; 232 struct _IO_codecvt; 233 struct _IO_wide_data; 234 235 236 237 238 typedef void _IO_lock_t; 239 240 241 242 243 244 struct _IO_FILE 245 { 246 int _flags; 247 248 249 char *_IO_read_ptr; 250 char *_IO_read_end; 251 char *_IO_read_base; 252 char *_IO_write_base; 253 char *_IO_write_ptr; 254 char *_IO_write_end; 255 char *_IO_buf_base; 256 char *_IO_buf_end; 257 258 259 char *_IO_save_base; 260 char *_IO_backup_base; 261 char *_IO_save_end; 262 263 struct _IO_marker *_markers; 264 265 struct _IO_FILE *_chain; 266 267 int _fileno; 268 int _flags2; 269 __off_t _old_offset; 270 271 272 unsigned short _cur_column; 273 signed char _vtable_offset; 274 char _shortbuf[1]; 275 276 _IO_lock_t *_lock; 277 278 279 280 281 282 283 284 __off64_t _offset; 285 286 struct _IO_codecvt *_codecvt; 287 struct _IO_wide_data *_wide_data; 288 struct _IO_FILE *_freeres_list; 289 void *_freeres_buf; 290 size_t __pad5; 291 int _mode; 292 293 char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)]; 294 }; 295 # 44 "/usr/include/stdio.h" 2 3 4 296 # 52 "/usr/include/stdio.h" 3 4 297 typedef __gnuc_va_list va_list; 298 # 63 "/usr/include/stdio.h" 3 4 299 typedef __off_t off_t; 300 # 77 "/usr/include/stdio.h" 3 4 301 typedef __ssize_t ssize_t; 302 303 304 305 306 307 308 typedef __fpos_t fpos_t; 309 # 133 "/usr/include/stdio.h" 3 4 310 # 1 "/usr/include/bits/stdio_lim.h" 1 3 4 311 # 134 "/usr/include/stdio.h" 2 3 4 312 313 314 315 extern FILE *stdin; 316 extern FILE *stdout; 317 extern FILE *stderr; 318 319 320 321 322 323 324 extern int remove (const char *__filename) __attribute__ ((__nothrow__ , __leaf__)); 325 326 extern int rename (const char *__old, const char *__new) __attribute__ ((__nothrow__ , __leaf__)); 327 328 329 330 extern int renameat (int __oldfd, const char *__old, int __newfd, 331 const char *__new) __attribute__ ((__nothrow__ , __leaf__)); 332 # 173 "/usr/include/stdio.h" 3 4 333 extern FILE *tmpfile (void) ; 334 # 187 "/usr/include/stdio.h" 3 4 335 extern char *tmpnam (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ; 336 337 338 339 340 extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ; 341 # 204 "/usr/include/stdio.h" 3 4 342 extern char *tempnam (const char *__dir, const char *__pfx) 343 __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) ; 344 345 346 347 348 349 350 351 extern int fclose (FILE *__stream); 352 353 354 355 356 extern int fflush (FILE *__stream); 357 # 227 "/usr/include/stdio.h" 3 4 358 extern int fflush_unlocked (FILE *__stream); 359 # 246 "/usr/include/stdio.h" 3 4 360 extern FILE *fopen (const char *__restrict __filename, 361 const char *__restrict __modes) ; 362 363 364 365 366 extern FILE *freopen (const char *__restrict __filename, 367 const char *__restrict __modes, 368 FILE *__restrict __stream) ; 369 # 279 "/usr/include/stdio.h" 3 4 370 extern FILE *fdopen (int __fd, const char *__modes) __attribute__ ((__nothrow__ , __leaf__)) ; 371 # 292 "/usr/include/stdio.h" 3 4 372 extern FILE *fmemopen (void *__s, size_t __len, const char *__modes) 373 __attribute__ ((__nothrow__ , __leaf__)) ; 374 375 376 377 378 extern FILE *open_memstream (char **__bufloc, size_t *__sizeloc) __attribute__ ((__nothrow__ , __leaf__)) ; 379 380 381 382 383 384 extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __attribute__ ((__nothrow__ , __leaf__)); 385 386 387 388 extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf, 389 int __modes, size_t __n) __attribute__ ((__nothrow__ , __leaf__)); 390 391 392 393 394 extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf, 395 size_t __size) __attribute__ ((__nothrow__ , __leaf__)); 396 397 398 extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 399 400 401 402 403 404 405 406 extern int fprintf (FILE *__restrict __stream, 407 const char *__restrict __format, ...); 408 409 410 411 412 extern int printf (const char *__restrict __format, ...); 413 414 extern int sprintf (char *__restrict __s, 415 const char *__restrict __format, ...) __attribute__ ((__nothrow__)); 416 417 418 419 420 421 extern int vfprintf (FILE *__restrict __s, const char *__restrict __format, 422 __gnuc_va_list __arg); 423 424 425 426 427 extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg); 428 429 extern int vsprintf (char *__restrict __s, const char *__restrict __format, 430 __gnuc_va_list __arg) __attribute__ ((__nothrow__)); 431 432 433 434 extern int snprintf (char *__restrict __s, size_t __maxlen, 435 const char *__restrict __format, ...) 436 __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 4))); 437 438 extern int vsnprintf (char *__restrict __s, size_t __maxlen, 439 const char *__restrict __format, __gnuc_va_list __arg) 440 __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0))); 441 # 379 "/usr/include/stdio.h" 3 4 442 extern int vdprintf (int __fd, const char *__restrict __fmt, 443 __gnuc_va_list __arg) 444 __attribute__ ((__format__ (__printf__, 2, 0))); 445 extern int dprintf (int __fd, const char *__restrict __fmt, ...) 446 __attribute__ ((__format__ (__printf__, 2, 3))); 447 448 449 450 451 452 453 454 extern int fscanf (FILE *__restrict __stream, 455 const char *__restrict __format, ...) ; 456 457 458 459 460 extern int scanf (const char *__restrict __format, ...) ; 461 462 extern int sscanf (const char *__restrict __s, 463 const char *__restrict __format, ...) __attribute__ ((__nothrow__ , __leaf__)); 464 465 466 467 468 469 # 1 "/usr/include/bits/floatn.h" 1 3 4 470 # 119 "/usr/include/bits/floatn.h" 3 4 471 # 1 "/usr/include/bits/floatn-common.h" 1 3 4 472 # 24 "/usr/include/bits/floatn-common.h" 3 4 473 # 1 "/usr/include/bits/long-double.h" 1 3 4 474 # 25 "/usr/include/bits/floatn-common.h" 2 3 4 475 # 120 "/usr/include/bits/floatn.h" 2 3 4 476 # 407 "/usr/include/stdio.h" 2 3 4 477 478 479 480 extern int fscanf (FILE *__restrict __stream, const char *__restrict __format, ...) __asm__ ("" "__isoc99_fscanf") 481 482 ; 483 extern int scanf (const char *__restrict __format, ...) __asm__ ("" "__isoc99_scanf") 484 ; 485 extern int sscanf (const char *__restrict __s, const char *__restrict __format, ...) __asm__ ("" "__isoc99_sscanf") __attribute__ ((__nothrow__ , __leaf__)) 486 487 ; 488 # 435 "/usr/include/stdio.h" 3 4 489 extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, 490 __gnuc_va_list __arg) 491 __attribute__ ((__format__ (__scanf__, 2, 0))) ; 492 493 494 495 496 497 extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) 498 __attribute__ ((__format__ (__scanf__, 1, 0))) ; 499 500 501 extern int vsscanf (const char *__restrict __s, 502 const char *__restrict __format, __gnuc_va_list __arg) 503 __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__format__ (__scanf__, 2, 0))); 504 505 506 507 508 509 extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vfscanf") 510 511 512 513 __attribute__ ((__format__ (__scanf__, 2, 0))) ; 514 extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vscanf") 515 516 __attribute__ ((__format__ (__scanf__, 1, 0))) ; 517 extern int vsscanf (const char *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vsscanf") __attribute__ ((__nothrow__ , __leaf__)) 518 519 520 521 __attribute__ ((__format__ (__scanf__, 2, 0))); 522 # 489 "/usr/include/stdio.h" 3 4 523 extern int fgetc (FILE *__stream); 524 extern int getc (FILE *__stream); 525 526 527 528 529 530 extern int getchar (void); 531 532 533 534 535 536 537 extern int getc_unlocked (FILE *__stream); 538 extern int getchar_unlocked (void); 539 # 514 "/usr/include/stdio.h" 3 4 540 extern int fgetc_unlocked (FILE *__stream); 541 # 525 "/usr/include/stdio.h" 3 4 542 extern int fputc (int __c, FILE *__stream); 543 extern int putc (int __c, FILE *__stream); 544 545 546 547 548 549 extern int putchar (int __c); 550 # 541 "/usr/include/stdio.h" 3 4 551 extern int fputc_unlocked (int __c, FILE *__stream); 552 553 554 555 556 557 558 559 extern int putc_unlocked (int __c, FILE *__stream); 560 extern int putchar_unlocked (int __c); 561 562 563 564 565 566 567 extern int getw (FILE *__stream); 568 569 570 extern int putw (int __w, FILE *__stream); 571 572 573 574 575 576 577 578 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream) 579 __attribute__ ((__access__ (__write_only__, 1, 2))); 580 # 608 "/usr/include/stdio.h" 3 4 581 extern __ssize_t __getdelim (char **__restrict __lineptr, 582 size_t *__restrict __n, int __delimiter, 583 FILE *__restrict __stream) ; 584 extern __ssize_t getdelim (char **__restrict __lineptr, 585 size_t *__restrict __n, int __delimiter, 586 FILE *__restrict __stream) ; 587 588 589 590 591 592 593 594 extern __ssize_t getline (char **__restrict __lineptr, 595 size_t *__restrict __n, 596 FILE *__restrict __stream) ; 597 598 599 600 601 602 603 604 extern int fputs (const char *__restrict __s, FILE *__restrict __stream); 605 606 607 608 609 610 extern int puts (const char *__s); 611 612 613 614 615 616 617 extern int ungetc (int __c, FILE *__stream); 618 619 620 621 622 623 624 extern size_t fread (void *__restrict __ptr, size_t __size, 625 size_t __n, FILE *__restrict __stream) ; 626 627 628 629 630 extern size_t fwrite (const void *__restrict __ptr, size_t __size, 631 size_t __n, FILE *__restrict __s); 632 # 678 "/usr/include/stdio.h" 3 4 633 extern size_t fread_unlocked (void *__restrict __ptr, size_t __size, 634 size_t __n, FILE *__restrict __stream) ; 635 extern size_t fwrite_unlocked (const void *__restrict __ptr, size_t __size, 636 size_t __n, FILE *__restrict __stream); 637 638 639 640 641 642 643 644 extern int fseek (FILE *__stream, long int __off, int __whence); 645 646 647 648 649 extern long int ftell (FILE *__stream) ; 650 651 652 653 654 extern void rewind (FILE *__stream); 655 # 712 "/usr/include/stdio.h" 3 4 656 extern int fseeko (FILE *__stream, __off_t __off, int __whence); 657 658 659 660 661 extern __off_t ftello (FILE *__stream) ; 662 # 736 "/usr/include/stdio.h" 3 4 663 extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos); 664 665 666 667 668 extern int fsetpos (FILE *__stream, const fpos_t *__pos); 669 # 762 "/usr/include/stdio.h" 3 4 670 extern void clearerr (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 671 672 extern int feof (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 673 674 extern int ferror (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 675 676 677 678 extern void clearerr_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 679 extern int feof_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 680 extern int ferror_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 681 682 683 684 685 686 687 688 extern void perror (const char *__s); 689 690 691 692 693 extern int fileno (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 694 695 696 697 698 extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 699 # 799 "/usr/include/stdio.h" 3 4 700 extern FILE *popen (const char *__command, const char *__modes) ; 701 702 703 704 705 706 extern int pclose (FILE *__stream); 707 708 709 710 711 712 extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__)); 713 # 839 "/usr/include/stdio.h" 3 4 714 extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 715 716 717 718 extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; 719 720 721 extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); 722 # 857 "/usr/include/stdio.h" 3 4 723 extern int __uflow (FILE *); 724 extern int __overflow (FILE *, int); 725 # 874 "/usr/include/stdio.h" 3 4 726 727 # 2 "hello1.c" 2 728 729 730 731 732 # 5 "hello1.c" 733 int main(int argc, char **argv) 734 { 735 printf("Hello world! NUM: %d ", 8); 736 737 return 0; 738 }
735行可以明显看出宏定义中的#define NUM 8已经其作用了,NUM已经被替换成功,包含条件编译的代码如下:
1 #include <stdio.h> 2 3 #define NUM 8 4 5 int main(int argc, char **argv) 6 { 7 printf("Hello world! NUM: %d ", NUM); 8 9 #ifdef TEST 10 printf("Test mode ...... "); 11 #endif 12 13 return 0; 14 }
若使用gcc -UTEST -g hello1.c -o hello1T,没有定义宏定义TEST,则结果为:
预编译结果为,其他不太相关部分省略:
# 857 "/usr/include/stdio.h" 3 4 722 extern int __uflow (FILE *); 723 extern int __overflow (FILE *, int); 724 # 874 "/usr/include/stdio.h" 3 4 725 726 # 2 "hello1.c" 2 727 728 729 730 731 # 5 "hello1.c" 732 int main(int argc, char **argv) 733 { 734 printf("Hello world! NUM: %d ", 8); 735 736 737 738 739 740 return 0; 741 }
最终执行结果为:
Hello world! NUM: 8
若使用gcc -DTEST -g hello1.c -o hello1N,定义了宏定义TEST,则结果为:
721 # 857 "/usr/include/stdio.h" 3 4 722 extern int __uflow (FILE *); 723 extern int __overflow (FILE *, int); 724 # 874 "/usr/include/stdio.h" 3 4 725 726 # 2 "hello1.c" 2 727 728 729 730 731 # 5 "hello1.c" 732 int main(int argc, char **argv) 733 { 734 printf("Hello world! NUM: %d ", 8); 735 736 737 printf("Test mode ...... "); 738 739 740 return 0; 741 }
最终执行结果为:
Hello world! NUM: 8
Test mode ......
三、源码的编译:
gcc -S hello.c或 gcc -S (-masam=att) hello.c -o hello.s结果为,是AT&T汇编:
1 .file "hello.c" 2 .text 3 .section .rodata 4 .LC0: 5 .string "Hello world!" 6 .text 7 .globl main 8 .type main, @function 9 main: 10 .LFB0: 11 .cfi_startproc 12 pushq %rbp 13 .cfi_def_cfa_offset 16 14 .cfi_offset 6, -16 15 movq %rsp, %rbp 16 .cfi_def_cfa_register 6 17 subq $16, %rsp 18 movl %edi, -4(%rbp) 19 movq %rsi, -16(%rbp) 20 leaq .LC0(%rip), %rdi 21 call puts@PLT 22 movl $0, %eax 23 leave 24 .cfi_def_cfa 7, 8 25 ret 26 .cfi_endproc 27 .LFE0: 28 .size main, .-main 29 .ident "GCC: (GNU) 10.2.0" 30 .section .note.GNU-stack,"",@progbits
gcc -S -masam=intel hello.c -o hello.s结果为,是INTEL汇编:
1 .file "hello.c" 2 .intel_syntax noprefix 3 .text 4 .section .rodata 5 .LC0: 6 .string "Hello world!" 7 .text 8 .globl main 9 .type main, @function 10 main: 11 .LFB0: 12 .cfi_startproc 13 push rbp 14 .cfi_def_cfa_offset 16 15 .cfi_offset 6, -16 16 mov rbp, rsp 17 .cfi_def_cfa_register 6 18 sub rsp, 16 19 mov DWORD PTR -4[rbp], edi 20 mov QWORD PTR -16[rbp], rsi 21 lea rdi, .LC0[rip] 22 call puts@PLT 23 mov eax, 0 24 leave 25 .cfi_def_cfa 7, 8 26 ret 27 .cfi_endproc 28 .LFE0: 29 .size main, .-main 30 .ident "GCC: (GNU) 10.2.0" 31 .section .note.GNU-stack,"",@progbits
下面讲解以att的代码为例:
说明:第1行:表明文件名为hello.c
第2行:text段开始
第3行:表示节名,只读段没有内容
第4行:只是一个标号,具体不太明白
第5行:用到的字符串"Hello world!"
第6行:代码节,text段开始
第7行:main是全局变量名
第8行:表明main是个函数名
第9行:表示main函数开始
第10行:表示main函数代码的开始,第一个函数的开始;
第11行:cif(call frame infromation)开始,表明有调用函数
第12行:保存栈基址
第13行:偏移出16个字节的空间,cfa(Canonical Frame Address规范化帧地址)
第14行:先前寄存器的值保存在取自CFA的偏移地址中
第15行:将栈顶指针作为新的栈基址
第16行:Canonical Frame Address (规范化帧地址)
第17行:开辟16字节的空间
第18行:将edi数据存入栈帧
第19行:将rsi数据存入栈帧
第20行:计算新地址
第21行:调用函数PLT
第22行:将0放入eax中,准备返回值,0为正常返回
第23行:离开
第24行:改变cfa的偏移地址
第25行:返回
第26行:cif(call frame infromation)结束,
第27行:表示main函数代码的结束,第一个函数的结束
第28行:size信息
第29行:编译器的信息
第30行:段的信息
.cfi_def_cfa register, offset:定义规范化栈地址:将寄存器中的地址进行偏移获取新地址;
.cfi_def_cfa_register register:改变计算规范化地址的规则:从现在开始寄存器用新值替换旧的值,偏移地址保持不变;
.cfi_def_cfa_offset offset:改变计算规范化地址的规则:
寄存器不变,改变偏移地址;
改变计算规范化地址的规则:
寄存器不变,改变偏移地址;.cfi_adjust_cfa_offset offset:和
.cfi_def_cfa_offset
相同,不同的是偏移地址通过相加或相减先前的偏移地址;
.cfi_offset register, offset:先前寄存器的值保存在取自CFA的偏移地址中;
四、汇编源码的汇编:
gcc -c helloA.s 生成helloA.o文件
可以通过objdump -d helloA.o
1 ~ � WORKM � studyCode � gnuC � objdump -d helloA.o 2 3 helloA.o: 文件格式 elf64-x86-64 4 5 6 Disassembly of section .text: 7 8 0000000000000000 <main>: 9 0: 55 push %rbp 10 1: 48 89 e5 mov %rsp,%rbp 11 4: 48 83 ec 10 sub $0x10,%rsp 12 8: 89 7d fc mov %edi,-0x4(%rbp) 13 b: 48 89 75 f0 mov %rsi,-0x10(%rbp) 14 f: 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # 16 <main+0x16> 15 16: e8 00 00 00 00 callq 1b <main+0x1b> 16 1b: b8 00 00 00 00 mov $0x0,%eax 17 20: c9 leaveq 18 21: c3 retq
gcc helloA.o -o helloA 生成可执行文件
也可以使用objdump -D helloAA >> helloAAA,内容为
1 helloAA: 文件格式 elf64-x86-64 2 3 4 Disassembly of section .init: 5 6 0000000000001000 <_init>: 7 1000: f3 0f 1e fa endbr64 8 1004: 48 83 ec 08 sub $0x8,%rsp 9 1008: 48 8b 05 d9 2f 00 00 mov 0x2fd9(%rip),%rax # 3fe8 <__gmon_start__> 10 100f: 48 85 c0 test %rax,%rax 11 1012: 74 02 je 1016 <_init+0x16> 12 1014: ff d0 callq *%rax 13 1016: 48 83 c4 08 add $0x8,%rsp 14 101a: c3 retq 15 16 Disassembly of section .plt: 17 18 0000000000001020 <.plt>: 19 1020: ff 35 e2 2f 00 00 pushq 0x2fe2(%rip) # 4008 <_GLOBAL_OFFSET_TABLE_+0x8> 20 1026: ff 25 e4 2f 00 00 jmpq *0x2fe4(%rip) # 4010 <_GLOBAL_OFFSET_TABLE_+0x10> 21 102c: 0f 1f 40 00 nopl 0x0(%rax) 22 23 0000000000001030 <puts@plt>: 24 1030: ff 25 e2 2f 00 00 jmpq *0x2fe2(%rip) # 4018 <puts@GLIBC_2.2.5> 25 1036: 68 00 00 00 00 pushq $0x0 26 103b: e9 e0 ff ff ff jmpq 1020 <.plt> 27 28 Disassembly of section .text: 29 30 0000000000001040 <_start>: 31 1040: f3 0f 1e fa endbr64 32 1044: 31 ed xor %ebp,%ebp 33 1046: 49 89 d1 mov %rdx,%r9 34 1049: 5e pop %rsi 35 104a: 48 89 e2 mov %rsp,%rdx 36 104d: 48 83 e4 f0 and $0xfffffffffffffff0,%rsp 37 1051: 50 push %rax 38 1052: 54 push %rsp 39 1053: 4c 8d 05 76 01 00 00 lea 0x176(%rip),%r8 # 11d0 <__libc_csu_fini> 40 105a: 48 8d 0d ff 00 00 00 lea 0xff(%rip),%rcx # 1160 <__libc_csu_init> 41 1061: 48 8d 3d d1 00 00 00 lea 0xd1(%rip),%rdi # 1139 <main> 42 1068: ff 15 72 2f 00 00 callq *0x2f72(%rip) # 3fe0 <__libc_start_main@GLIBC_2.2.5> 43 106e: f4 hlt 44 106f: 90 nop 45 46 0000000000001070 <deregister_tm_clones>: 47 1070: 48 8d 3d b9 2f 00 00 lea 0x2fb9(%rip),%rdi # 4030 <__TMC_END__> 48 1077: 48 8d 05 b2 2f 00 00 lea 0x2fb2(%rip),%rax # 4030 <__TMC_END__> 49 107e: 48 39 f8 cmp %rdi,%rax 50 1081: 74 15 je 1098 <deregister_tm_clones+0x28> 51 1083: 48 8b 05 4e 2f 00 00 mov 0x2f4e(%rip),%rax # 3fd8 <_ITM_deregisterTMCloneTable> 52 108a: 48 85 c0 test %rax,%rax 53 108d: 74 09 je 1098 <deregister_tm_clones+0x28> 54 108f: ff e0 jmpq *%rax 55 1091: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 56 1098: c3 retq 57 1099: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 58 59 00000000000010a0 <register_tm_clones>: 60 10a0: 48 8d 3d 89 2f 00 00 lea 0x2f89(%rip),%rdi # 4030 <__TMC_END__> 61 10a7: 48 8d 35 82 2f 00 00 lea 0x2f82(%rip),%rsi # 4030 <__TMC_END__> 62 10ae: 48 29 fe sub %rdi,%rsi 63 10b1: 48 89 f0 mov %rsi,%rax 64 10b4: 48 c1 ee 3f shr $0x3f,%rsi 65 10b8: 48 c1 f8 03 sar $0x3,%rax 66 10bc: 48 01 c6 add %rax,%rsi 67 10bf: 48 d1 fe sar %rsi 68 10c2: 74 14 je 10d8 <register_tm_clones+0x38> 69 10c4: 48 8b 05 25 2f 00 00 mov 0x2f25(%rip),%rax # 3ff0 <_ITM_registerTMCloneTable> 70 10cb: 48 85 c0 test %rax,%rax 71 10ce: 74 08 je 10d8 <register_tm_clones+0x38> 72 10d0: ff e0 jmpq *%rax 73 10d2: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) 74 10d8: c3 retq 75 10d9: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 76 77 00000000000010e0 <__do_global_dtors_aux>: 78 10e0: f3 0f 1e fa endbr64 79 10e4: 80 3d 45 2f 00 00 00 cmpb $0x0,0x2f45(%rip) # 4030 <__TMC_END__> 80 10eb: 75 33 jne 1120 <__do_global_dtors_aux+0x40> 81 10ed: 55 push %rbp 82 10ee: 48 83 3d 02 2f 00 00 cmpq $0x0,0x2f02(%rip) # 3ff8 <__cxa_finalize@GLIBC_2.2.5> 83 10f5: 00 84 10f6: 48 89 e5 mov %rsp,%rbp 85 10f9: 74 0d je 1108 <__do_global_dtors_aux+0x28> 86 10fb: 48 8b 3d 26 2f 00 00 mov 0x2f26(%rip),%rdi # 4028 <__dso_handle> 87 1102: ff 15 f0 2e 00 00 callq *0x2ef0(%rip) # 3ff8 <__cxa_finalize@GLIBC_2.2.5> 88 1108: e8 63 ff ff ff callq 1070 <deregister_tm_clones> 89 110d: c6 05 1c 2f 00 00 01 movb $0x1,0x2f1c(%rip) # 4030 <__TMC_END__> 90 1114: 5d pop %rbp 91 1115: c3 retq 92 1116: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 93 111d: 00 00 00 94 1120: c3 retq 95 1121: 66 66 2e 0f 1f 84 00 data16 nopw %cs:0x0(%rax,%rax,1) 96 1128: 00 00 00 00 97 112c: 0f 1f 40 00 nopl 0x0(%rax) 98 99 0000000000001130 <frame_dummy>: 100 1130: f3 0f 1e fa endbr64 101 1134: e9 67 ff ff ff jmpq 10a0 <register_tm_clones> 102 103 0000000000001139 <main>: 104 1139: 55 push %rbp 105 113a: 48 89 e5 mov %rsp,%rbp 106 113d: 48 83 ec 10 sub $0x10,%rsp 107 1141: 89 7d fc mov %edi,-0x4(%rbp) 108 1144: 48 89 75 f0 mov %rsi,-0x10(%rbp) 109 1148: 48 8d 3d b5 0e 00 00 lea 0xeb5(%rip),%rdi # 2004 <_IO_stdin_used+0x4> 110 114f: e8 dc fe ff ff callq 1030 <puts@plt> 111 1154: b8 00 00 00 00 mov $0x0,%eax 112 1159: c9 leaveq 113 115a: c3 retq 114 115b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 115 116 0000000000001160 <__libc_csu_init>: 117 1160: f3 0f 1e fa endbr64 118 1164: 41 57 push %r15 119 1166: 4c 8d 3d 7b 2c 00 00 lea 0x2c7b(%rip),%r15 # 3de8 <__frame_dummy_init_array_entry> 120 116d: 41 56 push %r14 121 116f: 49 89 d6 mov %rdx,%r14 122 1172: 41 55 push %r13 123 1174: 49 89 f5 mov %rsi,%r13 124 1177: 41 54 push %r12 125 1179: 41 89 fc mov %edi,%r12d 126 117c: 55 push %rbp 127 117d: 48 8d 2d 6c 2c 00 00 lea 0x2c6c(%rip),%rbp # 3df0 <__do_global_dtors_aux_fini_array_entry> 128 1184: 53 push %rbx 129 1185: 4c 29 fd sub %r15,%rbp 130 1188: 48 83 ec 08 sub $0x8,%rsp 131 118c: e8 6f fe ff ff callq 1000 <_init> 132 1191: 48 c1 fd 03 sar $0x3,%rbp 133 1195: 74 1f je 11b6 <__libc_csu_init+0x56> 134 1197: 31 db xor %ebx,%ebx 135 1199: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 136 11a0: 4c 89 f2 mov %r14,%rdx 137 11a3: 4c 89 ee mov %r13,%rsi 138 11a6: 44 89 e7 mov %r12d,%edi 139 11a9: 41 ff 14 df callq *(%r15,%rbx,8) 140 11ad: 48 83 c3 01 add $0x1,%rbx 141 11b1: 48 39 dd cmp %rbx,%rbp 142 11b4: 75 ea jne 11a0 <__libc_csu_init+0x40> 143 11b6: 48 83 c4 08 add $0x8,%rsp 144 11ba: 5b pop %rbx 145 11bb: 5d pop %rbp 146 11bc: 41 5c pop %r12 147 11be: 41 5d pop %r13 148 11c0: 41 5e pop %r14 149 11c2: 41 5f pop %r15 150 11c4: c3 retq 151 11c5: 66 66 2e 0f 1f 84 00 data16 nopw %cs:0x0(%rax,%rax,1) 152 11cc: 00 00 00 00 153 154 00000000000011d0 <__libc_csu_fini>: 155 11d0: f3 0f 1e fa endbr64 156 11d4: c3 retq 157 158 Disassembly of section .fini: 159 160 00000000000011d8 <_fini>: 161 11d8: f3 0f 1e fa endbr64 162 11dc: 48 83 ec 08 sub $0x8,%rsp 163 11e0: 48 83 c4 08 add $0x8,%rsp 164 11e4: c3 retq ~
五、链接:
经过前面的步骤,可以生成多个目标代码,壳虫定位目标码都是按地址0作为起始地址的,调用其他模块的函数的callq指令中的地址并没有填充被调用代码的地址。链接主要完成两个事情:
(1)、将各个模块目标代码在程序空间中指定自己的不重叠的位置和区间,这个举动常常称为“布局”。
(2)、对未确定的地址将布局后对应的函数地址替换函数调用中的全0地址,这个举动常被称为重定位,调用其他模块上的函数需要重定位,引用其他模块上的变量需要重定位,甚至引用本模块中的全局变量也需要重定位;使用静态链接,程序创建后就可以确定地址;使用动态链接,需要一个辅助位置,在程序运行中才能确定具体地址;
六、编译驱动:
gcc -v -g hello1.c -o hello1
1 ~ � WORKM � studyCode � gnuC � gcc -v -g hello1.c -o hello1 2 使用内建 specs。 3 COLLECT_GCC=gcc 4 COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper 5 目标:x86_64-pc-linux-gnu 6 配置为:/build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-werror gdc_include_dir=/usr/include/dlang/gdc 7 线程模型:posix 8 Supported LTO compression algorithms: zlib zstd 9 gcc 版本 10.2.0 (GCC) 10 COLLECT_GCC_OPTIONS='-v' '-g' '-o' 'hello1' '-mtune=generic' '-march=x86-64' 11 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/cc1 -quiet -v hello1.c -quiet -dumpbase hello1.c -mtune=generic -march=x86-64 -auxbase hello1 -g -version -o /tmp/cceOgteA.s 12 GNU C17 (GCC) 版本 10.2.0 (x86_64-pc-linux-gnu) 13 由 GNU C 版本 10.2.0 编译,GMP 版本 6.2.0,MPFR 版本 4.1.0,MPC 版本 1.1.0,isl 版本 isl-0.21-GMP 14 15 GGC 准则:--param ggc-min-expand=100 --param ggc-min-heapsize=131072 16 忽略不存在的目录“/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/include” 17 #include "..." 搜索从这里开始: 18 #include <...> 搜索从这里开始: 19 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include 20 /usr/local/include 21 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include-fixed 22 /usr/include 23 搜索列表结束。 24 GNU C17 (GCC) 版本 10.2.0 (x86_64-pc-linux-gnu) 25 由 GNU C 版本 10.2.0 编译,GMP 版本 6.2.0,MPFR 版本 4.1.0,MPC 版本 1.1.0,isl 版本 isl-0.21-GMP 26 27 GGC 准则:--param ggc-min-expand=100 --param ggc-min-heapsize=131072 28 Compiler executable checksum: 81412c83e0c3f5014cd5328f9bc3d624 29 COLLECT_GCC_OPTIONS='-v' '-g' '-o' 'hello1' '-mtune=generic' '-march=x86-64' 30 as -v --64 -o /tmp/cc58cN9y.o /tmp/cceOgteA.s 31 GNU assembler version 2.35.1 (x86_64-pc-linux-gnu) using BFD version (GNU Binutils) 2.35.1 32 COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/ 33 LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../:/lib/:/usr/lib/ 34 COLLECT_GCC_OPTIONS='-v' '-g' '-o' 'hello1' '-mtune=generic' '-march=x86-64' 35 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/collect2 -plugin /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccaxp9Vy.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -o hello1 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/Scrt1.o /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/crti.o /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/crtbeginS.o -L/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0 -L/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../.. /tmp/cc58cN9y.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/crtendS.o /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/crtn.o 36 COLLECT_GCC_OPTIONS='-v' '-g' '-o' 'hello1' '-mtune=generic' '-march=x86-64'
搞定了。