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
893 if (old_nb_section != s1->nb_sections)893 if (old_nb_section != s1->nb_sections)
894 cur_text_section->sh_addralign = 1;894 cur_text_section->sh_addralign = 1;
895 }895 }
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;
896 break;899 break;
897 case TOK_ASMDIR_previous:900 case TOK_ASMDIR_previous:
898 { 901 {
tccgen.c+1−0
@@ -8303,6 +8303,7 @@ static void gen_function(Sym *sym) · 8302 unmodified lines
8303 cur_scope = root_scope = &f;8303 cur_scope = root_scope = &f;
8304 nocode_wanted = 0;8304 nocode_wanted = 0;
8305 8305
8306 cur_text_section->sh_flags |= SHF_EXECINSTR;
8306 ind = cur_text_section->data_offset;8307 ind = cur_text_section->data_offset;
8307 if (sym->a.aligned) {8308 if (sym->a.aligned) {
8308 size_t newoff = section_add(cur_text_section, 0,8309 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
2execute bit not set, so you would get segmentation faults when code in these
3sections was exectuted. This file is a minimal example of a file that will put
4the resulting code in a non-executable section (and invoke it) prior to the fix.
5*/
6#include <stdio.h>
7
8void *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
22int 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
2the execute bit not set, so you would get segmentation faults when code in these
3sections was exectuted. This file is a minimal example of a file that will put
4the resulting code in a non-executable section (and invoke it) prior to the fix.
5*/
6__attribute__((section(".text.wumbo")))
7int wumbo (int arg) {
8 return arg * 2;
9}
10
11int 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