commit 5fa52cf167cadda288c71b4f083da38481698159
parent bea50a9fb4635fd90859754cd31e8756c21479b0
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sun, 19 Jan 2025 10:32:26 +0100
objdump: Add support for -j option
The -j allows to select a set of sections to filter the output of
objdump in commands like for example -h.
Diffstat:
3 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/src/cmd/scc-objdump/coff32.c b/src/cmd/scc-objdump/coff32.c
@@ -74,12 +74,16 @@ void
coff32scns(Obj *obj)
{
int i;
+ char *name;
SCNHDR *scn;
struct coff32 *coff = obj->data;
FILHDR *hdr = &coff->hdr;
for (i = 0; i < hdr->f_nscns; i++) {
scn = &coff->scns[i];
+ name = coff32str(coff, scn);
+ if (!selected(name))
+ continue;
printf("\nscnhdr: %d\n"
"\tname: %s\n"
"\ts_paddr: 0x%04lx\n"
@@ -92,7 +96,7 @@ coff32scns(Obj *obj)
"\ts_nlnno: %u\n"
"\ts_flags: %#lx\n",
i,
- coff32str(coff, scn),
+ name,
scn->s_paddr,
scn->s_vaddr,
scn->s_size,
diff --git a/src/cmd/scc-objdump/main.c b/src/cmd/scc-objdump/main.c
@@ -15,8 +15,8 @@
int tflag, fflag, hflag, pflag, aflag, rflag;
char *argv0;
-static int status;
-static char *filename, *membname;
+static int status, nsecs;
+static char *filename, *membname, **secs;
void
error(char *fmt, ...)
@@ -132,6 +132,22 @@ logb2(unsigned val)
return n;
}
+int
+selected(char *secname)
+{
+ int i;
+
+ if (nsecs == 0)
+ return 1;
+
+ for (i = 0; i < nsecs; i++) {
+ if (strcmp(secname, secs[i]) == 0)
+ return 1;
+ }
+
+ return 0;
+}
+
static void
dumpscns(Obj *obj)
{
@@ -142,6 +158,9 @@ dumpscns(Obj *obj)
puts("Sections:");
puts("Idx Name Size VMA LMA File off Algn");
for (i = 0; getsec(obj, &i, &sec); i++) {
+ if (!selected(sec.name))
+ continue;
+
printf("%3d %-13s %08llx %08llx %08llx %08llx 2**%d\n",
sec.index,
sec.name,
@@ -317,13 +336,15 @@ objdump(char *fname)
static void
usage(void)
{
- fputs("usage: objdump [-afhpt] file...\n", stderr);
+ fputs("usage: objdump [-afhpt][-j section] file...\n", stderr);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
+ char *s;
+
ARGBEGIN {
case 'a':
aflag = 1;
@@ -340,6 +361,11 @@ main(int argc, char *argv[])
case 't':
tflag = 1;
break;
+ case 'j':
+ s = EARGF(usage());
+ secs = xrealloc(secs, (nsecs + 1) * sizeof(char *));
+ secs[nsecs++] = s;
+ break;
default:
usage();
} ARGEND
diff --git a/src/cmd/scc-objdump/objdump.h b/src/cmd/scc-objdump/objdump.h
@@ -29,6 +29,7 @@ extern unsigned coff32fhdr(Obj *, unsigned long long *);
/* main.c */
extern void error(char *, ...);
extern void setflag(unsigned *, int, int);
+extern int selected(char *);
/* globals */
extern int pflag;