|
发表于 2006-1-13 14:30:13
|
显示全部楼层
一起讨论一下吧,呵呵。我是用 a.out 格式来理解的。
下面是 UNIX 中 a.out 格式文件的布局:
- /* Layout of a.out file :
- *
- * header of 8 words magic number 405, 407, 410, 411
- * text size )
- * data size ) in bytes but even
- * bss size )
- * symbol table size
- * entry point
- * {unused}
- * flag set if no relocation
- *
- *
- * header: 0
- * text: 16
- * data: 16+textsize
- * relocation: 16+textsize+datasize
- * symbol table: 16+2*(textsize+datasize) or 16+textsize+datasize
- *
- */
复制代码
下面是 a.out.h:
- struct exec { /* a.out header */
- int a_magic; /* magic number */
- unsigned a_text; /* size of text segment */
- unsigned a_data; /* size of initialized data */
- unsigned a_bss; /* size of unitialized data */
- unsigned a_syms; /* size of symbol table */
- unsigned a_entry; /* entry point */
- unsigned a_unused; /* not used */
- unsigned a_flag; /* relocation info stripped */
- };
- #define A_MAGIC1 0407 /* normal */
- #define A_MAGIC2 0410 /* read-only text */
- #define A_MAGIC3 0411 /* separated I&D */
- #define A_MAGIC4 0405 /* overlay */
- struct nlist { /* symbol table entry */
- char n_name[8]; /* symbol name */
- int n_type; /* type flag */
- unsigned n_value; /* value */
- };
- /* values for type flag */
- #define N_UNDF 0 /* undefined */
- #define N_ABS 01 /* absolute */
- #define N_TEXT 02 /* text symbol */
- #define N_DATA 03 /* data symbol */
- #define N_BSS 04 /* bss symbol */
- #define N_TYPE 037
- #define N_REG 024 /* register name */
- #define N_FN 037 /* file name symbol */
- #define N_EXT 040 /* external bit, or'ed in */
- #define FORMAT "%06o" /* to print a value */
复制代码
这是一个早期的 strip:
- #include <a.out.h>
- #include <signal.h>
- char *tname;
- char *mktemp();
- struct exec head;
- int a_magic[] = {A_MAGIC1, A_MAGIC2, A_MAGIC3, A_MAGIC4, 0};
- int status;
- int tf;
- main(argc, argv)
- char *argv[];
- {
- register i;
- signal(SIGHUP, SIG_IGN);
- signal(SIGINT, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
- tname = mktemp("/tmp/sXXXXX");
- close(creat(tname, 0600));
- tf = open(tname, 2);
- if(tf < 0) {
- printf("cannot create temp file\n");
- exit(2);
- }
- for(i=1; i<argc; i++) {
- strip(argv[i]);
- if(status > 1)
- break;
- }
- close(tf);
- unlink(tname);
- exit(status);
- }
- strip(name)
- char *name;
- {
- register f;
- long size;
- int i;
- f = open(name, 0);
- if(f < 0) {
- printf("cannot open %s\n", name);
- status = 1;
- goto out;
- }
- read(f, (char *)&head, sizeof(head));
- for(i=0;a_magic[i];i++)
- if(a_magic[i] == head.a_magic) break;
- if(a_magic[i] == 0) {
- printf("%s not in a.out format\n", name);
- status = 1;
- goto out;
- }
- if(head.a_syms == 0 && (head.a_flag&1) != 0) {
- printf("%s already stripped\n", name);
- goto out;
- }
- size = (long)head.a_text + head.a_data;
- head.a_syms = 0;
- head.a_flag |= 1;
- lseek(tf, (long)0, 0);
- write(tf, (char *)&head, sizeof(head));
- if(copy(name, f, tf, size)) {
- status = 1;
- goto out;
- }
- size += sizeof(head);
- close(f);
- f = creat(name, 0666);
- if(f < 0) {
- printf("%s cannot recreate\n", name);
- status = 1;
- goto out;
- }
- lseek(tf, (long)0, 0);
- if(copy(name, tf, f, size))
- status = 2;
- out:
- close(f);
- }
- copy(name, fr, to, size)
- char *name;
- long size;
- {
- register s, n;
- char buf[512];
- while(size != 0) {
- s = 512;
- if(size < 512)
- s = size;
- n = read(fr, buf, s);
- if(n != s) {
- printf("%s unexpected eof\n", name);
- return(1);
- }
- n = write(to, buf, s);
- if(n != s) {
- printf("%s unexpected write eof\n", name);
- return(1);
- }
- size -= s;
- }
- return(0);
- }
复制代码
这就是 strip 的作用。 |
|