2011-04-01 57 views
0

字符串我有如下一个非常简单的程序: 的#include查找在编译的可执行

int main(){ 
char* mystring = "ABCDEFGHIJKLMNO"; 
puts(mystring); 

char otherstring[15]; 
otherstring[0] = 'a'; 
otherstring[1] = 'b'; 
otherstring[2] = 'c'; 
otherstring[3] = 'd'; 
otherstring[4] = 'e'; 
otherstring[5] = 'f'; 
otherstring[6] = 'g'; 
otherstring[7] = 'h'; 
otherstring[8] = 'i'; 
otherstring[9] = 'j'; 
otherstring[10] = 'k'; 
otherstring[11] = 'l'; 
otherstring[12] = 'm'; 
otherstring[13] = 'n'; 
otherstring[14] = 'o'; 
puts(otherstring); 

return 0; 
} 

编译器为MS VC++。

我是否使用或不使用优化来构建此程序我可以使用十六进制编辑器在可执行文件中找到字符串“ABCDEFGHIJKLMNO”。

但是,什么是编译器这样做是otherstring不同,我不能找到字符串“ABCDEFGHIJKLMNO”

我使用的十六进制编辑器是Hexedit - 但尝试了其他人,仍然无法找到otherstring。任何人有任何想法为什么不或如何找到?

顺便说一下,我不是因为黑客的原因这样做。

+0

你能找到字符串 “\ X00 \ X00 \ x00b \ X00 \ X00 \ x00c \ X00 \ X00 \ X00” 中的二进制文件(使用4个字节的每一个字母)? – pmg 2011-04-01 11:38:00

+0

$ strings binary-file | grep“ABCDEFGHIJKLMNO” – Chappelle 2012-02-21 14:13:51

回答

5

这是我的gcc用这段代码所做的。我假设你的编译器做了类似的事情。字符串常量存储在只读部分,mystring用它的地址初始化。
各个字符都直接放置在堆栈的阵列位置。还要注意的是,当你调用puts时,其他字符串不会被NULL终止。

  .file "test.c" 
      .section  .rodata 
    .LC0: 
      .string "ABCDEFGHIJKLMNO" 
      .text 
    .globl main 
      .type main, @function 
    main: 
    .LFB0: 
      .cfi_startproc 
      pushq %rbp 
       .cfi_def_cfa_offset 16 
      movq %rsp, %rbp 
      .cfi_offset 6, -16 
      .cfi_def_cfa_register 6 
      subq $48, %rsp 
      movq %fs:40, %rax 
      movq %rax, -8(%rbp) 
      xorl %eax, %eax 
    /* here is where mystring is loaded with the address of "ABCDEFGHIJKLMNO" */ 
      movq $.LC0, -40(%rbp) 
    /* this is the call to puts */ 
       movq -40(%rbp), %rax 
      movq %rax, %rdi 
      call puts 
    /* here is where the bytes are loaded into otherstring on the stack */    
      movb $97, -32(%rbp) //'a' 
      movb $98, -31(%rbp) //'b' 
      movb $99, -30(%rbp) //'c' 
      movb $100, -29(%rbp) //'d' 
      movb $101, -28(%rbp) //'e' 
      movb $102, -27(%rbp) //'f' 
      movb $103, -26(%rbp) //'g' 
      movb $104, -25(%rbp) //'h' 
      movb $105, -24(%rbp) //'i' 
      movb $106, -23(%rbp) //'j' 
      movb $107, -22(%rbp) //'k' 
      movb $108, -21(%rbp) //'l' 
      movb $109, -20(%rbp) //'m' 
      movb $110, -19(%rbp) //'n' 
      movb $111, -18(%rbp) //'o' 
1

编译器可能会将每个字符的编号放在每个数组中,就像您编写它时一样,没有通过阅读代码可以找到任何优化。请记住,单个字符与c中的数字没有区别,因此您甚至可以使用ascii代码而不是'a'。我希望你会看到那些转换回来的字母,只是间隔一点。

1

在第一种情况下,编译器用确切的字符串“ABC ...”初始化数据。

在第二种情况下,每个分配都是按顺序完成的,因此编译器会生成代码来执行此分配。在可执行文件中,您应该看到15个重复字节序列,其中只有初始化程序('a','b','c'...)发生更改。

+1

换句话说,字符直接放置在生成的机器码中。 – 2011-04-01 14:35:22

相关问题