History

fix: code in non-executable ELF sections

Jonathan M. Wilbur committed 2 years ago

Commit e4d874d1 parent c85efac4 files · +45 −0Browse files

Unified · Split

4 files
  1. tccasm.c +3 −0
  2. tccgen.c +1 −0
  3. tests/exec_section_in_asm.c +28 −0
  4. tests/exec_section_in_c.c +13 −0
tccasm.c+3−0
@@ -893,6 +893,9 @@ static void asm_parse_directive(TCCState *s1, int global) · 892 unmodified lines
893893 if (old_nb_section != s1->nb_sections)
894894 cur_text_section->sh_addralign = 1;
895895 }
896+ /* The section directive supports flags, but they are unsupported.
897+ For now, just assume any section contains code. */
898+ cur_text_section->sh_flags |= SHF_EXECINSTR;
896899 break;
897900 case TOK_ASMDIR_previous:
898901 {
tccgen.c+1−0
@@ -8303,6 +8303,7 @@ static void gen_function(Sym *sym) · 8302 unmodified lines
83038303 cur_scope = root_scope = &f;
83048304 nocode_wanted = 0;
83058305
8306+ cur_text_section->sh_flags |= SHF_EXECINSTR;
83068307 ind = cur_text_section->data_offset;
83078308 if (sym->a.aligned) {
83088309 size_t newoff = section_add(cur_text_section, 0,
tests/exec_section_in_asm.c+28−0
@@ -0,0 +1,28 @@
1+/* Previously in TinyCC, ELF sections defined in assembly would always have the
2+execute bit not set, so you would get segmentation faults when code in these
3+sections was exectuted. This file is a minimal example of a file that will put
4+the resulting code in a non-executable section (and invoke it) prior to the fix.
5+*/
6+#include <stdio.h>
7+
8+void *memset(void *dst, int c, int len);
9+
10+__asm__ (
11+".section .text.nolibc_memset\n"
12+".weak memset\n"
13+"memset:\n"
14+ "xchgl %eax, %esi\n\t"
15+ "movq %rdx, %rcx\n\t"
16+ "pushq %rdi\n\t"
17+ "rep stosb\n\t"
18+ "popq %rax\n\t"
19+ "retq\n"
20+);
21+
22+int main () {
23+ char buf[10];
24+ memset(&buf[0], 'A', 9);
25+ buf[9] = 0;
26+ puts(buf);
27+ return 0;
28+}
tests/exec_section_in_c.c+13−0
@@ -0,0 +1,13 @@
1+/* Previously in TinyCC, ELF sections defined in attributes would always have
2+the execute bit not set, so you would get segmentation faults when code in these
3+sections was exectuted. This file is a minimal example of a file that will put
4+the resulting code in a non-executable section (and invoke it) prior to the fix.
5+*/
6+__attribute__((section(".text.wumbo")))
7+int wumbo (int arg) {
8+ return arg * 2;
9+}
10+
11+int main () {
12+ return wumbo(2);
13+}

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