aboutsummaryrefslogtreecommitdiff
path: root/bfd/elflink.c
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2015-06-25 08:16:00 -0700
committerH.J. Lu <hjl.tools@gmail.com>2015-06-25 08:16:14 -0700
commitef10c3ace00674e8c3599c3bf95f06c87d68898b (patch)
treed20f09dadf1e21502e851266062d404ea5460b49 /bfd/elflink.c
parentf8773be1be076f828b93ac3bebeab3f782e191e4 (diff)
downloadgdb-ef10c3ace00674e8c3599c3bf95f06c87d68898b.zip
gdb-ef10c3ace00674e8c3599c3bf95f06c87d68898b.tar.gz
gdb-ef10c3ace00674e8c3599c3bf95f06c87d68898b.tar.bz2
Use strtab with GC and suffix merging for .strtab
This patch uses ELF strtab with GC and suffix merging support to create ELF .strtab section. There is some small memory overhead to use ELF strtab: ==14928== HEAP SUMMARY: ==14928== in use at exit: 3,276,318 bytes in 679 blocks ==14928== total heap usage: 1,544 allocs, 865 frees, 15,259,146 bytes allocated vs. ==14936== HEAP SUMMARY: ==14936== in use at exit: 3,276,318 bytes in 679 blocks ==14936== total heap usage: 1,532 allocs, 853 frees, 15,026,402 bytes allocated when running: ./ld-new -m elf_x86_64 -o tmpdir/ld-partial.o -r ldgram.o ldlex-wrapper.o lexsup.o ldlang.o mri.o ldctor.o ldmain.o plugin.o ldwrite.o ldexp.o ldemul.o ldver.o ldmisc.o ldfile.o ldcref.o eelf_x86_64.o eelf32_x86_64.o eelf_i386.o eelf_iamcu.o ei386linux.o eelf_l1om.o eelf_k1om.o ldbuildid.o The results are [32] .strtab STRTAB 0+ 3beff8 00407a 00 0 0 1 vs [32] .strtab STRTAB 0+ 3beff8 0041d8 00 0 0 1 It reduces the .strtab size by 350 bytes, about 2% Saving on libc.so from glibc is much more since libc.so has many alias symbols with the same suffix. For x32 glibc, [82] .strtab STRTAB 0+ 81b348 0159e7 00 0 0 1 vs [82] .strtab STRTAB 0+ 81b8bc 019e72 00 0 0 1 It reduces the .strtab size by 17547 bytes, about 16%. bfd/ PR gas/18451 * elf-bfd.h (elf_sym_strtab): New. (elf_link_hash_table): Add strtabcount, strtabsize and strtab. (_bfd_elf_stringtab_init): Removed. * elf.c (_bfd_elf_stringtab_init): Removed. (_bfd_elf_compute_section_file_positions): Replace bfd_strtab_hash/_bfd_elf_stringtab_init/_bfd_stringtab_free/ _bfd_stringtab_size with elf_strtab_hash/_bfd_elf_strtab_init/_bfd_elf_strtab_free/ _bfd_elf_strtab_size. Use _bfd_elf_strtab_add, _bfd_elf_strtab_finalize and _bfd_elf_strtab_offset to get st_name. (swap_out_syms): Likewise. * elflink.c (elf_final_link_info): Replace bfd_strtab_hash with elf_strtab_hash. Remove symbuf, symbuf_count, symbuf_size and shndxbuf_size. (elf_link_flush_output_syms): Removed. (elf_link_output_sym): Renamed to ... (elf_link_output_symstrtab): This. Replace _bfd_stringtab_add with _bfd_elf_strtab_add. Don't flush symbols to the file nor swap out symbols. (elf_link_swap_symbols_out): New. (elf_link_output_extsym): Replace elf_link_output_sym with elf_link_output_symstrtab. (elf_link_input_bfd): Likewise. (elf_final_link_free): Replace _bfd_stringtab_free with _bfd_elf_strtab_free. Remove symbuf. (bfd_elf_final_link): Replace _bfd_elf_stringtab_init with _bfd_elf_strtab_init. Don't set symbuf, symbuf_count, symbuf_size nor shndxbuf_size. Initialize strtabsize and strtab. Initialize symshndxbuf to -1 when number of sections >= 64K. Replace elf_link_output_sym/elf_link_output_sym with elf_link_output_symstrtab/elf_link_output_symstrtab. Don't call elf_link_flush_output_syms. Call _bfd_elf_strtab_finalize and elf_link_swap_symbols_out. Replace _bfd_stringtab_size and _bfd_stringtab_emit with _bfd_elf_strtab_size and _bfd_elf_strtab_emit. gas/testsuite/ PR gas/18451 * gas/elf/elf.exp: Run strtab. * gas/elf/strtab.d: New file. * gas/elf/strtab.s: Likewise. ld/testsuite/ PR gas/18451 * ld-elf/strtab.d: New file. * ld-elf/strtab.s: Likewise.
Diffstat (limited to 'bfd/elflink.c')
-rw-r--r--bfd/elflink.c270
1 files changed, 151 insertions, 119 deletions
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 9520906..1c0861b 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -7444,7 +7444,7 @@ struct elf_final_link_info
/* Output BFD. */
bfd *output_bfd;
/* Symbol string table. */
- struct bfd_strtab_hash *symstrtab;
+ struct elf_strtab_hash *symstrtab;
/* .dynsym section. */
asection *dynsym_sec;
/* .hash section. */
@@ -7471,16 +7471,8 @@ struct elf_final_link_info
/* Array large enough to hold a section pointer for each local
symbol of any input BFD. */
asection **sections;
- /* Buffer to hold swapped out symbols. */
- bfd_byte *symbuf;
- /* And one for symbol section indices. */
+ /* Buffer for SHT_SYMTAB_SHNDX section. */
Elf_External_Sym_Shndx *symshndxbuf;
- /* Number of swapped out symbols in buffer. */
- size_t symbuf_count;
- /* Number of symbols which fit in symbuf. */
- size_t symbuf_size;
- /* And same for symshndxbuf. */
- size_t shndxbuf_size;
/* Number of STT_FILE syms seen. */
size_t filesym_count;
};
@@ -8570,47 +8562,21 @@ elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
return ret;
}
-/* Flush the output symbols to the file. */
-
-static bfd_boolean
-elf_link_flush_output_syms (struct elf_final_link_info *flinfo,
- const struct elf_backend_data *bed)
-{
- if (flinfo->symbuf_count > 0)
- {
- Elf_Internal_Shdr *hdr;
- file_ptr pos;
- bfd_size_type amt;
-
- hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
- pos = hdr->sh_offset + hdr->sh_size;
- amt = flinfo->symbuf_count * bed->s->sizeof_sym;
- if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0
- || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt)
- return FALSE;
-
- hdr->sh_size += amt;
- flinfo->symbuf_count = 0;
- }
-
- return TRUE;
-}
-
-/* Add a symbol to the output symbol table. */
+/* Add a symbol to the output symbol string table. */
static int
-elf_link_output_sym (struct elf_final_link_info *flinfo,
- const char *name,
- Elf_Internal_Sym *elfsym,
- asection *input_sec,
- struct elf_link_hash_entry *h)
-{
- bfd_byte *dest;
- Elf_External_Sym_Shndx *destshndx;
+elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
+ const char *name,
+ Elf_Internal_Sym *elfsym,
+ asection *input_sec,
+ struct elf_link_hash_entry *h)
+{
int (*output_symbol_hook)
(struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
struct elf_link_hash_entry *);
+ struct elf_link_hash_table *hash_table;
const struct elf_backend_data *bed;
+ bfd_size_type strtabsize;
BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
@@ -8623,49 +8589,119 @@ elf_link_output_sym (struct elf_final_link_info *flinfo,
return ret;
}
- if (name == NULL || *name == '\0')
- elfsym->st_name = 0;
- else if (input_sec->flags & SEC_EXCLUDE)
- elfsym->st_name = 0;
+ if (name == NULL
+ || *name == '\0'
+ || (input_sec->flags & SEC_EXCLUDE))
+ elfsym->st_name = (unsigned long) -1;
else
{
- elfsym->st_name = (unsigned long) _bfd_stringtab_add (flinfo->symstrtab,
- name, TRUE, FALSE);
+ /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
+ to get the final offset for st_name. */
+ elfsym->st_name
+ = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
+ name, FALSE);
if (elfsym->st_name == (unsigned long) -1)
return 0;
}
- if (flinfo->symbuf_count >= flinfo->symbuf_size)
+ hash_table = elf_hash_table (flinfo->info);
+ strtabsize = hash_table->strtabsize;
+ if (strtabsize <= hash_table->strtabcount)
{
- if (! elf_link_flush_output_syms (flinfo, bed))
+ strtabsize += strtabsize;
+ hash_table->strtabsize = strtabsize;
+ strtabsize *= sizeof (*hash_table->strtab);
+ hash_table->strtab
+ = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
+ strtabsize);
+ if (hash_table->strtab == NULL)
return 0;
}
+ hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
+ hash_table->strtab[hash_table->strtabcount].dest_index
+ = hash_table->strtabcount;
+ hash_table->strtab[hash_table->strtabcount].destshndx_index
+ = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
+
+ bfd_get_symcount (flinfo->output_bfd) += 1;
+ hash_table->strtabcount += 1;
+
+ return 1;
+}
+
+/* Swap symbols out to the symbol table and flush the output symbols to
+ the file. */
- dest = flinfo->symbuf + flinfo->symbuf_count * bed->s->sizeof_sym;
- destshndx = flinfo->symshndxbuf;
- if (destshndx != NULL)
+static bfd_boolean
+elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
+{
+ struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
+ bfd_size_type amt, i;
+ const struct elf_backend_data *bed;
+ bfd_byte *symbuf;
+ Elf_Internal_Shdr *hdr;
+ file_ptr pos;
+ bfd_boolean ret;
+
+ if (!hash_table->strtabcount)
+ return TRUE;
+
+ BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
+
+ bed = get_elf_backend_data (flinfo->output_bfd);
+
+ amt = bed->s->sizeof_sym * hash_table->strtabcount;
+ symbuf = (bfd_byte *) bfd_malloc (amt);
+ if (symbuf == NULL)
+ return FALSE;
+
+ if (flinfo->symshndxbuf)
{
- if (bfd_get_symcount (flinfo->output_bfd) >= flinfo->shndxbuf_size)
+ amt = (sizeof (Elf_External_Sym_Shndx)
+ * (bfd_get_symcount (flinfo->output_bfd)));
+ flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
+ if (flinfo->symshndxbuf == NULL)
{
- bfd_size_type amt;
-
- amt = flinfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
- destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
- amt * 2);
- if (destshndx == NULL)
- return 0;
- flinfo->symshndxbuf = destshndx;
- memset ((char *) destshndx + amt, 0, amt);
- flinfo->shndxbuf_size *= 2;
+ free (symbuf);
+ return FALSE;
}
- destshndx += bfd_get_symcount (flinfo->output_bfd);
}
- bed->s->swap_symbol_out (flinfo->output_bfd, elfsym, dest, destshndx);
- flinfo->symbuf_count += 1;
- bfd_get_symcount (flinfo->output_bfd) += 1;
+ for (i = 0; i < hash_table->strtabcount; i++)
+ {
+ struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
+ if (elfsym->sym.st_name == (unsigned long) -1)
+ elfsym->sym.st_name = 0;
+ else
+ elfsym->sym.st_name
+ = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
+ elfsym->sym.st_name);
+ bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
+ ((bfd_byte *) symbuf
+ + (elfsym->dest_index
+ * bed->s->sizeof_sym)),
+ (flinfo->symshndxbuf
+ + elfsym->destshndx_index));
+ }
+
+ hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
+ pos = hdr->sh_offset + hdr->sh_size;
+ amt = hash_table->strtabcount * bed->s->sizeof_sym;
+ if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
+ && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
+ {
+ hdr->sh_size += amt;
+ ret = TRUE;
+ }
+ else
+ ret = FALSE;
- return 1;
+ free (symbuf);
+
+ free (hash_table->strtab);
+ hash_table->strtab = NULL;
+
+ return ret;
}
/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
@@ -9291,15 +9327,16 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
memset (&fsym, 0, sizeof (fsym));
fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
fsym.st_shndx = SHN_ABS;
- if (!elf_link_output_sym (eoinfo->flinfo, NULL, &fsym,
- bfd_und_section_ptr, NULL))
+ if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
+ bfd_und_section_ptr, NULL))
return FALSE;
eoinfo->file_sym_done = TRUE;
}
indx = bfd_get_symcount (flinfo->output_bfd);
- ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h);
+ ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
+ input_sec, h);
if (ret == 0)
{
eoinfo->failed = TRUE;
@@ -9593,10 +9630,11 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
memset (&osym, 0, sizeof (osym));
osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
osym.st_shndx = SHN_ABS;
- if (!elf_link_output_sym (flinfo,
- (input_bfd->lto_output ? NULL
- : input_bfd->filename),
- &osym, bfd_abs_section_ptr, NULL))
+ if (!elf_link_output_symstrtab (flinfo,
+ (input_bfd->lto_output ? NULL
+ : input_bfd->filename),
+ &osym, bfd_abs_section_ptr,
+ NULL))
return FALSE;
}
@@ -9628,7 +9666,7 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
}
indx = bfd_get_symcount (output_bfd);
- ret = elf_link_output_sym (flinfo, name, &osym, isec, NULL);
+ ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
if (ret == 0)
return FALSE;
else if (ret == 1)
@@ -9711,7 +9749,8 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
sym.st_value += o->output_offset;
indx = bfd_get_symcount (output_bfd);
- ret = elf_link_output_sym (flinfo, name, &sym, o, NULL);
+ ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
+ NULL);
if (ret == 0)
return FALSE;
else if (ret == 1)
@@ -10186,8 +10225,9 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
}
indx = bfd_get_symcount (output_bfd);
- ret = elf_link_output_sym (flinfo, name, &sym, sec,
- NULL);
+ ret = elf_link_output_symstrtab (flinfo, name,
+ &sym, sec,
+ NULL);
if (ret == 0)
return FALSE;
else if (ret == 1)
@@ -10634,7 +10674,7 @@ elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
asection *o;
if (flinfo->symstrtab != NULL)
- _bfd_stringtab_free (flinfo->symstrtab);
+ _bfd_elf_strtab_free (flinfo->symstrtab);
if (flinfo->contents != NULL)
free (flinfo->contents);
if (flinfo->external_relocs != NULL)
@@ -10651,8 +10691,6 @@ elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
free (flinfo->indices);
if (flinfo->sections != NULL)
free (flinfo->sections);
- if (flinfo->symbuf != NULL)
- free (flinfo->symbuf);
if (flinfo->symshndxbuf != NULL)
free (flinfo->symshndxbuf);
for (o = obfd->sections; o != NULL; o = o->next)
@@ -10710,7 +10748,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
flinfo.info = info;
flinfo.output_bfd = abfd;
- flinfo.symstrtab = _bfd_elf_stringtab_init ();
+ flinfo.symstrtab = _bfd_elf_strtab_init ();
if (flinfo.symstrtab == NULL)
return FALSE;
@@ -10737,10 +10775,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
flinfo.internal_syms = NULL;
flinfo.indices = NULL;
flinfo.sections = NULL;
- flinfo.symbuf = NULL;
flinfo.symshndxbuf = NULL;
- flinfo.symbuf_count = 0;
- flinfo.shndxbuf_size = 0;
flinfo.filesym_count = 0;
/* The object attributes have been merged. Remove the input
@@ -10972,27 +11007,18 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
/* sh_offset is set just below. */
symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
- /* Allocate a buffer to hold swapped out symbols. This is to avoid
- continuously seeking to the right position in the file. */
- if (! info->keep_memory || max_sym_count < 20)
- flinfo.symbuf_size = 20;
- else
- flinfo.symbuf_size = max_sym_count;
- amt = flinfo.symbuf_size;
- amt *= bed->s->sizeof_sym;
- flinfo.symbuf = (bfd_byte *) bfd_malloc (amt);
- if (flinfo.symbuf == NULL)
+ if (max_sym_count < 20)
+ max_sym_count = 20;
+ elf_hash_table (info)->strtabsize = max_sym_count;
+ amt = max_sym_count * sizeof (struct elf_sym_strtab);
+ elf_hash_table (info)->strtab
+ = (struct elf_sym_strtab *) bfd_malloc (amt);
+ if (elf_hash_table (info)->strtab == NULL)
goto error_return;
- if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
- {
- /* Wild guess at number of output symbols. realloc'd as needed. */
- amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
- flinfo.shndxbuf_size = amt;
- amt *= sizeof (Elf_External_Sym_Shndx);
- flinfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
- if (flinfo.symshndxbuf == NULL)
- goto error_return;
- }
+ /* The real buffer will be allocated in elf_link_swap_symbols_out. */
+ flinfo.symshndxbuf
+ = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
+ ? (Elf_External_Sym_Shndx *) -1 : NULL);
if (info->strip != strip_all || emit_relocs)
{
@@ -11012,8 +11038,8 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
elfsym.st_other = 0;
elfsym.st_shndx = SHN_UNDEF;
elfsym.st_target_internal = 0;
- if (elf_link_output_sym (&flinfo, NULL, &elfsym, bfd_und_section_ptr,
- NULL) != 1)
+ if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
+ bfd_und_section_ptr, NULL) != 1)
goto error_return;
/* Output a symbol for each section. We output these even if we are
@@ -11036,7 +11062,8 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
elfsym.st_shndx = i;
if (!info->relocatable)
elfsym.st_value = o->vma;
- if (elf_link_output_sym (&flinfo, NULL, &elfsym, o, NULL) != 1)
+ if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
+ NULL) != 1)
goto error_return;
}
}
@@ -11253,7 +11280,8 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
struct elf_link_hash_entry *);
if (! ((*bed->elf_backend_output_arch_local_syms)
- (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
+ (abfd, info, &flinfo,
+ (out_sym_func) elf_link_output_symstrtab)))
return FALSE;
}
@@ -11364,12 +11392,16 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
struct elf_link_hash_entry *);
if (! ((*bed->elf_backend_output_arch_syms)
- (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
+ (abfd, info, &flinfo,
+ (out_sym_func) elf_link_output_symstrtab)))
return FALSE;
}
- /* Flush all symbols to the file. */
- if (! elf_link_flush_output_syms (&flinfo, bed))
+ /* Finalize the .strtab section. */
+ _bfd_elf_strtab_finalize (flinfo.symstrtab);
+
+ /* Swap out the .strtab section. */
+ if (!elf_link_swap_symbols_out (&flinfo))
return FALSE;
/* Now we know the size of the symtab section. */
@@ -11402,7 +11434,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
symstrtab_hdr->sh_type = SHT_STRTAB;
symstrtab_hdr->sh_flags = 0;
symstrtab_hdr->sh_addr = 0;
- symstrtab_hdr->sh_size = _bfd_stringtab_size (flinfo.symstrtab);
+ symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
symstrtab_hdr->sh_entsize = 0;
symstrtab_hdr->sh_link = 0;
symstrtab_hdr->sh_info = 0;
@@ -11414,7 +11446,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
elf_next_file_pos (abfd) = off;
if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
- || ! _bfd_stringtab_emit (abfd, flinfo.symstrtab))
+ || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
return FALSE;
}