History

elf: Tidy section headers

Don't emit useless section headers and also sort them in allocated order. Doesn't change behaviour except makes the resulting files a tiny bit smaller (though at the expense of some very tiny compile time and code size increase of tcc itself; not 100% it's worth it).

Michael Matz committed 9 years ago

Commit 24420bb1 parent 3c39cb51 file · +51 −2Browse files

Unified · Split

tccelf.c+51−2
@@ -1406,7 +1406,6 @@ static void alloc_sec_names(TCCState *s1, int file_type, Section *strsec) · 1405 unmodified lines
14061406 /* Allocate strings for section names */
14071407 for(i = 1; i < s1->nb_sections; i++) {
14081408 s = s1->sections[i];
1409− s->sh_name = put_elf_str(strsec, s->name);
14101409 /* when generating a DLL, we include relocations but we may
14111410 patch them */
14121411 if (file_type == TCC_OUTPUT_DLL &&
@@ -1420,11 +1419,14 @@ static void alloc_sec_names(TCCState *s1, int file_type, Section *strsec) · 7 unmodified lines
14201419 } else if (s1->do_debug ||
14211420 file_type == TCC_OUTPUT_OBJ ||
14221421 (s->sh_flags & SHF_ALLOC) ||
1423− i == (s1->nb_sections - 1)) {
1422+ i == (s1->nb_sections - 1)) {
14241423 /* we output all sections if debug or object file */
14251424 s->sh_size = s->data_offset;
14261425 }
1426+ if (s->sh_size || (s->sh_flags & SHF_ALLOC))
1427+ s->sh_name = put_elf_str(strsec, s->name);
14271428 }
1429+ strsec->sh_size = strsec->data_offset;
14281430 }
14291431
14301432 /* Info to be copied in dynamic section */
@@ -1898,6 +1900,51 @@ static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum, · 467 unmodified lines
18981900 return 0;
18991901 }
19001902
1903+/* Sort section headers by assigned sh_addr, remove sections
1904+ that we aren't going to output. */
1905+static void tidy_section_headers(TCCState *s1, int *sec_order)
1906+{
1907+ int i, nnew, l, *backmap;
1908+ Section **snew, *s;
1909+ ElfW(Sym) *sym;
1910+
1911+ snew = tcc_malloc(s1->nb_sections * sizeof(snew[0]));
1912+ backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0]));
1913+ for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
1914+ s = s1->sections[sec_order[i]];
1915+ if (!i || s->sh_name) {
1916+ backmap[sec_order[i]] = nnew;
1917+ snew[nnew] = s;
1918+ ++nnew;
1919+ } else {
1920+ backmap[sec_order[i]] = 0;
1921+ snew[--l] = s;
1922+ }
1923+ }
1924+ for (i = 0; i < nnew; i++) {
1925+ s = snew[i];
1926+ if (s) {
1927+ s->sh_num = i;
1928+ if (s->sh_type == SHT_RELX)
1929+ s->sh_info = backmap[s->sh_info];
1930+ }
1931+ }
1932+
1933+ for_each_elem(symtab_section, 1, sym, ElfW(Sym))
1934+ if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1935+ sym->st_shndx = backmap[sym->st_shndx];
1936+ for_each_elem(s1->dynsym, 1, sym, ElfW(Sym))
1937+ if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1938+ sym->st_shndx = backmap[sym->st_shndx];
1939+
1940+ for (i = 0; i < s1->nb_sections; i++)
1941+ sec_order[i] = i;
1942+ tcc_free(s1->sections);
1943+ s1->sections = snew;
1944+ s1->nb_sections = nnew;
1945+ tcc_free(backmap);
1946+}
1947+
19011948 /* Output an elf, coff or binary file */
19021949 /* XXX: suppress unneeded sections */
19031950 static int elf_output_file(TCCState *s1, const char *filename)
@@ -2068,6 +2115,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 164 unmodified lines
20682115 ret = final_sections_reloc(s1);
20692116 if (ret)
20702117 goto the_end;
2118+ tidy_section_headers(s1, sec_order);
20712119 }
20722120
20732121 /* Perform relocation to GOT or PLT entries */
@@ -2076,6 +2124,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 2 unmodified lines
20762124
20772125 /* Create the ELF file with name 'filename' */
20782126 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2127+ s1->nb_sections = shnum;
20792128 the_end:
20802129 tcc_free(sec_order);
20812130 tcc_free(phdr);

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