History

tccelf: cleanup sort_sections() & etc. fixes

with -Wl,-oformat=binary, executable code should come first. (for linux kernel image for example)

grischka committed 1 year ago

Commit dd2e5f81 parent 3eb63529 files · +141 −125Browse files

Unified · Split

9 files
  1. lib/Makefile +36 −35
  2. libtcc.c +18 −16
  3. tcc.c +5 −5
  4. tcc.h +7 −6
  5. tccdbg.c +6 −5
  6. tccelf.c +51 −45
  7. tccgen.c +2 −0
  8. tccpe.c +4 −6
  9. tccpp.c +12 −7
lib/Makefile+36−35
@@ -7,6 +7,8 @@ include $(TOP)/Makefile · 6 unmodified lines
77 VPATH = $(TOPSRC)/lib $(TOPSRC)/win32/lib
88 T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
99 X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
10+XCFG = $(or $(findstring -win,$T),-unx)
11+S = $(if $(findstring yes,$(SILENT)),@$(info * $@))
1012
1113 TCC = $(TOP)/$(X)tcc$(EXESUF)
1214 XTCC ?= $(TOP)/$(X)tcc$(EXESUF)
@@ -16,8 +18,6 @@ XFLAGS-unx = -B$(TOPSRC) · 3 unmodified lines
1618 XFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include
1719 XFLAGS = $(XFLAGS$(XCFG)) -I$(TOP)
1820 BFLAGS = -bt
19−XCFG = $(or $(findstring -win,$T),-unx)
20−S = $(if $(findstring yes,$(SILENT)),@$(info * $@))
2121
2222 # in order to use gcc, type: make <target>-libtcc1-usegcc=yes
2323 arm-libtcc1-usegcc ?= no
@@ -29,47 +29,48 @@ arm-libtcc1-usegcc ?= no · 5 unmodified lines
2929 ifeq "$($(T)-libtcc1-usegcc)" "yes"
3030 XCC = $(CC)
3131 XAR = $(AR)
32− XFLAGS = $(CFLAGS) -fPIC -gdwarf -fno-omit-frame-pointer -Wno-unused-function -Wno-unused-variable
33− BFLAGS = -g
32+ XFLAGS = $(CFLAGS) -fPIC -fno-omit-frame-pointer -Wno-unused-function -Wno-unused-variable
33+ BFLAGS = $(if $(CONFIG_dwarf),-gdwarf,-gstabs)
3434 endif
3535
36−ifneq ($(CONFIG_backtrace),no)
37−# only for native compiler
38−ifneq ($(CONFIG_bcheck),no)
39−$(X)BCHECK_O = bcheck.o
40−endif
41−$(X)BT_O = bt-exe.o bt-log.o
42−$(X)B_O = $(BCHECK_O) bt-exe.o bt-log.o bt-dll.o
43−endif
44−$(X)BT_O += runmain.o tcov.o
36+I386_O = libtcc1.o alloca.o alloca-bt.o $(COMMON_O)
37+X86_64_O = libtcc1.o alloca.o alloca-bt.o $(COMMON_O)
38+ARM_O = libtcc1.o armeabi.o alloca.o armflush.o $(COMMON_O)
39+ARM64_O = lib-arm64.o $(COMMON_O)
40+RISCV64_O = lib-arm64.o $(COMMON_O)
41+COMMON_O = stdatomic.o atomic.o builtin.o
42+WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o
43+LIN_O = dsohandle.o
44+OSX_O =
45+
46+# backtrace/bcheck/run only for native compiler
47+Nat = $(if $X,no,)
48+Cbt = $(Nat)$(subst yes,,$(CONFIG_backtrace))
49+Cbc = $(Cbt)$(subst yes,,$(CONFIG_bcheck))
4550
46−DSO_O = dsohandle.o
51+$(Nat)COMMON_O += runmain.o tcov.o
52+$(Cbt)COMMON_O += bt-exe.o bt-log.o
53+$(Cbt)WIN_O += bt-dll.o
54+$(Cbc)COMMON_O += bcheck.o
4755
4856 # not in libtcc1.a
4957 EXTRA_O = runmain.o bt-exe.o bt-dll.o bt-log.o bcheck.o
5058
51−I386_O = libtcc1.o alloca.o alloca-bt.o stdatomic.o atomic.o builtin.o $(BT_O)
52−X86_64_O = libtcc1.o alloca.o alloca-bt.o stdatomic.o atomic.o builtin.o $(BT_O)
53−ARM_O = libtcc1.o armeabi.o alloca.o armflush.o stdatomic.o atomic.o builtin.o $(BT_O)
54−ARM64_O = lib-arm64.o stdatomic.o atomic.o builtin.o $(BT_O)
55−RISCV64_O = lib-arm64.o stdatomic.o atomic.o builtin.o $(BT_O)
56−WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o
57−
58−OBJ-i386 = $(I386_O) $(BCHECK_O) $(DSO_O)
59−OBJ-x86_64 = $(X86_64_O) va_list.o $(BCHECK_O) $(DSO_O)
60−OBJ-x86_64-osx = $(X86_64_O) va_list.o $(BCHECK_O)
61−OBJ-i386-win32 = $(I386_O) chkstk.o $(B_O) $(WIN_O)
62−OBJ-x86_64-win32 = $(X86_64_O) chkstk.o $(B_O) $(WIN_O)
63−OBJ-arm64 = $(ARM64_O) $(BCHECK_O) $(DSO_O)
64−OBJ-arm64-osx = $(ARM64_O) $(BCHECK_O)
65−OBJ-arm = $(ARM_O) $(BCHECK_O) $(DSO_O)
66−OBJ-arm-fpa = $(ARM_O) $(DSO_O)
67−OBJ-arm-fpa-ld = $(ARM_O) $(DSO_O)
68−OBJ-arm-vfp = $(ARM_O) $(DSO_O)
69−OBJ-arm-eabi = $(ARM_O) $(DSO_O)
70−OBJ-arm-eabihf = $(ARM_O) $(DSO_O)
59+OBJ-i386 = $(I386_O) $(LIN_O)
60+OBJ-x86_64 = $(X86_64_O) va_list.o $(LIN_O)
61+OBJ-x86_64-osx = $(X86_64_O) va_list.o $(OSX_O)
62+OBJ-i386-win32 = $(I386_O) chkstk.o $(WIN_O)
63+OBJ-x86_64-win32 = $(X86_64_O) chkstk.o $(WIN_O)
64+OBJ-arm64 = $(ARM64_O) $(LIN_O)
65+OBJ-arm64-osx = $(ARM64_O) $(OSX_O)
66+OBJ-arm = $(ARM_O) $(LIN_O)
67+OBJ-arm-fpa = $(OBJ-arm)
68+OBJ-arm-fpa-ld = $(OBJ-arm)
69+OBJ-arm-vfp = $(OBJ-arm)
70+OBJ-arm-eabi = $(OBJ-arm)
71+OBJ-arm-eabihf = $(OBJ-arm)
7172 OBJ-arm-wince = $(ARM_O) $(WIN_O)
72−OBJ-riscv64 = $(RISCV64_O) $(BCHECK_O) $(DSO_O)
73+OBJ-riscv64 = $(RISCV64_O) $(LIN_O)
7374
7475 OBJ-extra = $(filter $(EXTRA_O),$(OBJ-$T))
7576 OBJ-libtcc1 = $(addprefix $(X),$(filter-out $(OBJ-extra),$(OBJ-$T)))
libtcc.c+18−16
@@ -1004,6 +1004,8 @@ static int tcc_glob_so(TCCState *s1, const char *pattern, char *buf, int size) · 1003 unmodified lines
10041004 }
10051005 #endif
10061006
1007+static int guess_filetype(const char *filename);
1008+
10071009 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10081010 {
10091011 int fd, ret = -1;
@@ -1014,6 +1016,9 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 4 unmodified lines
10141016 filename = buf;
10151017 #endif
10161018
1019+ if (0 == (flags & AFF_TYPE_MASK))
1020+ flags |= guess_filetype(filename);
1021+
10171022 /* ignore binary files with -E */
10181023 if (s1->output_type == TCC_OUTPUT_PREPROCESS
10191024 && (flags & AFF_TYPE_BIN))
@@ -1123,10 +1128,10 @@ check_success: · 103 unmodified lines
11231128 return ret;
11241129 }
11251130
1126−LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1131+static int guess_filetype(const char *filename)
11271132 {
1128− int filetype = s->filetype;
1129− if (0 == (filetype & AFF_TYPE_MASK)) {
1133+ int filetype = 0;
1134+ if (1) {
11301135 /* use a file extension to detect a filetype */
11311136 const char *ext = tcc_fileextension(filename);
11321137 if (ext[0]) {
@@ -1145,7 +1150,12 @@ LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename) · 12 unmodified lines
11451150 filetype = AFF_TYPE_C;
11461151 }
11471152 }
1148− return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
1153+ return filetype;
1154+}
1155+
1156+LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1157+{
1158+ return tcc_add_file_internal(s, filename, s->filetype | AFF_PRINT_ERROR);
11491159 }
11501160
11511161 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
@@ -1162,12 +1172,12 @@ static int tcc_add_library_internal(TCCState *s1, const char *fmt, · 10 unmodified lines
11621172
11631173 for(i = 0; i < nb_paths; i++) {
11641174 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1165− ret = tcc_add_file_internal(s1, buf, (flags & ~AFF_PRINT_ERROR) | AFF_TYPE_BIN);
1175+ ret = tcc_add_file_internal(s1, buf, flags & ~AFF_PRINT_ERROR);
11661176 if (ret != FILE_NOT_FOUND)
11671177 return ret;
11681178 }
11691179 if (flags & AFF_PRINT_ERROR)
1170− tcc_error_noabort("file '%s' not found", filename);
1180+ tcc_error_noabort("library '%s' not found", filename);
11711181 return FILE_NOT_FOUND;
11721182 }
11731183
@@ -1219,15 +1229,7 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) · 45 unmodified lines
12191229 return ret;
12201230 ++pp;
12211231 }
1222− return FILE_NOT_FOUND;
1223−}
1224−
1225−PUB_FUNC int tcc_add_library_err(TCCState *s1, const char *libname)
1226−{
1227− int ret = tcc_add_library(s1, libname);
1228− if (ret == FILE_NOT_FOUND)
1229− tcc_error_noabort("library '%s' not found", libname);
1230− return ret;
1232+ return tcc_add_dll(s, libraryname, AFF_PRINT_ERROR);
12311233 }
12321234
12331235 /* handle #pragma comment(lib,) */
@@ -1235,7 +1237,7 @@ ST_FUNC void tcc_add_pragma_libs(TCCState *s1) · 1 unmodified line
12351237 {
12361238 int i;
12371239 for (i = 0; i < s1->nb_pragma_libs; i++)
1238− tcc_add_library_err(s1, s1->pragma_libs[i]);
1240+ tcc_add_library(s1, s1->pragma_libs[i]);
12391241 }
12401242
12411243 LIBTCCAPI int tcc_add_symbol(TCCState *s1, const char *name, const void *val)
tcc.c+5−5
@@ -366,7 +366,7 @@ redo: · 365 unmodified lines
366366 struct filespec *f = s->files[n];
367367 s->filetype = f->type;
368368 if (f->type & AFF_TYPE_LIB) {
369− ret = tcc_add_library_err(s, f->name);
369+ ret = tcc_add_library(s, f->name);
370370 } else {
371371 if (1 == s->verbose)
372372 printf("-> %s\n", f->name);
@@ -392,9 +392,9 @@ redo: · 19 unmodified lines
392392 } else {
393393 if (!s->outfile)
394394 s->outfile = default_outputfile(s, first_file);
395− if (!s->just_deps && tcc_output_file(s, s->outfile))
396− ;
397− else if (s->gen_deps)
395+ if (!s->just_deps)
396+ ret = tcc_output_file(s, s->outfile);
397+ if (!ret && s->gen_deps)
398398 gen_makedeps(s, s->outfile, s->deps_outfile);
399399 }
400400 }
@@ -402,7 +402,7 @@ redo: · 1 unmodified line
402402 done = 1;
403403 if (t)
404404 done = 0; /* run more tests with -dt -run */
405− else if (s->nb_errors)
405+ else if (ret)
406406 ret = 1;
407407 else if (n < s->nb_files)
408408 done = 0; /* compile more files with -c */
tcc.h+7−6
@@ -974,8 +974,6 @@ struct TCCState { · 973 unmodified lines
974974 int uw_sym;
975975 unsigned uw_offs;
976976 # endif
977−#else
978− unsigned shf_RELRO; /* section flags for RELRO sections */
979977 #endif
980978
981979 #if defined TCC_TARGET_MACHO
@@ -1874,10 +1872,6 @@ ST_FUNC int gen_makedeps(TCCState *s, const char *target, const char *filename); · 892 unmodified lines
18741872
18751873 /* ------------ tccdbg.c ------------ */
18761874
1877−ST_FUNC void tcc_eh_frame_start(TCCState *s1);
1878−ST_FUNC void tcc_eh_frame_end(TCCState *s1);
1879−ST_FUNC void tcc_eh_frame_hdr(TCCState *s1, int final);
1880−
18811875 ST_FUNC void tcc_debug_new(TCCState *s);
18821876
18831877 ST_FUNC void tcc_debug_start(TCCState *s1);
@@ -1896,6 +1890,13 @@ ST_FUNC void tcc_debug_typedef(TCCState *s1, Sym *sym); · 12 unmodified lines
18961890 ST_FUNC void tcc_debug_stabn(TCCState *s1, int type, int value);
18971891 ST_FUNC void tcc_debug_fix_anon(TCCState *s1, CType *t);
18981892
1893+#if !(defined ELF_OBJ_ONLY || defined TCC_TARGET_ARM || defined TARGETOS_BSD)
1894+ST_FUNC void tcc_eh_frame_start(TCCState *s1);
1895+ST_FUNC void tcc_eh_frame_end(TCCState *s1);
1896+ST_FUNC void tcc_eh_frame_hdr(TCCState *s1, int final);
1897+#define TCC_EH_FRAME 1
1898+#endif
1899+
18991900 ST_FUNC void tcc_tcov_start(TCCState *s1);
19001901 ST_FUNC void tcc_tcov_end(TCCState *s1);
19011902 ST_FUNC void tcc_tcov_check_line(TCCState *s1, int start);
tccdbg.c+6−5
@@ -424,7 +424,7 @@ ST_FUNC void tcc_debug_new(TCCState *s1) · 423 unmodified lines
424424 if (s1->do_debug && s1->output_type == TCC_OUTPUT_MEMORY)
425425 s1->do_backtrace = 1;
426426 if (s1->do_backtrace)
427− shf = SHF_ALLOC | SHF_WRITE; // SHF_WRITE needed for musl/SELINUX
427+ shf = SHF_ALLOC; /* have debug data available at runtime */
428428 #endif
429429
430430 if (s1->dwarf) {
@@ -511,7 +511,7 @@ static void put_stabn(TCCState *s1, int type, int other, int desc, int value) · 80 unmodified lines
511511
512512 /* ------------------------------------------------------------------------- */
513513 #define dwarf_data1(s,data) \
514− { unsigned char *p = section_ptr_add((s), 1); *p = (data); }
514+ (*(uint8_t*)section_ptr_add((s), 1) = (data))
515515 #define dwarf_data2(s,data) \
516516 write16le(section_ptr_add((s), 2), (data))
517517 #define dwarf_data4(s,data) \
@@ -712,10 +712,9 @@ static void dwarf_sleb128_op (TCCState *s1, long long value) · 194 unmodified lines
712712 } while (more);
713713 }
714714
715+#if TCC_EH_FRAME
715716 ST_FUNC void tcc_eh_frame_start(TCCState *s1)
716717 {
717−#if !(defined _WIN32 || defined __APPLE__ || defined TCC_TARGET_ARM || \
718− defined TARGETOS_BSD)
719718 if (!s1->unwind_tables)
720719 return;
721720 eh_frame_section = new_section(s1, ".eh_frame", SHT_PROGBITS, SHF_ALLOC);
@@ -784,7 +783,6 @@ ST_FUNC void tcc_eh_frame_start(TCCState *s1) · 62 unmodified lines
784783 dwarf_data1(eh_frame_section, DW_CFA_nop);
785784 write32le(eh_frame_section->data + s1->eh_start, // length
786785 eh_frame_section->data_offset - s1->eh_start - 4);
787−#endif
788786 }
789787
790788 static void tcc_debug_frame_end(TCCState *s1, int size)
@@ -1006,6 +1004,7 @@ next: · 215 unmodified lines
10061004 qsort(eh_frame_hdr_section->data + tab_offset, count,
10071005 sizeof(struct eh_search_table), sort_eh_table);
10081006 }
1007+#endif
10091008
10101009 /* start of translation unit info */
10111010 ST_FUNC void tcc_debug_start(TCCState *s1)
@@ -2172,7 +2171,9 @@ ST_FUNC void tcc_debug_funcend(TCCState *s1, int size) · 1160 unmodified lines
21722171 /* lldb does not like function end and next function start at same pc */
21732172 int min_instr_len;
21742173
2174+#if TCC_EH_FRAME
21752175 tcc_debug_frame_end(s1, size);
2176+#endif
21762177 if (!s1->do_debug)
21772178 return;
21782179 min_instr_len = dwarf_line.last_pc == ind ? 0 : DWARF_MIN_INSTR_LEN;
tccelf.c+51−45
@@ -51,7 +51,7 @@ struct sym_version { · 50 unmodified lines
5151 #define shf_RELRO SHF_ALLOC
5252 static const char rdata[] = ".rdata";
5353 #else
54−#define shf_RELRO s1->shf_RELRO
54+#define shf_RELRO SHF_ALLOC /* eventually made SHF_WRITE in sort_sections() */
5555 static const char rdata[] = ".data.ro";
5656 #endif
5757
@@ -61,12 +61,6 @@ ST_FUNC void tccelf_new(TCCState *s) · 3 unmodified lines
6161 {
6262 TCCState *s1 = s;
6363
64−#ifndef TCC_TARGET_PE
65− shf_RELRO = SHF_ALLOC;
66− if (s1->output_type != TCC_OUTPUT_MEMORY)
67− shf_RELRO |= SHF_WRITE; /* the ELF loader will set it to RO at runtime */
68−#endif
69−
7064 /* no section zero */
7165 dynarray_add(&s->sections, &s->nb_sections, NULL);
7266
@@ -95,7 +89,9 @@ ST_FUNC void tccelf_new(TCCState *s) · 22 unmodified lines
9589 tcc_debug_new(s);
9690 }
9791
92+#if TCC_EH_FRAME
9893 tcc_eh_frame_start(s);
94+#endif
9995
10096 #ifdef CONFIG_TCC_BCHECK
10197 if (s->do_bounds_check) {
@@ -1126,8 +1122,8 @@ static void relocate_section(TCCState *s1, Section *s, Section *sr) · 1024 unmodified lines
11261122 r = 0; /* cannot apply 64bit relocation to 32bit value */
11271123 sr->data_offset = sr->sh_size = r;
11281124 #ifdef CONFIG_TCC_PIE
1129− if (r && 0 == (s->sh_flags & SHF_WRITE))
1130− tcc_warning("%d relocations to ro-section %s", (unsigned)(r / sizeof *qrel), s->name);
1125+ if (r && (s->sh_flags & SHF_EXECINSTR))
1126+ tcc_warning("%d relocations to %s", (unsigned)(r / sizeof *qrel), s->name);
11311127 #endif
11321128 }
11331129 }
@@ -1758,7 +1754,7 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) · 624 unmodified lines
17581754 if (s1->do_bounds_check && s1->output_type != TCC_OUTPUT_DLL) {
17591755 tcc_add_support(s1, "bcheck.o");
17601756 # if !(TARGETOS_OpenBSD || TARGETOS_NetBSD)
1761− tcc_add_library_err(s1, "dl");
1757+ tcc_add_library(s1, "dl");
17621758 # endif
17631759 lpthread = 1;
17641760 }
@@ -1774,8 +1770,8 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) · 9 unmodified lines
17741770 }
17751771 #endif
17761772 if (lpthread)
1777− tcc_add_library_err(s1, "pthread");
1778− tcc_add_library_err(s1, "c");
1773+ tcc_add_library(s1, "pthread");
1774+ tcc_add_library(s1, "c");
17791775 #ifdef TCC_LIBGCC
17801776 if (!s1->static_link) {
17811777 if (TCC_LIBGCC[0] == '/')
@@ -1785,7 +1781,7 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) · 3 unmodified lines
17851781 }
17861782 #endif
17871783 #if defined TCC_TARGET_ARM && TARGETOS_FreeBSD
1788− tcc_add_library_err(s1, "gcc_s"); // unwind code
1784+ tcc_add_library(s1, "gcc_s"); // unwind code
17891785 #endif
17901786 if (TCC_LIBTCC1[0])
17911787 tcc_add_support(s1, TCC_LIBTCC1);
@@ -2091,7 +2087,7 @@ static int set_sec_sizes(TCCState *s1) · 299 unmodified lines
20912087 /* allocate the section */
20922088 s->sh_flags |= SHF_ALLOC;
20932089 s->sh_size = count * sizeof(ElfW_Rel);
2094− if (!(s1->sections[s->sh_info]->sh_flags & SHF_WRITE))
2090+ if (s1->sections[s->sh_info]->sh_flags & SHF_EXECINSTR)
20952091 textrel += count;
20962092 }
20972093 }
@@ -2161,47 +2157,58 @@ static int sort_sections(TCCState *s1, int *sec_order, struct dyn_inf *d) · 63 unmodified lines
21612157 } else {
21622158 j = 0x700;
21632159 }
2160+ if (s1->output_format != TCC_OUTPUT_FORMAT_ELF) {
2161+ if (j >= 0x700 || 0 == strncmp(s->name, ".eh_frame", 9))
2162+ s->sh_size = 0, j = 0x900;
2163+ }
2164+
21642165 if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_DYNSYM) {
21652166 k = 0x10;
21662167 } else if (s->sh_type == SHT_STRTAB && strcmp(s->name, ".stabstr")) {
21672168 k = 0x11;
2168− if (i == nb_sections - 1) /* ".shstrtab" assumed to remain last */
2169+ if (i == nb_sections - 1) /* ".shstrtab" assumed to stay last */
21692170 k = 0xff;
21702171 } else if (s->sh_type == SHT_HASH || s->sh_type == SHT_GNU_HASH) {
21712172 k = 0x12;
2173+ } else if (s->sh_type == SHT_GNU_verdef
2174+ || s->sh_type == SHT_GNU_verneed
2175+ || s->sh_type == SHT_GNU_versym) {
2176+ k = 0x13;
21722177 } else if (s->sh_type == SHT_RELX) {
21732178 k = 0x20;
21742179 if (s1->plt && s == s1->plt->reloc)
21752180 k = 0x21;
2181+ } else if (s->sh_flags & SHF_EXECINSTR) {
2182+ k = 0x30;
2183+ /* RELRO sections --> */
21762184 } else if (s->sh_type == SHT_PREINIT_ARRAY) {
21772185 k = 0x41;
21782186 } else if (s->sh_type == SHT_INIT_ARRAY) {
21792187 k = 0x42;
21802188 } else if (s->sh_type == SHT_FINI_ARRAY) {
21812189 k = 0x43;
2182−#ifdef CONFIG_TCC_BCHECK
2183− } else if (s == bounds_section || s == lbounds_section) {
2184− k = 0x44;
2185−#endif
2186− } else if (s == rodata_section || 0 == strcmp(s->name, ".data.rel.ro")) {
2187− k = 0x45;
21882190 } else if (s->sh_type == SHT_DYNAMIC) {
21892191 k = 0x46;
21902192 } else if (s == s1->got) {
21912193 k = 0x47; /* .got as RELRO needs BIND_NOW in DT_FLAGS */
2194+ } else if (s->reloc && (s->reloc->sh_flags & SHF_ALLOC) && j == 0x100) {
2195+ k = 0x44;
2196+ /* <-- */
2197+ } else if (s->sh_type == SHT_NOTE) {
2198+ k = 0x60;
2199+ } else if (s->sh_type == SHT_NOBITS) {
2200+ k = 0x70; /* bss */
2201+ } else if (s == d->interp) {
2202+ k = 0x00;
21922203 } else {
2193− k = 0x50;
2194− if (s->sh_type == SHT_NOTE)
2195− k = 0x60;
2196− if (s->sh_flags & SHF_EXECINSTR)
2197− k = 0x70;
2198− if (s->sh_type == SHT_NOBITS)
2199− k = 0x80;
2200− if (s == d->interp)
2201− k = 0x00;
2204+ k = 0x50; /* data */
22022205 }
22032206 k += j;
22042207
2208+ if ((k & 0xfff0) == 0x140) {
2209+ /* make RELRO section writable */
2210+ k += 0x100, s->sh_flags |= SHF_WRITE;
2211+ }
22052212 for (n = i; n > 1 && k < (f = sec_cls[n - 1]); --n)
22062213 sec_cls[n] = f, sec_order[n] = sec_order[n - 1];
22072214 sec_cls[n] = k, sec_order[n] = i;
@@ -2233,7 +2240,7 @@ static int sort_sections(TCCState *s1, int *sec_order, struct dyn_inf *d) · 25 unmodified lines
22332240 f0 = f, ++n, f |= 1<<8;
22342241 }
22352242 sec_cls[i] = f;
2236− //printf("ph %d sec %02d : %3X %3X %8.2X %04X %s\n", (f>0) * n, i, f, k, s->sh_type, s->sh_size, s->name);
2243+ //printf("ph %d sec %02d : %3X %3X %8.2X %04X %s\n", (f>0) * n, i, f, k, s->sh_type, (int)s->sh_size, s->name);
22372244 }
22382245 return n;
22392246 }
@@ -2395,12 +2402,8 @@ static int layout_sections(TCCState *s1, int *sec_order, struct dyn_inf *d) · 155 unmodified lines
23952402 fill_phdr(++ph, PT_NOTE, d->note);
23962403 if (d->dynamic)
23972404 fill_phdr(++ph, PT_DYNAMIC, d->dynamic)->p_flags |= PF_W;
2398− if (eh_frame_hdr_section) {
2399− add32le(eh_frame_hdr_section->data + 4,
2400− eh_frame_section->sh_offset -
2401− eh_frame_hdr_section->sh_offset - 4);
2405+ if (eh_frame_hdr_section)
24022406 fill_phdr(++ph, PT_GNU_EH_FRAME, eh_frame_hdr_section);
2403− }
24042407 if (d->roinf)
24052408 fill_phdr(++ph, PT_GNU_RELRO, d->roinf)->p_flags |= PF_W;
24062409 if (d->interp)
@@ -2869,10 +2872,10 @@ static int elf_output_file(TCCState *s1, const char *filename) · 462 unmodified lines
28692872 /* shared library case: simply export all global symbols */
28702873 export_global_syms(s1);
28712874 }
2872−
2875+#if TCC_EH_FRAME
28732876 /* fill with initial data */
28742877 tcc_eh_frame_hdr(s1, 0);
2875−
2878+#endif
28762879 dyninf.gnu_hash = create_gnu_hash(s1);
28772880 } else {
28782881 build_got_entries(s1, 0);
@@ -2956,10 +2959,10 @@ static int elf_output_file(TCCState *s1, const char *filename) · 77 unmodified lines
29562959 update_gnu_hash(s1, dyninf.gnu_hash);
29572960
29582961 reorder_sections(s1, sec_order);
2959−
2962+#if TCC_EH_FRAME
29602963 /* fill with final data */
29612964 tcc_eh_frame_hdr(s1, 1);
2962−
2965+#endif
29632966 /* Create the ELF file with name 'filename' */
29642967 ret = tcc_write_elf_file(s1, filename, dyninf.phnum, dyninf.phdr);
29652968 the_end:
@@ -3146,6 +3149,11 @@ invalid: · 180 unmodified lines
31463149 if (sh->sh_type == SHT_RELX)
31473150 sh = &shdr[sh->sh_info];
31483151 /* ignore sections types we do not handle (plus relocs to those) */
3152+ if (0 == strncmp(strsec + sh->sh_name, ".debug_", 7)
3153+ || 0 == strncmp(strsec + sh->sh_name, ".stab", 5)) {
3154+ if (!s1->do_debug || seencompressed)
3155+ continue;
3156+ } else
31493157 if (sh->sh_type != SHT_PROGBITS &&
31503158 #ifdef TCC_ARM_EABI
31513159 sh->sh_type != SHT_ARM_EXIDX &&
@@ -3157,12 +3165,9 @@ invalid: · 5 unmodified lines
31573165 sh->sh_type != SHT_NOBITS &&
31583166 sh->sh_type != SHT_PREINIT_ARRAY &&
31593167 sh->sh_type != SHT_INIT_ARRAY &&
3160− sh->sh_type != SHT_FINI_ARRAY &&
3161− strcmp(strsec + sh->sh_name, ".stabstr")
3168+ sh->sh_type != SHT_FINI_ARRAY
31623169 )
31633170 continue;
3164− if (seencompressed && 0 == strncmp(strsec + sh->sh_name, ".debug_", 7))
3165− continue;
31663171
31673172 sh = &shdr[i];
31683173 sh_name = strsec + sh->sh_name;
@@ -3414,7 +3419,7 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize) · 245 unmodified lines
34143419
34153420 data = tcc_malloc(size);
34163421 if (full_read(fd, data, size) != size)
3417− goto the_end;
3422+ goto invalid;
34183423 nsyms = get_be(data, entrysize);
34193424 ar_index = data + entrysize;
34203425 ar_names = (char *) ar_index + nsyms * entrysize;
@@ -3432,6 +3437,7 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize) · 11 unmodified lines
34323437 off = get_be(ar_index + i * entrysize, entrysize);
34333438 len = read_ar_header(fd, off, &hdr);
34343439 if (len <= 0 || memcmp(hdr.ar_fmag, ARFMAG, 2)) {
3440+ invalid:
34353441 tcc_error_noabort("invalid archive");
34363442 goto the_end;
34373443 }
tccgen.c+2−0
@@ -401,7 +401,9 @@ ST_FUNC int tccgen_compile(TCCState *s1) · 400 unmodified lines
401401 gen_inline_functions(s1);
402402 check_vstack();
403403 /* end of translation unit info */
404+#if TCC_EH_FRAME
404405 tcc_eh_frame_end(s1);
406+#endif
405407 tcc_debug_end(s1);
406408 tcc_tcov_end(s1);
407409 return 0;
tccpe.c+4−6
@@ -845,7 +845,7 @@ found_dll: · 844 unmodified lines
845845 return s;
846846 }
847847
848−void pe_free_imports(struct pe_info *pe)
848+static void pe_free_imports(struct pe_info *pe)
849849 {
850850 int i;
851851 for (i = 0; i < pe->imp_count; ++i) {
@@ -1133,9 +1133,7 @@ static int pe_section_class(Section *s) · 281 unmodified lines
11331133 name = s->name;
11341134
11351135 if (0 == memcmp(name, ".stab", 5) || 0 == memcmp(name, ".debug_", 7)) {
1136− if (s->s1->do_debug)
1137− return sec_debug;
1138−
1136+ return sec_debug;
11391137 } else if (flags & SHF_ALLOC) {
11401138 if (type == SHT_PROGBITS
11411139 || type == SHT_INIT_ARRAY
@@ -1995,7 +1993,7 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) · 853 unmodified lines
19951993 tcc_add_support(s1, TCC_LIBTCC1);
19961994 for (pp = libs; 0 != (p = *pp); ++pp) {
19971995 if (*p)
1998− tcc_add_library_err(s1, p);
1996+ tcc_add_library(s1, p);
19991997 else if (PE_DLL != pe_type && PE_GUI != pe_type)
20001998 break;
20011999 }
@@ -2068,8 +2066,8 @@ ST_FUNC int pe_output_file(TCCState *s1, const char *filename) · 66 unmodified lines
20682066 #ifdef CONFIG_TCC_BCHECK
20692067 tcc_add_bcheck(s1);
20702068 #endif
2071− pe_add_runtime(s1, &pe);
20722069 tcc_add_pragma_libs(s1);
2070+ pe_add_runtime(s1, &pe);
20732071 resolve_common_syms(s1);
20742072 pe_set_options(s1, &pe);
20752073 pe_check_symbols(&pe);
tccpp.c+12−7
@@ -1929,23 +1929,28 @@ ST_FUNC void preprocess(int is_bof) · 1928 unmodified lines
19291929 break;
19301930 case TOK_LINE:
19311931 next_nomacro();
1932− if (tok != TOK_PPNUM)
1932+ if (tok != TOK_PPNUM) {
19331933 _line_err:
1934+ if (parse_flags & PARSE_FLAG_ASM_FILE)
1935+ goto ignore;
19341936 tcc_error("wrong #line format");
1937+ }
19351938 case TOK_PPNUM:
1936− parse_number(tokc.str.data);
1937− n = tokc.i;
1939+ for (n = 0, q = tokc.str.data; *q; ++q) {
1940+ if (!isnum(*q))
1941+ goto _line_err;
1942+ n = n * 10 + *q - '0';
1943+ }
19381944 next_nomacro();
19391945 if (tok != TOK_LINEFEED) {
19401946 if (tok == TOK_PPSTR && tokc.str.data[0] == '"') {
19411947 tokc.str.data[tokc.str.size - 2] = 0;
19421948 tccpp_putfile(tokc.str.data + 1);
1943− } else if (parse_flags & PARSE_FLAG_ASM_FILE)
1944− goto ignore;
1945− else
1949+ } else
19461950 goto _line_err;
19471951 next_nomacro();
1948− }
1952+ } else if (parse_flags & PARSE_FLAG_ASM_FILE)
1953+ goto ignore;
19491954 if (file->fd > 0)
19501955 total_lines += file->line_num - n;
19511956 file->line_num = n;

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