Calculate align page of an address statically

373 views Asked by At

i need to calculate statically the address of the first page that contains the text segment of an elf, in order to use mprotect() and make the text segment writable.

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

Any ideas?

1

There are 1 answers

0
abligh On

How about this program, which compiles normally and does not crash.

#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);
}

I've used __executable_start and __etext, but you might be better seeing if you can get these to work, which are at least documented in man pages:

NAME

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

SYNOPSIS

  extern etext;
  extern edata;
  extern end;

DESCRIPTION

  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).

CONFORMING TO

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