2013-07-21 53 views
0

GCC允许您通过&&获取标签的地址。 ICC是否有类似的功能?我一直无法找到任何有关它的文档。ICC中的标签地址

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/mman.h> 
#include <stdint.h> 

int main(int argc, char **argv) 
{ 
    int (*my_printf) (const char *format, ...); 
    void (*my_exit) (int); 
    void *page = (void *) ((unsigned long) (&&checkpoint) & ~(getpagesize() - 1)); 

    /* mark the code section we are going to overwrite           
    * as writable.                    
    */ 
    mprotect(page, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC); 

    /* Use the labels to avoid having GCC               
    * optimize them out */ 
    switch (argc) { 
    case 33: 
    goto checkpoint; 
    case 44: 
    goto newcode; 
    case 55: 
    goto newcode_end; 
    default: 
    break; 
    } 

    /* Replace code in checkpoint with code from             
    * newcode.                     
    */ 
    //memcpy(&&checkpoint, &&newcode, &&newcode_end - &&newcode);         

checkpoint: 
    printf("Good morning!\n"); 
    return 1; 

newcode: 
    my_printf = &printf; 
    (*(my_printf)) ("Good evening\n"); 

    my_exit = &exit; 
    (*(my_exit)) (0); 

newcode_end: 
    return 2; 
} 
+2

如果你还没有,那么它不会。这不是一个标准功能。 – 2013-07-21 18:37:05

+0

@ H2CO3:但是,我记得,ICC试图与gcc紧密相容。 –

回答

0

ICC的Linux版本也支持这一点。相同的语法:

void* ptr = &&some_label; 
+0

hmm,当我尝试使用icc编译时,我正在运行segfaulted的测试程序。使用icc编译时,mprotect或memcpy的工作方式是否有所不同? – user2142343

+0

@ user2142343:通过更新您的问题向我们展示测试程序以包含源代码(假设它不是太长)。 –

+0

@KeithThompson我已经更新了我试图运行的测试程序 – user2142343