History

mark executable sections as executable when running in memory

bellard committed 21 years ago

Commit b0b8ac11 parent 16559cd1 file · +31 −1Browse files

Unified · Split

tcc.c+31−1
@@ -40,14 +40,20 @@ · 39 unmodified lines
40#include <time.h>40#include <time.h>
41#ifdef WIN3241#ifdef WIN32
42#include <sys/timeb.h>42#include <sys/timeb.h>
43#include <windows.h>
43#endif44#endif
44#ifndef WIN3245#ifndef WIN32
45#include <sys/time.h>46#include <sys/time.h>
46#include <sys/ucontext.h>47#include <sys/ucontext.h>
48#include <sys/mman.h>
47#endif49#endif
48 50
49#endif /* !CONFIG_TCCBOOT */51#endif /* !CONFIG_TCCBOOT */
50 52
53#ifndef PAGESIZE
54#define PAGESIZE 4096
55#endif
56
51#include "elf.h"57#include "elf.h"
52#include "stab.h"58#include "stab.h"
53 59
@@ -199,7 +205,7 @@ typedef struct Section { · 145 unmodified lines
199 int sh_entsize; /* elf entry size */205 int sh_entsize; /* elf entry size */
200 unsigned long sh_size; /* section size (only used during output) */206 unsigned long sh_size; /* section size (only used during output) */
201 unsigned long sh_addr; /* address at which the section is relocated */207 unsigned long sh_addr; /* address at which the section is relocated */
202 unsigned long sh_offset; /* address at which the section is relocated */208 unsigned long sh_offset; /* file offset */
203 int nb_hashed_syms; /* used to resize the hash table */209 int nb_hashed_syms; /* used to resize the hash table */
204 struct Section *link; /* link to another section */210 struct Section *link; /* link to another section */
205 struct Section *reloc; /* corresponding section for relocation, if any */211 struct Section *reloc; /* corresponding section for relocation, if any */
@@ -9615,6 +9621,30 @@ int tcc_relocate(TCCState *s1) · 9409 unmodified lines
9615 if (s->reloc)9621 if (s->reloc)
9616 relocate_section(s1, s);9622 relocate_section(s1, s);
9617 }9623 }
9624
9625 /* mark executable sections as executable in memory */
9626 for(i = 1; i < s1->nb_sections; i++) {
9627 s = s1->sections[i];
9628 if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
9629 (SHF_ALLOC | SHF_EXECINSTR)) {
9630#ifdef WIN32
9631 {
9632 DWORD old_protect;
9633 VirtualProtect(s->data, s->data_offset,
9634 PAGE_EXECUTE_READWRITE, &old_protect);
9635 }
9636#else
9637 {
9638 unsigned long start, end;
9639 start = (unsigned long)(s->data) & ~(PAGESIZE - 1);
9640 end = (unsigned long)(s->data + s->data_offset);
9641 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
9642 mprotect((void *)start, end - start,
9643 PROT_READ | PROT_WRITE | PROT_EXEC);
9644 }
9645#endif
9646 }
9647 }
9618 return 0;9648 return 0;
9619}9649}
9620 9650

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t