This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: [PATCH] nm.c code rearrangement
- From: Elias Athanasopoulos <eathan at otenet dot gr>
- To: binutils at sources dot redhat dot com
- Date: Sat, 28 Sep 2002 14:36:34 +0300
- Subject: Re: [PATCH] nm.c code rearrangement
- References: <20020928143111.F1156@neutrino.particles.org>
On Sat, Sep 28, 2002 at 02:31:12PM +0300, Elias Athanasopoulos wrote:
> This patch rearranges a little the code of nm.c, in order nm to be
> aesthitic correct with the -fsysv output:
I resend the patch, because the first one had a bad diff format. Sorry
for the confusion.
Elias
ChangeLog
2002-09-28 Elias Athanasopoulos <eathan@otenet.gr>
* nm.c (extended_symbol_info): Add filename, lineno for line
numbers information.
(get_line_info): New function. Place the line numbers counting
code here.
(print_symbol): Remove line numbers counting code and call
get_line_info.
(print_symbol_info_bsd): Print line number information.
(print_symbol_info_sysv): Likewise.
(print_symbol_info_posix): Likewise.
--- nm.c.orig Sat Sep 28 12:43:03 2002
+++ nm.c Sat Sep 28 14:15:42 2002
@@ -57,7 +57,8 @@
symbol_info *sinfo;
bfd_vma ssize;
elf_symbol_type *elfinfo;
- /* FIXME: We should add more fields for Type, Line, Section. */
+ const char *filename;
+ unsigned int lineno;
};
#define SYM_NAME(sym) (sym->sinfo->name)
#define SYM_VALUE(sym) (sym->sinfo->value)
@@ -67,7 +68,9 @@
#define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other)
#define SYM_SIZE(sym) \
(sym->elfinfo ? sym->elfinfo->internal_elf_sym.st_size: sym->ssize)
-
+#define SYM_FILENAME(sym) (sym->filename)
+#define SYM_LINENO(sym) (sym->lineno)
+
static void usage PARAMS ((FILE *, int));
static void set_print_radix PARAMS ((char *));
static void set_output_format PARAMS ((char *));
@@ -109,7 +112,7 @@
static void print_symbol_info_posix PARAMS ((struct extended_symbol_info *, bfd *));
static void get_relocs PARAMS ((bfd *, asection *, PTR));
static const char * get_symbol_type PARAMS ((unsigned int));
-
+static void get_line_info PARAMS ((bfd *, asymbol *, const char **, unsigned int *));
/* Support for different output formats. */
struct output_fns
{
@@ -1189,7 +1192,7 @@
{
symbol_info syminfo;
struct extended_symbol_info info;
-
+
bfd_get_symbol_info (abfd, sym, &syminfo);
info.sinfo = &syminfo;
info.ssize = ssize;
@@ -1197,122 +1200,130 @@
info.elfinfo = (elf_symbol_type *) sym;
else
info.elfinfo = NULL;
+ if (line_numbers) {
+ info.filename = NULL;
+ info.lineno = 0;
+ get_line_info (abfd, sym, &(info.filename), &(info.lineno));
+ }
(*format->print_symbol_info) (&info, abfd);
}
+
+ putchar ('\n');
+}
- if (line_numbers)
+static void
+get_line_info (abfd, sym, filename, lineno)
+ bfd *abfd;
+ asymbol *sym;
+ const char **filename;
+ unsigned int *lineno;
+{
+ static asymbol **syms;
+ static long symcount;
+ const char *functionname;
+
+ /* We need to get the canonical symbols in order to call
+ bfd_find_nearest_line. This is inefficient, but, then, you
+ don't have to use --line-numbers. */
+ if (abfd != lineno_cache_bfd && syms != NULL)
+ {
+ free (syms);
+ syms = NULL;
+ }
+ if (syms == NULL)
{
- static asymbol **syms;
- static long symcount;
- const char *filename, *functionname;
- unsigned int lineno;
-
- /* We need to get the canonical symbols in order to call
- bfd_find_nearest_line. This is inefficient, but, then, you
- don't have to use --line-numbers. */
- if (abfd != lineno_cache_bfd && syms != NULL)
+ long symsize;
+
+ symsize = bfd_get_symtab_upper_bound (abfd);
+ if (symsize < 0)
+ bfd_fatal (bfd_get_filename (abfd));
+ syms = (asymbol **) xmalloc (symsize);
+ symcount = bfd_canonicalize_symtab (abfd, syms);
+ if (symcount < 0)
+ bfd_fatal (bfd_get_filename (abfd));
+ lineno_cache_bfd = abfd;
+ }
+
+ if (bfd_is_und_section (bfd_get_section (sym)))
+ {
+ static asection **secs;
+ static arelent ***relocs;
+ static long *relcount;
+ static unsigned int seccount;
+ unsigned int i;
+ const char *symname;
+
+ /* For an undefined symbol, we try to find a reloc for the
+ symbol, and print the line number of the reloc. */
+ if (abfd != lineno_cache_rel_bfd && relocs != NULL)
{
- free (syms);
- syms = NULL;
+ for (i = 0; i < seccount; i++)
+ if (relocs[i] != NULL)
+ free (relocs[i]);
+ free (secs);
+ free (relocs);
+ free (relcount);
+ secs = NULL;
+ relocs = NULL;
+ relcount = NULL;
}
- if (syms == NULL)
+
+ if (relocs == NULL)
{
- long symsize;
-
- symsize = bfd_get_symtab_upper_bound (abfd);
- if (symsize < 0)
- bfd_fatal (bfd_get_filename (abfd));
- syms = (asymbol **) xmalloc (symsize);
- symcount = bfd_canonicalize_symtab (abfd, syms);
- if (symcount < 0)
- bfd_fatal (bfd_get_filename (abfd));
- lineno_cache_bfd = abfd;
+ struct get_relocs_info info;
+
+ seccount = bfd_count_sections (abfd);
+
+ secs = (asection **) xmalloc (seccount * sizeof *secs);
+ relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
+ relcount = (long *) xmalloc (seccount * sizeof *relcount);
+
+ info.secs = secs;
+ info.relocs = relocs;
+ info.relcount = relcount;
+ info.syms = syms;
+ bfd_map_over_sections (abfd, get_relocs, (PTR) &info);
+ lineno_cache_rel_bfd = abfd;
}
-
- if (bfd_is_und_section (bfd_get_section (sym)))
+
+ symname = bfd_asymbol_name (sym);
+ for (i = 0; i < seccount; i++)
{
- static asection **secs;
- static arelent ***relocs;
- static long *relcount;
- static unsigned int seccount;
- unsigned int i;
- const char *symname;
-
- /* For an undefined symbol, we try to find a reloc for the
- symbol, and print the line number of the reloc. */
- if (abfd != lineno_cache_rel_bfd && relocs != NULL)
+ long j;
+
+ for (j = 0; j < relcount[i]; j++)
{
- for (i = 0; i < seccount; i++)
- if (relocs[i] != NULL)
- free (relocs[i]);
- free (secs);
- free (relocs);
- free (relcount);
- secs = NULL;
- relocs = NULL;
- relcount = NULL;
- }
-
- if (relocs == NULL)
- {
- struct get_relocs_info info;
-
- seccount = bfd_count_sections (abfd);
-
- secs = (asection **) xmalloc (seccount * sizeof *secs);
- relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
- relcount = (long *) xmalloc (seccount * sizeof *relcount);
-
- info.secs = secs;
- info.relocs = relocs;
- info.relcount = relcount;
- info.syms = syms;
- bfd_map_over_sections (abfd, get_relocs, (PTR) &info);
- lineno_cache_rel_bfd = abfd;
- }
-
- symname = bfd_asymbol_name (sym);
- for (i = 0; i < seccount; i++)
- {
- long j;
-
- for (j = 0; j < relcount[i]; j++)
+ arelent *r;
+
+ r = relocs[i][j];
+ if (r->sym_ptr_ptr != NULL
+ && (*r->sym_ptr_ptr)->section == sym->section
+ && (*r->sym_ptr_ptr)->value == sym->value
+ && strcmp (symname,
+ bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
+ && bfd_find_nearest_line (abfd, secs[i], syms,
+ r->address, filename,
+ &functionname, lineno)
+ && filename != NULL)
{
- arelent *r;
-
- r = relocs[i][j];
- if (r->sym_ptr_ptr != NULL
- && (*r->sym_ptr_ptr)->section == sym->section
- && (*r->sym_ptr_ptr)->value == sym->value
- && strcmp (symname,
- bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
- && bfd_find_nearest_line (abfd, secs[i], syms,
- r->address, &filename,
- &functionname, &lineno)
- && filename != NULL)
- {
- /* We only print the first one we find. */
- printf ("\t%s:%u", filename, lineno);
- i = seccount;
- break;
- }
+ /* We only get the first one we find. */
+ i = seccount;
+ break;
}
}
}
- else if (bfd_get_section (sym)->owner == abfd)
+ }
+ else if (bfd_get_section (sym)->owner == abfd)
+ {
+ if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
+ sym->value, filename, &functionname,
+ lineno)
+ && filename != NULL
+ && lineno != 0)
{
- if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
- sym->value, &filename, &functionname,
- &lineno)
- && filename != NULL
- && lineno != 0)
- {
- printf ("\t%s:%u", filename, lineno);
- }
+ return;
}
- }
-
- putchar ('\n');
+ }
}
/* The following 3 groups of functions are called unconditionally,
@@ -1523,6 +1534,8 @@
printf (" %5s", SYM_STAB_NAME (info));
}
print_symname (" %s", SYM_NAME (info), abfd);
+ if (SYM_FILENAME (info) && SYM_LINENO (info))
+ printf ("\t%s:%u", SYM_FILENAME (info), SYM_LINENO (info));
}
static void
@@ -1569,11 +1582,12 @@
else
printf (" ");
}
-
- if (info->elfinfo)
- printf("| |%s", info->elfinfo->symbol.section->name);
+ if (SYM_FILENAME (info) && SYM_LINENO (info))
+ printf ("|%s:%u|", SYM_FILENAME (info), SYM_LINENO (info));
else
- printf("| |");
+ printf ("| |");
+ if (info->elfinfo)
+ printf ("%s", info->elfinfo->symbol.section->name);
}
}
@@ -1594,6 +1608,8 @@
if (SYM_SIZE (info))
print_value (abfd, SYM_SIZE (info));
}
+ if (SYM_FILENAME (info) && SYM_LINENO (info))
+ printf ("\t%s:%u", SYM_FILENAME (info), SYM_LINENO (info));
}
static void