【问题标题】:Calculate align page of an address statically静态计算地址的对齐页面
【发布时间】:2014-12-20 16:22:37
【问题描述】:

我需要静态计算包含精灵文本段的第一页的地址,以便使用mprotect()并使文本段可写。

Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al .. [14] .text PROGBITS 08048380 000380 0002e0 00 AX 0 0 128

有什么想法吗?

【问题讨论】:

    标签: c linux system elf mprotect


    【解决方案1】:

    这个程序怎么样,编译正常,不会崩溃。

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <unistd.h>
    #include <sys/mman.h>
    
    extern char __executable_start;
    extern char __etext;
    
    int
    main (int argc, char **argv)
    {
      int pagesize = sysconf (_SC_PAGE_SIZE);
      char *start =
        (char *) (((uintptr_t) & __executable_start) & ~(pagesize - 1));
      char *end =
        (char *) (((uintptr_t) & __etext + pagesize - 1) & ~(pagesize - 1));
      mprotect (start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
      printf ("Hello world\n");
      void *m = main;
      *((char *) m) = 0;
      exit (0);
    }
    

    我使用过__executable_start__etext,但您最好看看是否可以让它们工作,至少在手册页中有记录:

    姓名

      `etext`, `edata`, `end` - end of program segments
    

    概要

      extern etext;
      extern edata;
      extern end;
    

    描述

      The addresses of these symbols indicate the end of various program segments:
    
      `etext`  This is the first address past the end of the text segment (the program
               code).
    
      `edata`  This is the first address past the end of the initialized data segment.
    
      `end`    This  is the first address past the end of the uninitialized data
               segment (also known as the BSS segment).
    

    符合

      Although these symbols have long been provided on most UNIX systems, they are
      not standardized; use with caution.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多