From 600018ce474b1ecb7ea51f9fa0b005000ccc9101 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Sat, 6 May 2017 07:30:44 +0200 Subject: [PATCH] elf: Ignore SHF_COMPRESSED sections some newer systems have debug sections compressed by default, which includes those in the crt[1in].o startup files. These can't simply be concatenated like all others (which leads to invalid section contents ultimately making gdb fail) but need special handling. Instead of that special handling (decompressing, which in turn requires linking against zlib) let's just ignore such sections, even though that means to also ignore all other debug sections from that particular input file. Our own generated files of course don't have the problem. --- elf.h | 1 + tccelf.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/elf.h b/elf.h index d761747..68f3990 100644 --- a/elf.h +++ b/elf.h @@ -376,6 +376,7 @@ typedef struct required */ #define SHF_GROUP (1 << 9) /* Section is member of a group. */ #define SHF_TLS (1 << 10) /* Section hold thread-local data. */ +#define SHF_COMPRESSED (1 << 11) /* Section with compressed data. */ #define SHF_MASKOS 0x0ff00000 /* OS-specific. */ #define SHF_MASKPROC 0xf0000000 /* Processor-specific */ #define SHF_ORDERED (1 << 30) /* Special ordering requirement diff --git a/tccelf.c b/tccelf.c index 76f3da6..5dc1e70 100644 --- a/tccelf.c +++ b/tccelf.c @@ -2194,7 +2194,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, { ElfW(Ehdr) ehdr; ElfW(Shdr) *shdr, *sh; - int size, i, j, offset, offseti, nb_syms, sym_index, ret; + int size, i, j, offset, offseti, nb_syms, sym_index, ret, seencompressed; unsigned char *strsec, *strtab; int *old_to_new_syms; char *sh_name, *name; @@ -2232,6 +2232,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, symtab = NULL; strtab = NULL; nb_syms = 0; + seencompressed = 0; for(i = 1; i < ehdr.e_shnum; i++) { sh = &shdr[i]; if (sh->sh_type == SHT_SYMTAB) { @@ -2249,6 +2250,8 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, sh = &shdr[sh->sh_link]; strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); } + if (sh->sh_flags & SHF_COMPRESSED) + seencompressed = 1; } /* now examine each section and try to merge its content with the @@ -2272,6 +2275,12 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, strcmp(sh_name, ".stabstr") ) continue; + if (seencompressed + && (!strncmp(sh_name, ".debug_", sizeof(".debug_")-1) + || (sh->sh_type == SHT_RELX + && !strncmp((char*)strsec + shdr[sh->sh_info].sh_name, + ".debug_", sizeof(".debug_")-1)))) + continue; if (sh->sh_addralign < 1) sh->sh_addralign = 1; /* find corresponding section, if any */