History

Add support for thread-local storage variables

Thomas Preud'homme committed 13 years ago

Commit 1c4afd11 parent 3b07a155 files · +54 −14Browse files

Unified · Split

5 files
  1. elf.h +3 −1
  2. libtcc.c +4 −1
  3. tccelf.c +21 −8
  4. tccgen.c +25 −4
  5. tcctok.h +1 −0
elf.h+3−1
@@ -447,6 +447,7 @@ typedef struct · 446 unmodified lines
447447 #define STT_SECTION 3 /* Symbol associated with a section */
448448 #define STT_FILE 4 /* Symbol's name is file name */
449449 #define STT_NUM 5 /* Number of defined types. */
450+#define STT_TLS 6 /* Symbol is a thread-local data object */
450451 #define STT_GNU_IFUNC 10 /* Symbol is a indirect code object */
451452 #define STT_LOOS 11 /* Start of OS-specific */
452453 #define STT_HIOS 12 /* End of OS-specific */
@@ -555,7 +556,8 @@ typedef struct · 102 unmodified lines
555556 #define PT_NOTE 4 /* Auxiliary information */
556557 #define PT_SHLIB 5 /* Reserved */
557558 #define PT_PHDR 6 /* Entry for header table itself */
558−#define PT_NUM 7 /* Number of defined types. */
559+#define PT_TLS 7 /* Thread-local program segment */
560+#define PT_NUM 8 /* Number of defined types. */
559561 #define PT_LOOS 0x60000000 /* Start of OS-specific */
560562 #define PT_HIOS 0x6fffffff /* End of OS-specific */
561563 #define PT_LOPROC 0x70000000 /* Start of processor-specific */
libtcc.c+4−1
@@ -444,7 +444,10 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, · 443 unmodified lines
444444 } else if ((sym->type.t & VT_BTYPE) == VT_VOID) {
445445 sym_type = STT_NOTYPE;
446446 } else {
447− sym_type = STT_OBJECT;
447+ if (section && section->sh_flags & SHF_TLS)
448+ sym_type = STT_TLS;
449+ else
450+ sym_type = STT_OBJECT;
448451 }
449452
450453 if (sym->type.t & VT_STATIC)
tccelf.c+21−8
@@ -1543,6 +1543,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 1542 unmodified lines
15431543 int fd, mode, ret;
15441544 int *section_order;
15451545 int shnum, i, phnum, file_offset, offset, size, j, sh_order_index, k;
1546+ int have_tls_section = 0;
15461547 long long tmp;
15471548 addr_t addr;
15481549 Section *strsec, *s;
@@ -1861,6 +1862,11 @@ static int elf_output_file(TCCState *s1, const char *filename) · 312 unmodified lines
18611862 /* we output all sections if debug or object file */
18621863 s->sh_size = s->data_offset;
18631864 }
1865+ /* if tls section we'll need to add one segment */
1866+ if (s->sh_flags & SHF_TLS) {
1867+ have_tls_section = 1;
1868+ phnum++;
1869+ }
18641870 }
18651871
18661872 /* allocate program segment headers */
@@ -1904,12 +1910,16 @@ static int elf_output_file(TCCState *s1, const char *filename) · 37 unmodified lines
19041910 if (interp)
19051911 ph += 1 + HAVE_PHDR;
19061912
1907− for(j = 0; j < 2; j++) {
1908− ph->p_type = PT_LOAD;
1909− if (j == 0)
1910− ph->p_flags = PF_R | PF_X;
1913+ for(j = 0; j < 2 + have_tls_section; j++) {
1914+ if (j != 2)
1915+ ph->p_type = PT_LOAD;
19111916 else
1912− ph->p_flags = PF_R | PF_W;
1917+ ph->p_type = PT_TLS;
1918+ ph->p_flags = PF_R;
1919+ if (j == 0)
1920+ ph->p_flags |= PF_X;
1921+ else if (j == 1)
1922+ ph->p_flags |= PF_W;
19131923 ph->p_align = s1->section_align;
19141924
19151925 /* we do the following ordering: interp, symbol tables,
@@ -1920,13 +1930,16 @@ static int elf_output_file(TCCState *s1, const char *filename) · 4 unmodified lines
19201930 s = s1->sections[i];
19211931 /* compute if section should be included */
19221932 if (j == 0) {
1923− if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1933+ if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_TLS)) !=
19241934 SHF_ALLOC)
19251935 continue;
1926− } else {
1927− if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1936+ } else if (j == 1) {
1937+ if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_TLS)) !=
19281938 (SHF_ALLOC | SHF_WRITE))
19291939 continue;
1940+ } else {
1941+ if ((s->sh_flags & SHF_TLS) != SHF_TLS)
1942+ continue;
19301943 }
19311944 if (s == interp) {
19321945 if (k != 0)
tccgen.c+25−4
@@ -31,6 +31,7 @@ · 30 unmodified lines
3131 ST_DATA int rsym, anon_sym, ind, loc;
3232
3333 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
34+ST_DATA Section *tdata_section, *tbss_section; /* thread-local storage sections */
3435 ST_DATA Section *cur_text_section; /* current section where function code is generated */
3536 #ifdef CONFIG_TCC_ASM
3637 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
@@ -3092,6 +3093,10 @@ static int parse_btype(CType *type, AttributeDef *ad) · 3055 unmodified lines
30923093 t |= VT_INLINE;
30933094 next();
30943095 break;
3096+ case TOK_THREAD:
3097+ t |= VT_TLS;
3098+ next();
3099+ break;
30953100
30963101 /* GNUC attribute */
30973102 case TOK_ATTRIBUTE1:
@@ -5495,10 +5500,26 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 2397 unmodified lines
54955500 /* allocate symbol in corresponding section */
54965501 sec = ad->section;
54975502 if (!sec) {
5498− if (has_init)
5499− sec = data_section;
5500− else if (tcc_state->nocommon)
5501− sec = bss_section;
5503+ if (has_init) {
5504+ if (type->t & VT_TLS) {
5505+ if (!tdata_section)
5506+ tdata_section = new_section(tcc_state, ".tdata",
5507+ SHT_PROGBITS,
5508+ SHF_ALLOC | SHF_WRITE | SHF_TLS);
5509+ sec = tdata_section;
5510+ } else
5511+ sec = data_section;
5512+ }
5513+ else if (tcc_state->nocommon) {
5514+ if (type->t & VT_TLS) {
5515+ if (!tbss_section)
5516+ tbss_section = new_section(tcc_state, ".tbss",
5517+ SHT_NOBITS,
5518+ SHF_ALLOC | SHF_WRITE | SHF_TLS);
5519+ sec = tbss_section;
5520+ } else
5521+ sec = bss_section;
5522+ }
55025523 }
55035524 if (sec) {
55045525 data_offset = sec->data_offset;
tcctok.h+1−0
@@ -10,6 +10,7 @@ · 9 unmodified lines
1010 DEF(TOK_FOR, "for")
1111 DEF(TOK_EXTERN, "extern")
1212 DEF(TOK_STATIC, "static")
13+ DEF(TOK_THREAD, "__thread")
1314 DEF(TOK_UNSIGNED, "unsigned")
1415 DEF(TOK_GOTO, "goto")
1516 DEF(TOK_DO, "do")

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