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
1406 /* Allocate strings for section names */1406 /* Allocate strings for section names */
1407 for(i = 1; i < s1->nb_sections; i++) {1407 for(i = 1; i < s1->nb_sections; i++) {
1408 s = s1->sections[i];1408 s = s1->sections[i];
1409 s->sh_name = put_elf_str(strsec, s->name);
1410 /* when generating a DLL, we include relocations but we may1409 /* when generating a DLL, we include relocations but we may
1411 patch them */1410 patch them */
1412 if (file_type == TCC_OUTPUT_DLL &&1411 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
1420 } else if (s1->do_debug ||1419 } else if (s1->do_debug ||
1421 file_type == TCC_OUTPUT_OBJ ||1420 file_type == TCC_OUTPUT_OBJ ||
1422 (s->sh_flags & SHF_ALLOC) ||1421 (s->sh_flags & SHF_ALLOC) ||
1423 i == (s1->nb_sections - 1)) {1422 i == (s1->nb_sections - 1)) {
1424 /* we output all sections if debug or object file */1423 /* we output all sections if debug or object file */
1425 s->sh_size = s->data_offset;1424 s->sh_size = s->data_offset;
1426 }1425 }
1426 if (s->sh_size || (s->sh_flags & SHF_ALLOC))
1427 s->sh_name = put_elf_str(strsec, s->name);
1427 }1428 }
1429 strsec->sh_size = strsec->data_offset;
1428}1430}
1429 1431
1430/* Info to be copied in dynamic section */1432/* 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
1898 return 0;1900 return 0;
1899}1901}
1900 1902
1903/* Sort section headers by assigned sh_addr, remove sections
1904 that we aren't going to output. */
1905static 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
1901/* Output an elf, coff or binary file */1948/* Output an elf, coff or binary file */
1902/* XXX: suppress unneeded sections */1949/* XXX: suppress unneeded sections */
1903static int elf_output_file(TCCState *s1, const char *filename)1950static 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
2068 ret = final_sections_reloc(s1);2115 ret = final_sections_reloc(s1);
2069 if (ret)2116 if (ret)
2070 goto the_end;2117 goto the_end;
2118 tidy_section_headers(s1, sec_order);
2071 }2119 }
2072 2120
2073 /* Perform relocation to GOT or PLT entries */2121 /* Perform relocation to GOT or PLT entries */
@@ -2076,6 +2124,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 2 unmodified lines
2076 2124
2077 /* Create the ELF file with name 'filename' */2125 /* Create the ELF file with name 'filename' */
2078 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);2126 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2127 s1->nb_sections = shnum;
2079 the_end:2128 the_end:
2080 tcc_free(sec_order);2129 tcc_free(sec_order);
2081 tcc_free(phdr);2130 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