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
4040 #include <time.h>
4141 #ifdef WIN32
4242 #include <sys/timeb.h>
43+#include <windows.h>
4344 #endif
4445 #ifndef WIN32
4546 #include <sys/time.h>
4647 #include <sys/ucontext.h>
48+#include <sys/mman.h>
4749 #endif
4850
4951 #endif /* !CONFIG_TCCBOOT */
5052
53+#ifndef PAGESIZE
54+#define PAGESIZE 4096
55+#endif
56+
5157 #include "elf.h"
5258 #include "stab.h"
5359
@@ -199,7 +205,7 @@ typedef struct Section { · 145 unmodified lines
199205 int sh_entsize; /* elf entry size */
200206 unsigned long sh_size; /* section size (only used during output) */
201207 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 */
203209 int nb_hashed_syms; /* used to resize the hash table */
204210 struct Section *link; /* link to another section */
205211 struct Section *reloc; /* corresponding section for relocation, if any */
@@ -9615,6 +9621,30 @@ int tcc_relocate(TCCState *s1) · 9409 unmodified lines
96159621 if (s->reloc)
96169622 relocate_section(s1, s);
96179623 }
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+ }
96189648 return 0;
96199649 }
96209650

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