relocation sections to sections we don't want to handle lead to segfaults, handle this situation (by ignoring the relocs as well).
Unified · Split
| @@ -2271,6 +2271,7 @@ static void *load_data(int fd, unsigned long file_offset, unsigned long size) · 2270 unmodified lines | ||
| 2271 | 2271 | typedef struct SectionMergeInfo { |
| 2272 | 2272 | Section *s; /* corresponding existing section */ |
| 2273 | 2273 | unsigned long offset; /* offset of the new section in the existing section */ |
| 2274 | + unsigned long oldlen; /* size of existing section before adding this one */ | |
| 2274 | 2275 | uint8_t new_section; /* true if section 's' was added */ |
| 2275 | 2276 | uint8_t link_once; /* true if link once section */ |
| 2276 | 2277 | } SectionMergeInfo; |
| @@ -2421,6 +2422,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 144 unmodified lines | ||
| 2421 | 2422 | } |
| 2422 | 2423 | |
| 2423 | 2424 | /* align start of section */ |
| 2425 | + sm_table[i].oldlen = s->data_offset; | |
| 2424 | 2426 | offset = s->data_offset; |
| 2425 | 2427 | |
| 2426 | 2428 | if (0 == strcmp(sh_name, ".stab")) { |
| @@ -2478,9 +2480,15 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 51 unmodified lines | ||
| 2478 | 2480 | if (sh->sh_link > 0) |
| 2479 | 2481 | s->link = sm_table[sh->sh_link].s; |
| 2480 | 2482 | if (sh->sh_type == SHT_RELX) { |
| 2481 | − s->sh_info = sm_table[sh->sh_info].s->sh_num; | |
| 2482 | − /* update backward link */ | |
| 2483 | − s1->sections[s->sh_info]->reloc = s; | |
| 2483 | + if (sm_table[sh->sh_info].s) { | |
| 2484 | + s->sh_info = sm_table[sh->sh_info].s->sh_num; | |
| 2485 | + /* update backward link */ | |
| 2486 | + s1->sections[s->sh_info]->reloc = s; | |
| 2487 | + } else { | |
| 2488 | + /* we haven't read the target of relocs, so cancel them */ | |
| 2489 | + s->data_offset = sm_table[i].oldlen; | |
| 2490 | + sm_table[i].s = NULL; | |
| 2491 | + } | |
| 2484 | 2492 | } |
| 2485 | 2493 | } |
| 2486 | 2494 | sm = sm_table; |