From fef838db2d124db3f1357385972371ccba7af2c6 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Mon, 4 Mar 2019 16:53:24 +0100 Subject: [PATCH] Fix relocs of unwanted sections relocation sections to sections we don't want to handle lead to segfaults, handle this situation (by ignoring the relocs as well). --- tccelf.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tccelf.c b/tccelf.c index 0a5ba71d..e5f6c42c 100644 --- a/tccelf.c +++ b/tccelf.c @@ -2271,6 +2271,7 @@ static void *load_data(int fd, unsigned long file_offset, unsigned long size) typedef struct SectionMergeInfo { Section *s; /* corresponding existing section */ unsigned long offset; /* offset of the new section in the existing section */ + unsigned long oldlen; /* size of existing section before adding this one */ uint8_t new_section; /* true if section 's' was added */ uint8_t link_once; /* true if link once section */ } SectionMergeInfo; @@ -2421,6 +2422,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, } /* align start of section */ + sm_table[i].oldlen = s->data_offset; offset = s->data_offset; if (0 == strcmp(sh_name, ".stab")) { @@ -2478,9 +2480,15 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, if (sh->sh_link > 0) s->link = sm_table[sh->sh_link].s; if (sh->sh_type == SHT_RELX) { - s->sh_info = sm_table[sh->sh_info].s->sh_num; - /* update backward link */ - s1->sections[s->sh_info]->reloc = s; + if (sm_table[sh->sh_info].s) { + s->sh_info = sm_table[sh->sh_info].s->sh_num; + /* update backward link */ + s1->sections[s->sh_info]->reloc = s; + } else { + /* we haven't read the target of relocs, so cancel them */ + s->data_offset = sm_table[i].oldlen; + sm_table[i].s = NULL; + } } } sm = sm_table; -- 2.11.4.GIT