This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
[PATCH] New option --print-gc-sections
- From: Eric Botcazou <ebotcazou at adacore dot com>
- To: binutils at gcc dot gnu dot org
- Cc: Arnaud Charlet <charlet at adacore dot com>
- Date: Tue, 28 Mar 2006 11:31:41 +0200
- Subject: [PATCH] New option --print-gc-sections
Hi,
We'd like to introduce a --print-gc-sections option, to be used in conjunction
with --gc-sections, now that its output is accurate thanks to HJ's work.
The warning/error message format recently changed and is now one-lined, so I'm
proposing the following format:
1. "objfile:location GC function" for .text.function sections.
2. "objfile GC section" for other sections.
For example, on an Ada example with --print-gc-sections --demangle=gnat:
./u.o GC .text
./u.o GC .bss
./u.o:/home/eric/gnat/bugs/F115-010/u.adb:18 GC u.unused
./u.o GC .gcc_except_table.u__unused
./u.o:/home/eric/gnat/bugs/F115-010/u.adb:27 GC u.unused_2
./ma.o GC .text
./ma.o GC .data
./ma.o GC .bss
Tentative patch attached, tested on AMD64/Linux. Comments?
2006-03-28 Eric Botcazou <ebotcazou@adacore.com>
bfd/
* elflink.c (elf_gc_sweep): Invoke the gc_section link callback
on excluded sections.
include/
* bfdlink.h (struct bfd_link_info): New flag print_gc_sections.
(struct bfd_link_callbacks): New method gc_section.
ld/
* ld.texinfo (Command Line Options): Document --print-gc-sections.
* ldmain.c (gc_section): New callback.
(link_callbacks): Add gc_section.
* lexsup.c (enum option_values): New value OPTION_PRINT_GC_SECTIONS.
(ld_options): Add --print-gc-sections.
(parse_args): Handle OPTION_PRINT_GC_SECTIONS.
--
Eric Botcazou
Index: bfd/elflink.c
===================================================================
RCS file: /cvs/src/src/bfd/elflink.c,v
retrieving revision 1.207
diff -u -p -r1.207 elflink.c
--- bfd/elflink.c 17 Mar 2006 18:37:21 -0000 1.207
+++ bfd/elflink.c 28 Mar 2006 09:06:30 -0000
@@ -8930,6 +8930,9 @@ elf_gc_sweep (bfd *abfd, struct bfd_link
to remove a section from the output. */
o->flags |= SEC_EXCLUDE;
+ if (! (*info->callbacks->gc_section) (info, sub, o))
+ return FALSE;
+
/* But we also have to update some of the relocation
info we collected before. */
if (gc_sweep_hook
Index: include/bfdlink.h
===================================================================
RCS file: /cvs/src/src/include/bfdlink.h,v
retrieving revision 1.58
diff -u -p -r1.58 bfdlink.h
--- include/bfdlink.h 3 Nov 2005 02:52:51 -0000 1.58
+++ include/bfdlink.h 28 Mar 2006 09:06:30 -0000
@@ -327,6 +327,9 @@ struct bfd_link_info
/* TRUE if unreferenced sections should be removed. */
unsigned int gc_sections: 1;
+ /* TRUE if we should print the list of garbage-collected sections. */
+ unsigned int print_gc_sections: 1;
+
/* What to do with unresolved symbols in an object file.
When producing executables the default is GENERATE_ERROR.
When producing shared libraries the default is IGNORE. The
@@ -534,6 +537,10 @@ struct bfd_link_callbacks
bfd_boolean (*notice)
(struct bfd_link_info *, const char *name,
bfd *abfd, asection *section, bfd_vma address);
+ /* A function which is called when a section is garbage-collected.
+ ABFD and SECTION are the section. */
+ bfd_boolean (*gc_section)
+ (struct bfd_link_info *, bfd *abfd, asection *section);
/* General link info message. */
void (*einfo)
(const char *fmt, ...);
Index: ld/ld.texinfo
===================================================================
RCS file: /cvs/src/src/ld/ld.texinfo,v
retrieving revision 1.157
diff -u -p -r1.157 ld.texinfo
--- ld/ld.texinfo 3 Mar 2006 09:31:59 -0000 1.157
+++ ld/ld.texinfo 28 Mar 2006 09:06:31 -0000
@@ -1220,6 +1220,11 @@ with @samp{-r}. The default behaviour (o
collection) can be restored by specifying @samp{--no-gc-sections} on
the command line.
+@kindex --print-gc-sections
+@cindex garbage collection
+@item --print-gc-sections
+Print the list of garbage-collected sections with @samp{--gc-sections}.
+
@cindex help
@cindex usage
@kindex --help
Index: ld/ldmain.c
===================================================================
RCS file: /cvs/src/src/ld/ldmain.c,v
retrieving revision 1.105
diff -u -p -r1.105 ldmain.c
--- ld/ldmain.c 16 Mar 2006 12:20:16 -0000 1.105
+++ ld/ldmain.c 28 Mar 2006 09:06:31 -0000
@@ -147,6 +147,8 @@ static bfd_boolean unattached_reloc
(struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
static bfd_boolean notice
(struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
+static bfd_boolean gc_section
+ (struct bfd_link_info *, bfd *, asection *);
static struct bfd_link_callbacks link_callbacks =
{
@@ -161,6 +163,7 @@ static struct bfd_link_callbacks link_ca
reloc_dangerous,
unattached_reloc,
notice,
+ gc_section,
einfo
};
@@ -316,6 +319,7 @@ main (int argc, char **argv)
link_info.need_relax_finalize = FALSE;
link_info.warn_shared_textrel = FALSE;
link_info.gc_sections = FALSE;
+ link_info.print_gc_sections = FALSE;
ldfile_add_arch ("");
@@ -1526,3 +1530,22 @@ notice (struct bfd_link_info *info,
return TRUE;
}
+
+/* This is called when a section is garbage-collected. */
+
+static bfd_boolean
+gc_section (struct bfd_link_info *info,
+ bfd *abfd,
+ asection *section)
+{
+ if (! info->print_gc_sections)
+ return TRUE;
+
+ if (strncmp (section->name, ".text.", 6) == 0)
+ einfo ("%D GC %s\n", abfd, section, (bfd_vma) 0,
+ demangle (section->name+6));
+ else
+ einfo ("%B GC %A\n", abfd, section);
+
+ return TRUE;
+}
Index: ld/lexsup.c
===================================================================
RCS file: /cvs/src/src/ld/lexsup.c,v
retrieving revision 1.87
diff -u -p -r1.87 lexsup.c
--- ld/lexsup.c 30 Oct 2005 18:08:52 -0000 1.87
+++ ld/lexsup.c 28 Mar 2006 09:06:31 -0000
@@ -124,6 +124,7 @@ enum option_values
OPTION_FORCE_EXE_SUFFIX,
OPTION_GC_SECTIONS,
OPTION_NO_GC_SECTIONS,
+ OPTION_PRINT_GC_SECTIONS,
OPTION_HASH_SIZE,
OPTION_CHECK_SECTIONS,
OPTION_NO_CHECK_SECTIONS,
@@ -421,6 +422,8 @@ static const struct ld_option ld_options
{ {"oformat", required_argument, NULL, OPTION_OFORMAT},
'\0', N_("TARGET"), N_("Specify target of output file"),
EXACTLY_TWO_DASHES },
+ { {"print-gc-sections", no_argument, NULL, OPTION_PRINT_GC_SECTIONS},
+ '\0', NULL, N_("Print garbage-collected sections"), TWO_DASHES },
{ {"qmagic", no_argument, NULL, OPTION_IGNORE},
'\0', NULL, N_("Ignored for Linux compatibility"), ONE_DASH },
{ {"reduce-memory-overheads", no_argument, NULL,
@@ -952,6 +955,9 @@ parse_args (unsigned argc, char **argv)
case OPTION_OFORMAT:
lang_add_output_format (optarg, NULL, NULL, 0);
break;
+ case OPTION_PRINT_GC_SECTIONS:
+ link_info.print_gc_sections = TRUE;
+ break;
case 'q':
link_info.emitrelocations = TRUE;
break;