public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] New option --print-gc-sections
@ 2006-03-28 12:04 Eric Botcazou
  2006-03-29 14:31 ` Alan Modra
  0 siblings, 1 reply; 14+ messages in thread
From: Eric Botcazou @ 2006-03-28 12:04 UTC (permalink / raw)
  To: binutils; +Cc: Arnaud Charlet

[-- Attachment #1: Type: text/plain, Size: 1345 bytes --]

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

[-- Attachment #2: f115-010_217.diff --]
[-- Type: text/x-diff, Size: 5267 bytes --]

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;

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-03-28 12:04 [PATCH] New option --print-gc-sections Eric Botcazou
@ 2006-03-29 14:31 ` Alan Modra
  2006-04-03  6:36   ` Eric Botcazou
  0 siblings, 1 reply; 14+ messages in thread
From: Alan Modra @ 2006-03-29 14:31 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: binutils, Arnaud Charlet

On Tue, Mar 28, 2006 at 11:31:41AM +0200, Eric Botcazou wrote:
> We'd like to introduce a --print-gc-sections option

Actually, you can already glean this from a link map.  GC'd sections
will be those that are shown with zero size.  Perhaps you could instead
enhance ldlang.c:print_input_section a little to flag them as being
discarded, like the following (totally untested) patch.

Index: ld/ldlang.c
===================================================================
RCS file: /cvs/src/src/ld/ldlang.c,v
retrieving revision 1.213
diff -u -p -r1.213 ldlang.c
--- ld/ldlang.c	16 Mar 2006 12:20:16 -0000	1.213
+++ ld/ldlang.c	29 Mar 2006 12:47:51 -0000
@@ -3511,7 +3511,7 @@ print_input_section (lang_input_section_
 
       minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
 
-      if (size != i->rawsize && i->rawsize != 0)
+      if (size == 0 || (size != i->rawsize && i->rawsize != 0))
 	{
 	  len = SECTION_NAME_MAP_LENGTH + 3;
 #ifdef BFD64
@@ -3525,7 +3525,10 @@ print_input_section (lang_input_section_
 	      --len;
 	    }
 
-	  minfo (_("%W (size before relaxing)\n"), i->rawsize);
+	  if (size == 0)
+	    minfo (_("(discarded)\n"));
+	  else
+	    minfo (_("%W (size before relaxing)\n"), i->rawsize);
 	}
 
       if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-03-29 14:31 ` Alan Modra
@ 2006-04-03  6:36   ` Eric Botcazou
  2006-04-03  7:50     ` Alan Modra
  0 siblings, 1 reply; 14+ messages in thread
From: Eric Botcazou @ 2006-04-03  6:36 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils, Arnaud Charlet

> Actually, you can already glean this from a link map.  GC'd sections
> will be those that are shown with zero size.

That doesn't seem to work.  Even if I remove the "if (size != 0)" at the 
beginning of print_input_section, the interesting sections are not present in 
the link map.

On my example, a grep "unused" should have returned .text.u__unused, 
.text.u__unused2 and .gcc_except_table.u__unused, whereas I only see

 .rel.text.u__unused
                0x0000000008049088        0x0 /usr/lib/crt1.o
 .rel.text.u__unused_2
                0x0000000008049088        0x0 /usr/lib/crt1.o
 .rel.gcc_except_table.u__unused
                0x00000000080490f8        0x0 /usr/lib/crt1.o

-- 
Eric Botcazou

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-03  6:36   ` Eric Botcazou
@ 2006-04-03  7:50     ` Alan Modra
  2006-04-04  7:57       ` Eric Botcazou
  0 siblings, 1 reply; 14+ messages in thread
From: Alan Modra @ 2006-04-03  7:50 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: binutils, Arnaud Charlet

On Mon, Apr 03, 2006 at 08:39:40AM +0200, Eric Botcazou wrote:
> > Actually, you can already glean this from a link map.  GC'd sections
> > will be those that are shown with zero size.
> 
> That doesn't seem to work.

Silly me.  I'd forgotten that discarded linkonce sections don't have a
lang_input_section statement, and the link map is built by looking
through the list of linker statements.  No statement, no mention.

I still like the idea of displaying discarded sections in the link map
output though.  I think it would be useful to do so whenever -Map or -M
is given, so you wouldn't need a new ld option.  You also shouldn't need
any new hooks.  At the end of lang_map, just iterate over input
sections, looking for any that don't make it to the output bfd.

  LANG_FOR_EACH_INPUT_STATEMENT (file)
    {
      asection *s;

      if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
	  || file->just_syms_flag)
	continue;

      for (s = file->the_bfd->sections; s != NULL; s = s->next)
	if (s->output_section == NULL
	    || s->output_section->owner != output_bfd)
	  {
	    /* This section was excluded from the output for some
	       reason.  */
	  }
    }

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-03  7:50     ` Alan Modra
@ 2006-04-04  7:57       ` Eric Botcazou
  2006-04-04  8:30         ` Alan Modra
  0 siblings, 1 reply; 14+ messages in thread
From: Eric Botcazou @ 2006-04-04  7:57 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils, Arnaud Charlet

[-- Attachment #1: Type: text/plain, Size: 760 bytes --]

> I still like the idea of displaying discarded sections in the link map
> output though.  I think it would be useful to do so whenever -Map or -M
> is given, so you wouldn't need a new ld option.  You also shouldn't need
> any new hooks.  At the end of lang_map, just iterate over input
> sections, looking for any that don't make it to the output bfd.

Thanks, works fine.  I've put it between the list of archives members included
in the link and the memory map.  Tested on AMD64/Linux.


2006-04-04  Eric Botcazou  <ebotcazou@adacore.com>

	* ldlang.c (lang_map): Print the list of discarded input sections.
	(print_input_section): Change parameter and print zero-sized sections.
	(print_statement): Adjust call to print_input_section.


-- 
Eric Botcazou

[-- Attachment #2: f115-010-2_217.diff --]
[-- Type: text/x-diff, Size: 2795 bytes --]

Index: ldlang.c
===================================================================
RCS file: /cvs/src/src/ld/ldlang.c,v
retrieving revision 1.213
diff -u -p -r1.213 ldlang.c
--- ldlang.c	16 Mar 2006 12:20:16 -0000	1.213
+++ ldlang.c	4 Apr 2006 07:43:58 -0000
@@ -81,6 +81,7 @@ static void print_statement (lang_statem
 static void print_statement_list (lang_statement_union_type *,
 				  lang_output_section_statement_type *);
 static void print_statements (void);
+static void print_input_section (asection *);
 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
 static void lang_record_phdrs (void);
 static void lang_do_version_exports_section (void);
@@ -1598,6 +1599,22 @@ lang_map (void)
   lang_memory_region_type *m;
   bfd *p;
 
+  fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
+
+  LANG_FOR_EACH_INPUT_STATEMENT (file)
+    {
+      asection *s;
+
+      if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
+	  || file->just_syms_flag)
+	continue;
+
+      for (s = file->the_bfd->sections; s != NULL; s = s->next)
+	if (s->output_section == NULL
+	    || s->output_section->owner != output_bfd)
+	  print_input_section (s);
+    }
+
   minfo (_("\nMemory Configuration\n\n"));
   fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
 	   _("Name"), _("Origin"), _("Length"), _("Attributes"));
@@ -3475,13 +3492,12 @@ print_all_symbols (sec)
 /* Print information about an input section to the map file.  */
 
 static void
-print_input_section (lang_input_section_type *in)
+print_input_section (asection *i)
 {
-  asection *i = in->section;
   bfd_size_type size = i->size;
 
   init_opb ();
-  if (size != 0)
+
     {
       int len;
       bfd_vma addr;
@@ -3501,7 +3517,7 @@ print_input_section (lang_input_section_
 	  ++len;
 	}
 
-      if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
+      if (i->output_section != NULL && i->output_section->owner == output_bfd)
 	addr = i->output_section->vma + i->output_offset;
       else
 	{
@@ -3528,7 +3544,7 @@ print_input_section (lang_input_section_
 	  minfo (_("%W (size before relaxing)\n"), i->rawsize);
 	}
 
-      if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
+      if (i->output_section != NULL && i->output_section->owner == output_bfd)
 	{
 	  if (command_line.reduce_memory_overheads)
 	    bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
@@ -3797,7 +3813,7 @@ print_statement (lang_statement_union_ty
       print_reloc_statement (&s->reloc_statement);
       break;
     case lang_input_section_enum:
-      print_input_section (&s->input_section);
+      print_input_section (s->input_section.section);
       break;
     case lang_padding_statement_enum:
       print_padding_statement (&s->padding_statement);

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-04  7:57       ` Eric Botcazou
@ 2006-04-04  8:30         ` Alan Modra
  2006-04-04  9:08           ` Eric Botcazou
  0 siblings, 1 reply; 14+ messages in thread
From: Alan Modra @ 2006-04-04  8:30 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: binutils, Arnaud Charlet

On Tue, Apr 04, 2006 at 10:00:55AM +0200, Eric Botcazou wrote:
> > I still like the idea of displaying discarded sections in the link map
> > output though.  I think it would be useful to do so whenever -Map or -M
> > is given, so you wouldn't need a new ld option.  You also shouldn't need
> > any new hooks.  At the end of lang_map, just iterate over input
> > sections, looking for any that don't make it to the output bfd.
> 
> Thanks, works fine.  I've put it between the list of archives members included
> in the link and the memory map.  Tested on AMD64/Linux.
> 
> 
> 2006-04-04  Eric Botcazou  <ebotcazou@adacore.com>
> 
> 	* ldlang.c (lang_map): Print the list of discarded input sections.
> 	(print_input_section): Change parameter and print zero-sized sections.
> 	(print_statement): Adjust call to print_input_section.

I think it would be better to only print the header when you actually
have some discarded sections, ie. print header on finding first
discarded section.  Otherwise you ought to print "None" or somesuch.
OK with that change.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-04  8:30         ` Alan Modra
@ 2006-04-04  9:08           ` Eric Botcazou
  2006-04-05  3:34             ` Alan Modra
  0 siblings, 1 reply; 14+ messages in thread
From: Eric Botcazou @ 2006-04-04  9:08 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils, Arnaud Charlet

> I think it would be better to only print the header when you actually
> have some discarded sections, ie. print header on finding first
> discarded section.  Otherwise you ought to print "None" or somesuch.
> OK with that change.

Thanks, installed with the first suggested change.

Would you agree on enhancing addr2line so that it can return the correct 
answer for object files compiled with -ffunction-sections, now that the 
DWARF-2 back-end can deal with them?  That would probably mean having it 
accept an optional section name.

-- 
Eric Botcazou

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-04  9:08           ` Eric Botcazou
@ 2006-04-05  3:34             ` Alan Modra
  2006-04-05  7:48               ` Eric Botcazou
  0 siblings, 1 reply; 14+ messages in thread
From: Alan Modra @ 2006-04-05  3:34 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: binutils, Arnaud Charlet

On Tue, Apr 04, 2006 at 11:12:08AM +0200, Eric Botcazou wrote:
> Would you agree on enhancing addr2line so that it can return the correct 
> answer for object files compiled with -ffunction-sections, now that the 
> DWARF-2 back-end can deal with them?  That would probably mean having it 
> accept an optional section name.

If you want to use addr2line on relocatable object files, I suppose this
is a reasonable idea.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-05  3:34             ` Alan Modra
@ 2006-04-05  7:48               ` Eric Botcazou
  2006-04-05 15:34                 ` Alan Modra
  0 siblings, 1 reply; 14+ messages in thread
From: Eric Botcazou @ 2006-04-05  7:48 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils, Arnaud Charlet

[-- Attachment #1: Type: text/plain, Size: 751 bytes --]

> If you want to use addr2line on relocatable object files, I suppose this
> is a reasonable idea.

First try attached, tested on AMD64/Linux.


2006-04-05  Eric Botcazou  <ebotcazou@adacore.com>

	* addr2line.c (long_options): Add new option 'section'.
	(usage): Document new -j/--section option.
	(find_offset_in_section): New function.
	(translate_addresses): Add 'section' parameter.
	If it is non-null, call find_offset_in_section on it.
	(process_file): Add 'section_name' parameter.
	If it is non-null, look for the section in the BFD object.
	Pass the section to translate_addresses.
	(main): Handle new -j option.
	Pass the section name to process_file.
	* doc/binutils.texi (addr2line): Document new -j/--section option.


-- 
Eric Botcazou

[-- Attachment #2: f115-010-3_217.diff --]
[-- Type: text/x-diff, Size: 7089 bytes --]

Index: addr2line.c
===================================================================
RCS file: /cvs/src/src/binutils/addr2line.c,v
retrieving revision 1.26
diff -u -p -r1.26 addr2line.c
--- addr2line.c	3 Oct 2005 19:37:44 -0000	1.26
+++ addr2line.c	5 Apr 2006 07:37:33 -0000
@@ -56,6 +56,7 @@ static struct option long_options[] =
   {"exe", required_argument, NULL, 'e'},
   {"functions", no_argument, NULL, 'f'},
   {"inlines", no_argument, NULL, 'i'},
+  {"section", required_argument, NULL, 'j'},
   {"target", required_argument, NULL, 'b'},
   {"help", no_argument, NULL, 'H'},
   {"version", no_argument, NULL, 'V'},
@@ -65,8 +66,9 @@ static struct option long_options[] =
 static void usage (FILE *, int);
 static void slurp_symtab (bfd *);
 static void find_address_in_section (bfd *, asection *, void *);
-static void translate_addresses (bfd *);
-static void process_file (const char *, const char *);
+static void find_offset_in_section (bfd *, asection *);
+static void translate_addresses (bfd *, asection *);
+static void process_file (const char *, const char *, const char *);
 \f
 /* Print a usage message to STREAM and exit with STATUS.  */
 
@@ -80,7 +82,8 @@ usage (FILE *stream, int status)
   @<file>                Read options from <file>\n\
   -b --target=<bfdname>  Set the binary file format\n\
   -e --exe=<executable>  Set the input file name (default is a.out)\n\
-  -i --inlines		 Unwind inlined functions\n\
+  -i --inlines           Unwind inlined functions\n\
+  -j --section=<name>    Read section-relative offsets instead of addresses\n\
   -s --basenames         Strip directory names\n\
   -f --functions         Show function names\n\
   -C --demangle[=style]  Demangle function names\n\
@@ -150,11 +153,32 @@ find_address_in_section (bfd *abfd, asec
 				 &filename, &functionname, &line);
 }
 
+/* Look for an offset in a section.  This is directly called.  */
+
+static void
+find_offset_in_section (bfd *abfd, asection *section)
+{
+  bfd_size_type size;
+
+  if (found)
+    return;
+
+  if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
+    return;
+
+  size = bfd_get_section_size (section);
+  if (pc >= size)
+    return;
+
+  found = bfd_find_nearest_line (abfd, section, syms, pc,
+				 &filename, &functionname, &line);
+}
+
 /* Read hexadecimal addresses from stdin, translate into
    file_name:line_number and optionally function name.  */
 
 static void
-translate_addresses (bfd *abfd)
+translate_addresses (bfd *abfd, asection *section)
 {
   int read_stdin = (naddr == 0);
 
@@ -177,7 +201,10 @@ translate_addresses (bfd *abfd)
 	}
 
       found = FALSE;
-      bfd_map_over_sections (abfd, find_address_in_section, NULL);
+      if (section)
+	find_offset_in_section (abfd, section);
+      else
+	bfd_map_over_sections (abfd, find_address_in_section, NULL);
 
       if (! found)
 	{
@@ -237,9 +264,11 @@ translate_addresses (bfd *abfd)
 /* Process a file.  */
 
 static void
-process_file (const char *file_name, const char *target)
+process_file (const char *file_name, const char *section_name,
+	      const char *target)
 {
   bfd *abfd;
+  asection *section;
   char **matching;
 
   if (get_file_size (file_name) < 1)
@@ -250,7 +279,7 @@ process_file (const char *file_name, con
     bfd_fatal (file_name);
 
   if (bfd_check_format (abfd, bfd_archive))
-    fatal (_("%s: can not get addresses from archive"), file_name);
+    fatal (_("%s: cannot get addresses from archive"), file_name);
 
   if (! bfd_check_format_matches (abfd, bfd_object, &matching))
     {
@@ -263,9 +292,18 @@ process_file (const char *file_name, con
       xexit (1);
     }
 
+  if (section_name != NULL)
+    {
+      section = bfd_get_section_by_name (abfd, section_name);
+      if (section == NULL)
+	fatal (_("%s: cannot find section %s"), file_name, section_name);
+    }
+  else
+    section = NULL;
+
   slurp_symtab (abfd);
 
-  translate_addresses (abfd);
+  translate_addresses (abfd, section);
 
   if (syms != NULL)
     {
@@ -276,12 +314,12 @@ process_file (const char *file_name, con
   bfd_close (abfd);
 }
 \f
-int main (int, char **);
 
 int
 main (int argc, char **argv)
 {
   const char *file_name;
+  const char *section_name;
   char *target;
   int c;
 
@@ -303,8 +341,9 @@ main (int argc, char **argv)
   set_default_bfd_target ();
 
   file_name = NULL;
+  section_name = NULL;
   target = NULL;
-  while ((c = getopt_long (argc, argv, "b:Ce:sfHhiVv", long_options, (int *) 0))
+  while ((c = getopt_long (argc, argv, "b:Ce:sfHhij:Vv", long_options, (int *) 0))
 	 != EOF)
     {
       switch (c)
@@ -348,6 +387,9 @@ main (int argc, char **argv)
 	case 'i':
 	  unwind_inlines = TRUE;
 	  break;
+	case 'j':
+	  section_name = optarg;
+	  break;
 	default:
 	  usage (stderr, 1);
 	  break;
@@ -360,7 +402,7 @@ main (int argc, char **argv)
   addr = argv + optind;
   naddr = argc - optind;
 
-  process_file (file_name, target);
+  process_file (file_name, section_name, target);
 
   return 0;
 }
Index: doc/binutils.texi
===================================================================
RCS file: /cvs/src/src/binutils/doc/binutils.texi,v
retrieving revision 1.89
diff -u -p -r1.89 binutils.texi
--- doc/binutils.texi	9 Mar 2006 21:50:53 -0000	1.89
+++ doc/binutils.texi	5 Apr 2006 07:37:34 -0000
@@ -2601,6 +2601,7 @@ addr2line [@option{-b} @var{bfdname}|@op
           [@option{-e} @var{filename}|@option{--exe=}@var{filename}]
           [@option{-f}|@option{--functions}] [@option{-s}|@option{--basename}]
           [@option{-i}|@option{--inlines}]
+          [@option{-j}|@option{--section=}@var{name}]
           [@option{-H}|@option{--help}] [@option{-V}|@option{--version}]
           [addr addr @dots{}]
 @c man end
@@ -2608,13 +2609,14 @@ addr2line [@option{-b} @var{bfdname}|@op
 
 @c man begin DESCRIPTION addr2line
 
-@command{addr2line} translates program addresses into file names and line
-numbers.  Given an address and an executable, it uses the debugging
-information in the executable to figure out which file name and line
-number are associated with a given address.
-
-The executable to use is specified with the @option{-e} option.  The
-default is the file @file{a.out}.
+@command{addr2line} translates addresses into file names and line numbers.
+Given an address in an executable or an offset in a section of a relocatable
+object, it uses the debugging information to figure out which file name and
+line number are associated with it.
+
+The executable or relocatable object to use is specified with the @option{-e}
+option.  The default is the file @file{a.out}.  The section in the relocatable
+object to use is specified with the @option{-j} option.
 
 @command{addr2line} has two modes of operation.
 
@@ -2682,6 +2684,10 @@ function will also be printed.  For exam
 @code{callee1} which inlines @code{callee2}, and address is from
 @code{callee2}, the source information for @code{callee1} and @code{main}
 will also be printed.
+
+@item -j
+@itemx --section
+Read offsets relative to the specified section instead of absolute addresses.
 @end table
 
 @c man end

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-05  7:48               ` Eric Botcazou
@ 2006-04-05 15:34                 ` Alan Modra
  2006-04-05 15:52                   ` Eric Botcazou
  0 siblings, 1 reply; 14+ messages in thread
From: Alan Modra @ 2006-04-05 15:34 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: binutils, Arnaud Charlet

On Wed, Apr 05, 2006 at 09:51:19AM +0200, Eric Botcazou wrote:
> +static void
> +find_offset_in_section (bfd *abfd, asection *section)
> +{
> +  bfd_size_type size;
> +
> +  if (found)
> +    return;
> +
> +  if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
> +    return;
> +
> +  size = bfd_get_section_size (section);
> +  if (pc >= size)
> +    return;
> +
> +  found = bfd_find_nearest_line (abfd, section, syms, pc,
> +				 &filename, &functionname, &line);
> +}

It would be better to check that pc is not less than section vma, then
use "off = pc - vma" for size check and bfd_find_nearest_line.  That way
--section lets you specify a section relative offset in an executable.

> @@ -276,12 +314,12 @@ process_file (const char *file_name, con
>    bfd_close (abfd);
>  }
>  \f
> -int main (int, char **);

Don't delete this.  I believe it is there to quash warnings generated by
some versions of gcc.  OK with these changes.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-05 15:34                 ` Alan Modra
@ 2006-04-05 15:52                   ` Eric Botcazou
  2006-04-05 15:55                     ` Alan Modra
  0 siblings, 1 reply; 14+ messages in thread
From: Eric Botcazou @ 2006-04-05 15:52 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils, Arnaud Charlet

> > +  if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
> > +    return;
> > +
> > +  size = bfd_get_section_size (section);
> > +  if (pc >= size)
> > +    return;
> > +
> > +  found = bfd_find_nearest_line (abfd, section, syms, pc,
> > +				 &filename, &functionname, &line);
> > +}
>
> It would be better to check that pc is not less than section vma, then
> use "off = pc - vma" for size check and bfd_find_nearest_line.  That way
> --section lets you specify a section relative offset in an executable.

That's precisely what find_address_in_section does, isn't it?  Here 'pc' is 
already an offset.

> > @@ -276,12 +314,12 @@ process_file (const char *file_name, con
> >    bfd_close (abfd);
> >  }
> >  \f
> > -int main (int, char **);
>
> Don't delete this.  I believe it is there to quash warnings generated by
> some versions of gcc.

Note that objdump.c doesn't have it.

-- 
Eric Botcazou

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-05 15:52                   ` Eric Botcazou
@ 2006-04-05 15:55                     ` Alan Modra
  2006-04-05 16:05                       ` Eric Botcazou
  0 siblings, 1 reply; 14+ messages in thread
From: Alan Modra @ 2006-04-05 15:55 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: binutils, Arnaud Charlet

On Wed, Apr 05, 2006 at 05:38:16PM +0200, Eric Botcazou wrote:
> > > +  if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
> > > +    return;
> > > +
> > > +  size = bfd_get_section_size (section);
> > > +  if (pc >= size)
> > > +    return;
> > > +
> > > +  found = bfd_find_nearest_line (abfd, section, syms, pc,
> > > +				 &filename, &functionname, &line);
> > > +}
> >
> > It would be better to check that pc is not less than section vma, then
> > use "off = pc - vma" for size check and bfd_find_nearest_line.  That way
> > --section lets you specify a section relative offset in an executable.
> 
> That's precisely what find_address_in_section does, isn't it?  Here 'pc' is 
> already an offset.

I'm thinking of "addr2line -e some_exe -j .data 0 4 8 12 16".
.data might start at 0x123456789abcdef0 and the user wants to minimize
typing.  ie. make --section work on both executables and relocatable
object files.

> > > @@ -276,12 +314,12 @@ process_file (const char *file_name, con
> > >    bfd_close (abfd);
> > >  }
> > >  \f
> > > -int main (int, char **);
> >
> > Don't delete this.  I believe it is there to quash warnings generated by
> > some versions of gcc.
> 
> Note that objdump.c doesn't have it.

Hmm, well then deleting the prototype is probably OK.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-05 15:55                     ` Alan Modra
@ 2006-04-05 16:05                       ` Eric Botcazou
  2006-04-05 17:44                         ` Alan Modra
  0 siblings, 1 reply; 14+ messages in thread
From: Eric Botcazou @ 2006-04-05 16:05 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils, Arnaud Charlet

> > That's precisely what find_address_in_section does, isn't it?  Here 'pc'
> > is already an offset.
>
> I'm thinking of "addr2line -e some_exe -j .data 0 4 8 12 16".
> .data might start at 0x123456789abcdef0 and the user wants to minimize
> typing.  ie. make --section work on both executables and relocatable
> object files.

But that will work out of box, won't it?

-- 
Eric Botcazou

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] New option --print-gc-sections
  2006-04-05 16:05                       ` Eric Botcazou
@ 2006-04-05 17:44                         ` Alan Modra
  0 siblings, 0 replies; 14+ messages in thread
From: Alan Modra @ 2006-04-05 17:44 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: binutils, Arnaud Charlet

On Wed, Apr 05, 2006 at 05:59:34PM +0200, Eric Botcazou wrote:
> > > That's precisely what find_address_in_section does, isn't it?  Here 'pc'
> > > is already an offset.
> >
> > I'm thinking of "addr2line -e some_exe -j .data 0 4 8 12 16".
> > .data might start at 0x123456789abcdef0 and the user wants to minimize
> > typing.  ie. make --section work on both executables and relocatable
> > object files.
> 
> But that will work out of box, won't it?

Yes, sorry.  Brain already asleep.  Patch OK.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2006-04-05 16:05 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-28 12:04 [PATCH] New option --print-gc-sections Eric Botcazou
2006-03-29 14:31 ` Alan Modra
2006-04-03  6:36   ` Eric Botcazou
2006-04-03  7:50     ` Alan Modra
2006-04-04  7:57       ` Eric Botcazou
2006-04-04  8:30         ` Alan Modra
2006-04-04  9:08           ` Eric Botcazou
2006-04-05  3:34             ` Alan Modra
2006-04-05  7:48               ` Eric Botcazou
2006-04-05 15:34                 ` Alan Modra
2006-04-05 15:52                   ` Eric Botcazou
2006-04-05 15:55                     ` Alan Modra
2006-04-05 16:05                       ` Eric Botcazou
2006-04-05 17:44                         ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).