This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
[PATCH] Deleting discardable stabs
- To: binutils at sources dot redhat dot com
- Subject: [PATCH] Deleting discardable stabs
- From: Daniel Jacobowitz <drow at mvista dot com>
- Date: Sun, 11 Nov 2001 22:03:40 -0500
After a little while reacquainting myself with BFD, I was able to get the
desired effects out. Entire stabs N_FUN..N_FUN blocks, as well as
N_LCSYM/N_STSYM markers outside of functions, can be deleted if they
reference a symbol in a discarded section.
Apart from a testcase (which I don't know how to write. Any suggestions?
If I assume GCC and stabs, I could do it...) I think this is done. I'd
appreciate feedback. It should work for at least non-elf_bad_symtab()
cases; no regressions.
--
Daniel Jacobowitz Carnegie Mellon University
MontaVista Software Debian GNU/Linux Developer
2001-11-11 Daniel Jacobowitz <drow@mvista.com>
* bfd-in.h (bfd_elf32_discard_stabs): Add prototype.
(bfd_elf64_discard_stabs): Likewise.
* bfd-in2.h: Regenerate.
* elfcode.h (elf_bfd_discard_stabs): Define.
* elflink.h (struct reloc_cookie): New.
(elf_reloc_symbol_deleted_p): New.
(elf_bfd_discard_stabs): New.
* stabs.c (_bfd_discard_section_stabs): New.
2001-11-11 Daniel Jacobowitz <drow@mvista.com>
* emultempl/elf32.em (gld${EMULATION_NAME}_finish): New.
(struct ld_emulation_xfer_struct): Use it.
Index: bfd/bfd-in.h
===================================================================
RCS file: /cvs/src/src/bfd/bfd-in.h,v
retrieving revision 1.35
diff -p -u -r1.35 bfd-in.h
--- bfd-in.h 2001/10/02 05:58:37 1.35
+++ bfd-in.h 2001/11/12 02:40:31
@@ -635,6 +635,10 @@ extern void bfd_elf_set_dt_needed_soname
extern const char *bfd_elf_get_dt_soname PARAMS ((bfd *));
extern struct bfd_link_needed_list *bfd_elf_get_runpath_list
PARAMS ((bfd *, struct bfd_link_info *));
+extern boolean bfd_elf32_discard_stabs
+ PARAMS ((struct bfd_link_info *));
+extern boolean bfd_elf64_discard_stabs
+ PARAMS ((struct bfd_link_info *));
/* Return an upper bound on the number of bytes required to store a
copy of ABFD's program header table entries. Return -1 if an error
Index: bfd/bfd-in2.h
===================================================================
RCS file: /cvs/src/src/bfd/bfd-in2.h,v
retrieving revision 1.126
diff -p -u -r1.126 bfd-in2.h
--- bfd-in2.h 2001/10/30 15:20:02 1.126
+++ bfd-in2.h 2001/11/12 02:40:31
@@ -641,6 +641,10 @@ extern void bfd_elf_set_dt_needed_soname
extern const char *bfd_elf_get_dt_soname PARAMS ((bfd *));
extern struct bfd_link_needed_list *bfd_elf_get_runpath_list
PARAMS ((bfd *, struct bfd_link_info *));
+extern boolean bfd_elf32_discard_stabs
+ PARAMS ((struct bfd_link_info *));
+extern boolean bfd_elf64_discard_stabs
+ PARAMS ((struct bfd_link_info *));
/* Return an upper bound on the number of bytes required to store a
copy of ABFD's program header table entries. Return -1 if an error
Index: bfd/elfcode.h
===================================================================
RCS file: /cvs/src/src/bfd/elfcode.h,v
retrieving revision 1.23
diff -p -u -r1.23 elfcode.h
--- elfcode.h 2001/10/19 16:29:12 1.23
+++ elfcode.h 2001/11/12 02:40:31
@@ -123,6 +123,7 @@ Foundation, Inc., 59 Temple Place - Suit
#define elf_slurp_reloc_table NAME(bfd_elf,slurp_reloc_table)
#define elf_link_create_dynamic_sections \
NAME(bfd_elf,link_create_dynamic_sections)
+#define elf_bfd_discard_stabs NAME(bfd_elf,discard_stabs)
#define elf_link_record_dynamic_symbol _bfd_elf_link_record_dynamic_symbol
#define elf_bfd_final_link NAME(bfd_elf,bfd_final_link)
#define elf_create_pointer_linker_section NAME(bfd_elf,create_pointer_linker_section)
Index: bfd/elflink.h
===================================================================
RCS file: /cvs/src/src/bfd/elflink.h,v
retrieving revision 1.123
diff -p -u -r1.123 elflink.h
--- elflink.h 2001/11/10 00:23:35 1.123
+++ elflink.h 2001/11/12 02:40:31
@@ -75,6 +75,8 @@ static int elf_link_sort_cmp2
PARAMS ((const void *, const void *));
static size_t elf_link_sort_relocs
PARAMS ((bfd *, struct bfd_link_info *, asection **));
+static boolean elf_reloc_symbol_deleted_p
+ PARAMS ((bfd_vma, PTR));
/* Given an ELF BFD, add symbols to the global hash table as
appropriate. */
@@ -7755,4 +7757,180 @@ elf_collect_hash_codes (h, data)
free (alc);
return true;
+}
+
+struct reloc_cookie
+{
+ Elf_Internal_Rela *rels, *rel, *relend;
+ Elf_External_Sym *locsyms;
+ bfd *abfd;
+ size_t locsymcount;
+ size_t extsymoff;
+ struct elf_link_hash_entry **sym_hashes;
+ boolean bad_symtab;
+};
+
+static boolean
+elf_reloc_symbol_deleted_p (offset, cookie)
+ bfd_vma offset;
+ PTR cookie;
+{
+ struct reloc_cookie *rcookie = (struct reloc_cookie *)cookie;
+
+ if (rcookie->bad_symtab)
+ rcookie->rel = rcookie->rels;
+
+ for (; rcookie->rel < rcookie->relend; rcookie->rel++)
+ {
+ unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
+
+ if (! rcookie->bad_symtab)
+ if (rcookie->rel->r_offset > offset)
+ return false;
+ if (rcookie->rel->r_offset != offset)
+ continue;
+
+ rcookie->rel ++;
+
+ /* If rcookie->bad_symtab, we should check
+ finfo->sections[r_symndx] == NULL by analogy with
+ elf_link_input_bfd. That's not available yet,
+ so we might miss some. */
+ if (r_symndx >= rcookie->locsymcount)
+ {
+ struct elf_link_hash_entry *h;
+
+ h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
+
+ while (h->root.type == bfd_link_hash_indirect
+ || h->root.type == bfd_link_hash_warning)
+ h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+ if ((h->root.type == bfd_link_hash_defined
+ || h->root.type == bfd_link_hash_defweak)
+ && ! bfd_is_abs_section (h->root.u.def.section)
+ && bfd_is_abs_section (h->root.u.def.section
+ ->output_section))
+ return true;
+ else
+ return false;
+ }
+ else if (rcookie->locsyms)
+ {
+ /* It's not a relocation against a global symbol,
+ but it could be a relocation against a section
+ symbol for a discarded section. */
+ Elf_Internal_Sym isym;
+ asection *isec;
+
+ elf_swap_symbol_in (rcookie->abfd, rcookie->locsyms + r_symndx,
+ &isym);
+
+ /* Need to: get the symbol; get the section. */
+ if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
+ {
+ isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
+ if (isec != NULL
+ && ELF_ST_TYPE (isym.st_info) == STT_SECTION
+ && ! bfd_is_abs_section (isec)
+ && bfd_is_abs_section (isec->output_section))
+ return true;
+ }
+ }
+ return false;
+ }
+ return false;
+}
+
+/* Returns true if any section's size was changed. */
+
+boolean
+elf_bfd_discard_stabs (info)
+ struct bfd_link_info *info;
+{
+ struct reloc_cookie cookie;
+ asection *o;
+ Elf_Internal_Shdr *symtab_hdr;
+ struct elf_backend_data *bed;
+ bfd *abfd;
+ boolean ret = false;
+
+ if (info->relocateable
+ || info->traditional_format
+ || info->hash->creator->flavour != bfd_target_elf_flavour
+ || ! is_elf_hash_table (info)
+ || info->strip == strip_all
+ || info->strip == strip_debugger)
+ return false;
+ for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
+ {
+ if ((abfd->flags & DYNAMIC) != 0)
+ continue;
+
+ bed = get_elf_backend_data (abfd);
+ symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+
+ cookie.abfd = abfd;
+ cookie.sym_hashes = elf_sym_hashes (abfd);
+ cookie.bad_symtab = elf_bad_symtab (abfd);
+ if (cookie.bad_symtab)
+ {
+ cookie.locsymcount =
+ symtab_hdr->sh_size / sizeof (Elf_External_Sym);
+ cookie.extsymoff = 0;
+ }
+ else
+ {
+ cookie.locsymcount = symtab_hdr->sh_info;
+ cookie.extsymoff = symtab_hdr->sh_info;
+ }
+
+ if (symtab_hdr->contents)
+ cookie.locsyms = (Elf_External_Sym *) symtab_hdr->contents;
+ else if (cookie.locsymcount == 0)
+ cookie.locsyms = NULL;
+ else
+ {
+ bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
+ cookie.locsyms = bfd_malloc (amt);
+ if (cookie.locsyms == NULL
+ || bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
+ || bfd_bread (cookie.locsyms, amt, abfd) != amt)
+ {
+ /* Something is very wrong - but we can still do our job for
+ global symbols, so don't give up. */
+ if (cookie.locsyms)
+ free (cookie.locsyms);
+ cookie.locsyms = NULL;
+ }
+ else
+ {
+ symtab_hdr->contents = (unsigned char *) cookie.locsyms;
+ }
+ }
+
+ o = bfd_get_section_by_name (abfd, ".stab");
+ if (! o)
+ continue;
+
+ cookie.rels = (NAME(_bfd_elf,link_read_relocs)
+ (abfd, o, (PTR) NULL,
+ (Elf_Internal_Rela *) NULL,
+ info->keep_memory));
+ if (! cookie.rels)
+ continue;
+ cookie.rel = cookie.rels;
+ cookie.relend =
+ cookie.rels + o->reloc_count * bed->s->int_rels_per_ext_rel;
+
+ if (_bfd_discard_section_stabs (abfd, o,
+ elf_section_data (o)->stab_info,
+ elf_reloc_symbol_deleted_p,
+ &cookie))
+ ret = true;
+
+ if (! info->keep_memory)
+ free (cookie.rels);
+ }
+ return ret;
}
Index: bfd/libbfd-in.h
===================================================================
RCS file: /cvs/src/src/bfd/libbfd-in.h,v
retrieving revision 1.16
diff -p -u -r1.16 libbfd-in.h
--- libbfd-in.h 2001/10/01 14:03:35 1.16
+++ libbfd-in.h 2001/11/12 02:40:31
@@ -446,6 +446,11 @@ extern bfd_reloc_status_type _bfd_reloca
extern boolean _bfd_link_section_stabs
PARAMS ((bfd *, PTR *, asection *, asection *, PTR *));
+/* Eliminate stabs for discarded functions and symbols. */
+extern boolean _bfd_discard_section_stabs
+ PARAMS ((bfd *, asection *, PTR,
+ boolean (*) (bfd_vma, PTR), PTR));
+
/* Write out the .stab section when linking stabs in sections. */
extern boolean _bfd_write_section_stabs
Index: bfd/libbfd.h
===================================================================
RCS file: /cvs/src/src/bfd/libbfd.h,v
retrieving revision 1.48
diff -p -u -r1.48 libbfd.h
--- libbfd.h 2001/10/30 15:20:02 1.48
+++ libbfd.h 2001/11/12 02:40:31
@@ -451,6 +451,11 @@ extern bfd_reloc_status_type _bfd_reloca
extern boolean _bfd_link_section_stabs
PARAMS ((bfd *, PTR *, asection *, asection *, PTR *));
+/* Eliminate stabs for discarded functions and symbols. */
+extern boolean _bfd_discard_section_stabs
+ PARAMS ((bfd *, asection *, PTR,
+ boolean (*) (bfd_vma, PTR), PTR));
+
/* Write out the .stab section when linking stabs in sections. */
extern boolean _bfd_write_section_stabs
Index: bfd/stabs.c
===================================================================
RCS file: /cvs/src/src/bfd/stabs.c,v
retrieving revision 1.7
diff -p -u -r1.7 stabs.c
--- stabs.c 2001/09/19 05:33:13 1.7
+++ stabs.c 2001/11/12 02:40:31
@@ -502,6 +502,176 @@ _bfd_link_section_stabs (abfd, psinfo, s
return false;
}
+
+/* This function is called for each input file before the stab
+ section is relocated. It discards stab entries for discarded
+ functions and variables. The function returns true iff
+ any entries have been deleted.
+*/
+
+boolean
+_bfd_discard_section_stabs (abfd, stabsec, psecinfo,
+ reloc_symbol_deleted_p, cookie)
+ bfd *abfd;
+ asection *stabsec;
+ PTR psecinfo;
+ boolean (*reloc_symbol_deleted_p) (bfd_vma, PTR);
+ PTR cookie;
+{
+ bfd_size_type count, amt;
+ struct stab_section_info *secinfo;
+ bfd_byte *stabbuf = NULL;
+ bfd_byte *sym, *symend;
+ bfd_size_type skip;
+ bfd_size_type *pstridx;
+ int deleting;
+
+ if (stabsec->_raw_size == 0)
+ {
+ /* This file does not contain stabs debugging information. */
+ return false;
+ }
+
+ if (stabsec->_raw_size % STABSIZE != 0)
+ {
+ /* Something is wrong with the format of these stab symbols.
+ Don't try to optimize them. */
+ return false;
+ }
+
+ if ((stabsec->output_section != NULL
+ && bfd_is_abs_section (stabsec->output_section)))
+ {
+ /* At least one of the sections is being discarded from the
+ link, so we should just ignore them. */
+ return false;
+ }
+
+ /* We should have initialized our data in _bfd_link_stab_sections.
+ If there was some bizarre error reading the string sections, though,
+ we might not have. Bail rather than asserting. */
+ if (psecinfo == NULL)
+ return false;
+
+ count = stabsec->_raw_size / STABSIZE;
+ secinfo = (struct stab_section_info *) psecinfo;
+
+ /* Read the stabs information from abfd. */
+
+ stabbuf = (bfd_byte *) bfd_malloc (stabsec->_raw_size);
+ if (stabbuf == NULL)
+ goto error_return;
+
+ if (! bfd_get_section_contents (abfd, stabsec, stabbuf, (bfd_vma) 0,
+ stabsec->_raw_size))
+ goto error_return;
+
+ /* Look through the stabs symbols and discard any information for
+ discarded functions. */
+
+ skip = 0;
+ deleting = -1;
+
+ symend = stabbuf + stabsec->_raw_size;
+ for (sym = stabbuf, pstridx = secinfo->stridxs;
+ sym < symend;
+ sym += STABSIZE, ++pstridx)
+ {
+ int type;
+
+ if (*pstridx == (bfd_size_type) -1)
+ {
+ /* This stab was deleted in a previous pass. */
+ continue;
+ }
+
+ type = sym[TYPEOFF];
+
+ if (type == N_FUN)
+ {
+ int strx = bfd_get_32 (abfd, sym + STRDXOFF);
+
+ if (strx == 0)
+ {
+ if (deleting)
+ {
+ skip++;
+ *pstridx = -1;
+ }
+ deleting = -1;
+ continue;
+ }
+ deleting = 0;
+ if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
+ deleting = 1;
+ }
+
+ if (deleting == 1)
+ {
+ *pstridx = -1;
+ skip++;
+ }
+ else if (deleting == -1)
+ {
+ /* Outside of a function. Check for deleted variables. */
+ if (type == N_STSYM || type == N_LCSYM)
+ if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
+ {
+ *pstridx = -1;
+ skip ++;
+ }
+ /* We should also check for N_GSYM entries which reference a
+ deleted global, but those are less harmful to debuggers
+ and would require parsing the stab strings. */
+ }
+ }
+
+ free (stabbuf);
+ stabbuf = NULL;
+
+ /* Shrink the stabsec as needed. */
+ stabsec->_cooked_size -= skip * STABSIZE;
+ if (stabsec->_cooked_size == 0)
+ stabsec->flags |= SEC_EXCLUDE;
+
+ /* Recalculate the `cumulative_skips' array now that stabs have been
+ deleted for this section. */
+
+ if (skip != 0)
+ {
+ bfd_size_type i, offset;
+ bfd_size_type *pskips;
+
+ if (secinfo->cumulative_skips == NULL)
+ {
+ amt = count * sizeof (bfd_size_type);
+ secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
+ if (secinfo->cumulative_skips == NULL)
+ goto error_return;
+ }
+
+ pskips = secinfo->cumulative_skips;
+ pstridx = secinfo->stridxs;
+ offset = 0;
+
+ for (i = 0; i < count; i++, pskips++, pstridx++)
+ {
+ *pskips = offset;
+ if (*pstridx == (bfd_size_type) -1)
+ offset += STABSIZE;
+ }
+
+ BFD_ASSERT (offset != 0);
+ }
+
+ return (skip > 0);
+
+ error_return:
+ if (stabbuf != NULL)
+ free (stabbuf);
+ return false;
+}
+
/* Write out the stab section. This is called with the relocated
contents. */
Index: ld/emultempl/elf32.em
===================================================================
RCS file: /cvs/src/src/ld/emultempl/elf32.em,v
retrieving revision 1.64
diff -p -u -r1.64 elf32.em
--- elf32.em 2001/10/15 23:28:21 1.64
+++ elf32.em 2001/11/12 02:40:32
@@ -80,6 +80,8 @@ static asection *output_prev_sec_find
PARAMS ((lang_output_section_statement_type *));
static boolean gld${EMULATION_NAME}_place_orphan
PARAMS ((lang_input_statement_type *, asection *));
+static void gld${EMULATION_NAME}_finish
+ PARAMS ((void));
static char *gld${EMULATION_NAME}_get_script
PARAMS ((int *isfile));
@@ -1319,6 +1321,27 @@ gld${EMULATION_NAME}_place_orphan (file,
return true;
}
+boolean elf_bfd_discard_stabs (struct bfd_link_info *);
+
+static void
+gld${EMULATION_NAME}_finish ()
+{
+ ${LDEMUL_FINISH+${LDEMUL_FINISH} ();}
+
+ if (bfd_elf${ELFSIZE}_discard_stabs (&link_info))
+ {
+ /* Resize the sections. */
+ lang_size_sections (stat_ptr->head, abs_output_section,
+ &stat_ptr->head, 0, (bfd_vma) 0, false);
+
+ /* Redo special stuff. */
+ ldemul_after_allocation ();
+
+ /* Do the assignments again. */
+ lang_do_assignments (stat_ptr->head, abs_output_section,
+ (fill_type) 0, (bfd_vma) 0);
+ }
+}
EOF
fi
@@ -1618,7 +1641,7 @@ struct ld_emulation_xfer_struct ld_${EMU
${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
"${EMULATION_NAME}",
"${OUTPUT_FORMAT}",
- ${LDEMUL_FINISH-NULL},
+ gld${EMULATION_NAME}_finish,
${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},