History

Remove gaps between RELX sections

These gaps are a result of final_sections_reloc. Here some relocs are removed. The gaps are then filled with 0 in tcc_output_elf. The 0 is intepreted as R_...NONE reloc. This does work on most targets but on OpenBSD/arm64 this is illegal.

herman ten brugge committed 5 years ago

Commit c9bdfce1 parent cd91ea61 file · +33 −14Browse files

Unified · Split

tccelf.c+33−14
@@ -1863,8 +1863,7 @@ struct ro_inf { · 1862 unmodified lines
18631863 static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr,
18641864 int phnum, int phfill,
18651865 Section *interp, Section* strsec,
1866− struct dyn_inf *dyninf, struct ro_inf *roinf,
1867− int *sec_order)
1866+ struct ro_inf *roinf, int *sec_order)
18681867 {
18691868 int i, sh_order_index, file_offset;
18701869 Section *s;
@@ -1912,9 +1911,6 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, · 41 unmodified lines
19121911 if (interp)
19131912 ph += 2;
19141913
1915− /* dynamic relocation table information, for .dynamic section */
1916− dyninf->rel_addr = dyninf->rel_size = 0;
1917−
19181914 /* read only segment mapping for GNU_RELRO */
19191915 roinf->sh_offset = roinf->sh_addr = roinf->sh_size = 0;
19201916
@@ -2001,12 +1997,6 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, · 80 unmodified lines
20011997 ph->p_vaddr = addr;
20021998 ph->p_paddr = ph->p_vaddr;
20031999 }
2004− /* update dynamic relocation infos */
2005− if (s->sh_type == SHT_RELX && s != relocplt) {
2006− if (dyninf->rel_size == 0)
2007− dyninf->rel_addr = addr;
2008− dyninf->rel_size = (addr - dyninf->rel_addr) + s->sh_size;
2009− }
20102000 if (s == data_ro_section ||
20112001 #ifdef CONFIG_TCC_BCHECK
20122002 s == bounds_section ||
@@ -2248,6 +2238,32 @@ static int final_sections_reloc(TCCState *s1) · 235 unmodified lines
22482238 }
22492239 return 0;
22502240 }
2241+
2242+/* Remove gaps between RELX sections.
2243+ These gaps are a result of final_sections_reloc. Here some relocs are removed.
2244+ The gaps are then filled with 0 in tcc_output_elf. The 0 is intepreted as
2245+ R_...NONE reloc. This does work on most targets but on OpenBSD/arm64 this
2246+ is illegal. */
2247+static void update_reloc_sections(TCCState *s1, struct dyn_inf *dyninf)
2248+{
2249+ int i;
2250+ Section *s;
2251+ Section *relocplt = s1->got ? s1->got->relocplt : NULL;
2252+
2253+ /* dynamic relocation table information, for .dynamic section */
2254+ dyninf->rel_addr = dyninf->rel_size = 0;
2255+
2256+ for(i = 1; i < s1->nb_sections; i++) {
2257+ s = s1->sections[i];
2258+ if (s->sh_type == SHT_RELX && s != relocplt) {
2259+ if (dyninf->rel_size == 0)
2260+ dyninf->rel_addr = s->sh_addr;
2261+ else
2262+ s->sh_addr = dyninf->rel_addr + dyninf->rel_size;
2263+ dyninf->rel_size += s->sh_size;
2264+ }
2265+ }
2266+}
22512267 #endif
22522268
22532269 /* Create an ELF file on disk.
@@ -2671,7 +2687,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 417 unmodified lines
26712687
26722688 /* compute section to program header mapping */
26732689 file_offset = layout_sections(s1, phdr, phnum, phfill, interp, strsec,
2674− &dyninf, &roinf, sec_order);
2690+ &roinf, sec_order);
26752691
26762692 #ifndef ELF_OBJ_ONLY
26772693 /* Fill remaining program header and finalize relocation related to dynamic
@@ -2680,8 +2696,6 @@ static int elf_output_file(TCCState *s1, const char *filename) · 2 unmodified lines
26802696 fill_unloadable_phdr(phdr, phnum, interp, dynamic, note, roinf_use);
26812697 if (dynamic) {
26822698 ElfW(Sym) *sym;
2683− dynamic->data_offset = dyninf.data_offset;
2684− fill_dynamic(s1, &dyninf);
26852699
26862700 /* put in GOT the dynamic section address and relocate PLT */
26872701 write32le(s1->got->data, dynamic->sh_addr);
@@ -2703,6 +2717,11 @@ static int elf_output_file(TCCState *s1, const char *filename) · 15 unmodified lines
27032717 ret = final_sections_reloc(s1);
27042718 if (ret)
27052719 goto the_end;
2720+ if (dynamic) {
2721+ update_reloc_sections (s1, &dyninf);
2722+ dynamic->data_offset = dyninf.data_offset;
2723+ fill_dynamic(s1, &dyninf);
2724+ }
27062725 tidy_section_headers(s1, sec_order);
27072726
27082727 /* Perform relocation to GOT or PLT entries */

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