ref: ed326ceb48992680934f1c82c387893b0ccf1af0
parent: 25e676d87fef3efa5cd4101ce7f955f477564cfc
author: Roberto E. Vargas Caballero <[email protected]>
date: Sat Feb 3 05:53:07 EST 2018
[nm] Fix some small bugs
--- a/nm/main.c
+++ b/nm/main.c
@@ -30,12 +30,10 @@
fgetpos(fp, &pos);
fread(magic, sizeof(magic), 1, fp);
- if (!ferror(fp)) {
- if (!strncmp(magic, MYROMAGIC, MYROMAGIC_SIZ))
- return 1;
- }
-
fsetpos(fp, &pos);
+
+ if (!ferror(fp) && !strncmp(magic, MYROMAGIC, MYROMAGIC_SIZ))
+ return 1;
return 0;
}
@@ -47,13 +45,10 @@
fgetpos(fp, &pos);
fread(magic, sizeof(magic), 1, fp);
-
- if (!ferror(fp)) {
- if (!strncmp(magic, ARMAGIC, ARMAGIC_SIZ))
- return 1;
- }
-
fsetpos(fp, &pos);
+
+ if (!ferror(fp) && !strncmp(magic, ARMAGIC, ARMAGIC_SIZ))
+ return 1;
return 0;
}
@@ -75,7 +70,7 @@
}
static void
-print(char *file, char *member, struct myrosym *sym, FILE *fp)
+print(char *file, char *member, struct myrosym *sym)
{
char *fmt, *name = strings + sym->name;
int type = typeof(sym);
@@ -86,9 +81,9 @@
return;
if (Aflag)
- fprintf(fp, (archflag) ? "%s[%s]: " : "%s: ", file, member);
+ printf((archflag) ? "%s[%s]: " : "%s: ", file, member);
if (Pflag) {
- fprintf(fp, "%s %c", name, type);
+ printf("%s %c", name, type);
if (type != 'U') {
if (radix == 8)
fmt = "%llo %llo";
@@ -96,7 +91,7 @@
fmt = "%llu %llu";
else
fmt = "%llx %llx";
- fprintf(fp, fmt, sym->offset, sym->len);
+ printf(fmt, sym->offset, sym->len);
}
} else {
if (type == 'U')
@@ -107,10 +102,10 @@
fmt = "%016.16lld";
else
fmt = "%016.16llx";
- fprintf(fp, fmt, sym->offset);
- fprintf(fp, " %c %s", type, name);
+ printf(fmt, sym->offset);
+ printf(" %c %s", type, name);
}
- putc('\n', fp);
+ putchar('\n');
}
static void
@@ -154,13 +149,13 @@
for (i = 0; i < n; ++i) {
if (rdmyrosym(fp, &syms[i]) < 0)
- goto free_arrays;
+ goto symbol_error;
if (syms[i].name >= hdr.strsize)
goto offset_overflow;
}
qsort(syms, n, sizeof(*syms), cmp);
for (i = 0; i < n; ++i)
- print(fname, member, &syms[i], fp);
+ print(fname, member, &syms[i]);
free_arrays:
@@ -168,6 +163,10 @@
free(strings);
return;
+symbol_error:
+ fprintf(stderr, "nm: %s: error reading symbols\n", fname);
+ goto free_arrays;
+
offset_overflow:
fprintf(stderr, "nm: %s: overflow in headers of archive\n",
fname);
@@ -273,8 +272,8 @@
if (argc == 0) {
doit("a.out");
} else {
- while (argc-- > 0)
- doit(*++argv);
+ for ( ; *argv; ++argv)
+ doit(*argv);
}
return 0;
--- a/objdump/main.c
+++ b/objdump/main.c
@@ -222,7 +222,7 @@
for (off = 0; ; off += 32) {
printf(" %016llX:", off);
for (i = 0; i < 2; i++) {
- for (j = 0; j < 16; j++) {
+ for (j = 0; j < 8; j++) {
if ((c = getc(obj->fp)) == EOF)
goto exit_loop;
printf(" %02X", c);