History

tcc -ar: pad archive member position to even

Since commit 45cff8f0 tcc eventually generates object files of non-even size. tccelf.c: - check ARFMAG for better invalid archive detection - file_offset needs to be aligned, not the size (just a nitpick) lib/Makefile: - remake everything when tcc did change

grischka committed 2 years ago

Commit 9fb89c21 parent d7f91663 files · +25 −12Browse files

Unified · Split

3 files
  1. lib/Makefile +14 −7
  2. tccelf.c +4 −3
  3. tcctools.c +7 −2
lib/Makefile+14−7
@@ -8,6 +8,7 @@ VPATH = $(TOPSRC)/lib $(TOPSRC)/win32/lib · 7 unmodified lines
8T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)8T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
9X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)9X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
10 10
11TCC = $(TOP)/$(X)tcc$(EXESUF)
11XTCC ?= $(TOP)/$(X)tcc$(EXESUF)12XTCC ?= $(TOP)/$(X)tcc$(EXESUF)
12XCC = $(XTCC)13XCC = $(XTCC)
13XAR = $(XTCC) -ar14XAR = $(XTCC) -ar
@@ -44,6 +45,9 @@ $(X)BT_O += runmain.o tcov.o · 30 unmodified lines
44 45
45DSO_O = dsohandle.o46DSO_O = dsohandle.o
46 47
48# not in libtcc1.a
49EXTRA_O = runmain.o bt-exe.o bt-dll.o bt-log.o bcheck.o
50
47I386_O = libtcc1.o alloca.o alloca-bt.o stdatomic.o atomic.o builtin.o $(BT_O)51I386_O = libtcc1.o alloca.o alloca-bt.o stdatomic.o atomic.o builtin.o $(BT_O)
48X86_64_O = libtcc1.o alloca.o alloca-bt.o stdatomic.o atomic.o builtin.o $(BT_O)52X86_64_O = libtcc1.o alloca.o alloca-bt.o stdatomic.o atomic.o builtin.o $(BT_O)
49ARM_O = libtcc1.o armeabi.o alloca.o armflush.o stdatomic.o atomic.o builtin.o $(BT_O)53ARM_O = libtcc1.o armeabi.o alloca.o armflush.o stdatomic.o atomic.o builtin.o $(BT_O)
@@ -67,23 +71,23 @@ OBJ-arm-eabihf = $(ARM_O) $(DSO_O) · 17 unmodified lines
67OBJ-arm-wince = $(ARM_O) $(WIN_O)71OBJ-arm-wince = $(ARM_O) $(WIN_O)
68OBJ-riscv64 = $(RISCV64_O) $(BCHECK_O) $(DSO_O)72OBJ-riscv64 = $(RISCV64_O) $(BCHECK_O) $(DSO_O)
69 73
70OBJ-extra = $(filter $(B_O) runmain.o,$(OBJ-$T))74OBJ-extra = $(filter $(EXTRA_O),$(OBJ-$T))
71OBJ-libtcc1 = $(addprefix $(X),$(filter-out $(OBJ-extra),$(OBJ-$T)))75OBJ-libtcc1 = $(addprefix $(X),$(filter-out $(OBJ-extra),$(OBJ-$T)))
72 76
73ALL = $(addprefix $(TOP)/,$(X)libtcc1.a $(OBJ-extra))77ALL = $(addprefix $(TOP)/,$(X)libtcc1.a $(OBJ-extra))
74 78
75all: $(ALL)79all: $(ALL)
76 80
77$(TOP)/$(X)libtcc1.a : $(OBJ-libtcc1)81$(TOP)/$(X)libtcc1.a : $(OBJ-libtcc1) $(TCC)
78 $S$(XAR) rcs $@ $^82 $S$(XAR) rcs $@ $(OBJ-libtcc1)
79 83
80$(X)%.o : %.c84$(X)%.o : %.c $(TCC)
81 $S$(XCC) -c $< -o $@ $(XFLAGS)85 $S$(XCC) -c $< -o $@ $(XFLAGS)
82 86
83$(X)%.o : %.S87$(X)%.o : %.S $(TCC)
84 $S$(XCC) -c $< -o $@ $(XFLAGS)88 $S$(XCC) -c $< -o $@ $(XFLAGS)
85 89
86$(TOP)/%.o : %.c90$(TOP)/%.o : %.c $(TCC)
87 $S$(XCC) -c $< -o $@ $(XFLAGS)91 $S$(XCC) -c $< -o $@ $(XFLAGS)
88 92
89$(TOP)/bcheck.o : XFLAGS += $(BFLAGS) $(if $(CONFIG_musl),-DTCC_MUSL)93$(TOP)/bcheck.o : XFLAGS += $(BFLAGS) $(if $(CONFIG_musl),-DTCC_MUSL)
@@ -92,5 +96,8 @@ $(TOP)/bt-exe.o : $(TOP)/tccrun.c · 2 unmodified lines
92$(X)crt1w.o : crt1.c96$(X)crt1w.o : crt1.c
93$(X)wincrt1w.o : wincrt1.c97$(X)wincrt1w.o : wincrt1.c
94 98
99# don't try to make it
100$(TCC) : ;
101
95clean :102clean :
96 rm -f *.a *.o $(ALL)103 rm -f *.o $(addprefix $(TOP)/,*libtcc1.a $(EXTRA_O))
tccelf.c+4−3
@@ -3374,6 +3374,8 @@ static int read_ar_header(int fd, int offset, ArchiveHeader *hdr) · 3373 unmodified lines
3374 len = full_read(fd, hdr, sizeof(ArchiveHeader));3374 len = full_read(fd, hdr, sizeof(ArchiveHeader));
3375 if (len != sizeof(ArchiveHeader))3375 if (len != sizeof(ArchiveHeader))
3376 return len ? -1 : 0;3376 return len ? -1 : 0;
3377 if (memcmp(hdr->ar_fmag, ARFMAG, sizeof hdr->ar_fmag))
3378 return -1;
3377 p = hdr->ar_name;3379 p = hdr->ar_name;
3378 for (e = p + sizeof hdr->ar_name; e > p && e[-1] == ' ';)3380 for (e = p + sizeof hdr->ar_name; e > p && e[-1] == ' ';)
3379 --e;3381 --e;
@@ -3451,8 +3453,6 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte) · 71 unmodified lines
3451 return tcc_error_noabort("invalid archive");3453 return tcc_error_noabort("invalid archive");
3452 file_offset += len;3454 file_offset += len;
3453 size = strtol(hdr.ar_size, NULL, 0);3455 size = strtol(hdr.ar_size, NULL, 0);
3454 /* align to even */
3455 size = (size + 1) & ~1;
3456 if (alacarte) {3456 if (alacarte) {
3457 /* coff symbol table : we handle it */3457 /* coff symbol table : we handle it */
3458 if (!strcmp(hdr.ar_name, "/"))3458 if (!strcmp(hdr.ar_name, "/"))
@@ -3465,7 +3465,8 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte) · 6 unmodified lines
3465 if (tcc_load_object_file(s1, fd, file_offset) < 0)3465 if (tcc_load_object_file(s1, fd, file_offset) < 0)
3466 return -1;3466 return -1;
3467 }3467 }
3468 file_offset += size;3468 /* align to even */
3469 file_offset = (file_offset + size + 1) & ~1;
3469 }3470 }
3470}3471}
3471 3472
tcctools.c+7−2
@@ -87,7 +87,7 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv) · 86 unmodified lines
87 ElfW(Shdr) *shdr;87 ElfW(Shdr) *shdr;
88 ElfW(Sym) *sym;88 ElfW(Sym) *sym;
89 int i, fsize, i_lib, i_obj;89 int i, fsize, i_lib, i_obj;
90 char *buf, *shstr, *symtab = NULL, *strtab = NULL;90 char *buf, *shstr, *symtab, *strtab;
91 int symtabsize = 0;//, strtabsize = 0;91 int symtabsize = 0;//, strtabsize = 0;
92 char *anames = NULL;92 char *anames = NULL;
93 int *afpos = NULL;93 int *afpos = NULL;
@@ -174,6 +174,8 @@ no_ar: · 80 unmodified lines
174 /* ignore date/uid/gid/mode */174 /* ignore date/uid/gid/mode */
175 }175 }
176 }176 }
177 if (fsize & 1)
178 fgetc(fh);
177 tcc_free(buf);179 tcc_free(buf);
178 }180 }
179 ret = 0;181 ret = 0;
@@ -232,6 +234,7 @@ finish: · 52 unmodified lines
232 234
233 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);235 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
234 shstr = (char *)(buf + shdr->sh_offset);236 shstr = (char *)(buf + shdr->sh_offset);
237 symtab = strtab = NULL;
235 for (i = 0; i < ehdr->e_shnum; i++)238 for (i = 0; i < ehdr->e_shnum; i++)
236 {239 {
237 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);240 shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
@@ -252,7 +255,7 @@ finish: · 14 unmodified lines
252 }255 }
253 }256 }
254 257
255 if (symtab && symtabsize)258 if (symtab && strtab)
256 {259 {
257 int nsym = symtabsize / sizeof(ElfW(Sym));260 int nsym = symtabsize / sizeof(ElfW(Sym));
258 //printf("symtab: info size shndx name\n");261 //printf("symtab: info size shndx name\n");
@@ -298,6 +301,8 @@ finish: · 39 unmodified lines
298 tcc_free(buf);301 tcc_free(buf);
299 i_obj++;302 i_obj++;
300 fpos += (fsize + sizeof(arhdro));303 fpos += (fsize + sizeof(arhdro));
304 if (fpos & 1)
305 fputc(0, fo), ++fpos;
301 }306 }
302 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);307 hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
303 fpos = 0;308 fpos = 0;

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