Skip to content

Commit 4051540

Browse files
committed
add: [srxfixup] report errno on file open/read error
1 parent dccb61d commit 4051540

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

tools/srxfixup/src/elflib.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
#include "srxfixup_internal.h"
12+
#include <errno.h>
1213
#include <stdint.h>
1314
#include <stdio.h>
1415
#include <stdlib.h>
@@ -37,14 +38,14 @@ elf_file *read_elf(const char *filename)
3738
fp = fopen(filename, "rbe");
3839
if ( !fp )
3940
{
40-
fprintf(stderr, "`%s' can't open\n", filename);
41+
fprintf(stderr, "\"%s\" can't open (errno=%d)\n", filename, errno);
4142
return 0;
4243
}
4344
elf = (elf_file *)calloc(1, sizeof(elf_file));
4445
elf->ehp = (Elf32_Ehdr *)malloc(sizeof(Elf32_Ehdr));
4546
if ( fread(elf->ehp, sizeof(Elf32_Ehdr), 1, fp) != 1 )
4647
{
47-
fprintf(stderr, "%s: Could not read ELF header\n", filename);
48+
fprintf(stderr, "%s: Could not read ELF header (errno=%d)\n", filename, errno);
4849
exit(1);
4950
}
5051
swapmemory(elf->ehp, "ccccccccccccccccsslllllssssss", 1);
@@ -104,7 +105,7 @@ elf_file *read_elf(const char *filename)
104105
{
105106
if ( fread(&elf->php[i_1], sizeof(Elf32_Phdr), 1, fp) != 1 )
106107
{
107-
fprintf(stderr, "%s: Could not read ELF program header\n", filename);
108+
fprintf(stderr, "%s: Could not read ELF program header (errno=%d)\n", filename, errno);
108109
exit(1);
109110
}
110111
swapmemory(&elf->php[i_1], "llllllll", 1);
@@ -127,7 +128,7 @@ elf_file *read_elf(const char *filename)
127128
elf->scp[i_2] = (elf_section *)calloc(1, sizeof(elf_section));
128129
if ( fread(elf->scp[i_2], sizeof(Elf32_Shdr), 1, fp) != 1 )
129130
{
130-
fprintf(stderr, "%s: Could not read ELF section header\n", filename);
131+
fprintf(stderr, "%s: Could not read ELF section header (errno=%d)\n", filename, errno);
131132
exit(1);
132133
}
133134
swapmemory(elf->scp[i_2], "llllllllll", 1);
@@ -318,7 +319,7 @@ elf_file *read_elf(const char *filename)
318319
elf->scp[i_4]->data = (uint8_t *)malloc(size);
319320
if ( fread(elf->scp[i_4]->data, size, 1, fp) != 1 )
320321
{
321-
fprintf(stderr, "%s: Could not read ELF section contents\n", filename);
322+
fprintf(stderr, "%s: Could not read ELF section contents, (errno=%d)\n", filename, errno);
322323
exit(1);
323324
}
324325
swapmemory(elf->scp[i_4]->data, "l", size >> 2);
@@ -334,7 +335,7 @@ elf_file *read_elf(const char *filename)
334335
elf->scp[i_4]->data = (uint8_t *)malloc(size);
335336
if ( fread(elf->scp[i_4]->data, size, 1, fp) != 1 )
336337
{
337-
fprintf(stderr, "%s: Could not read ELF section contents\n", filename);
338+
fprintf(stderr, "%s: Could not read ELF section contents (errno=%d)\n", filename, errno);
338339
exit(1);
339340
}
340341
swapmemory(elf->scp[i_4]->data, "lllllls", 1);
@@ -343,7 +344,7 @@ elf_file *read_elf(const char *filename)
343344
elf->scp[i_4]->data = (uint8_t *)malloc(size);
344345
if ( fread(elf->scp[i_4]->data, size, 1, fp) != 1 )
345346
{
346-
fprintf(stderr, "%s: Could not read ELF section contents\n", filename);
347+
fprintf(stderr, "%s: Could not read ELF section contents (errno=%d)\n", filename, errno);
347348
exit(1);
348349
}
349350
swapmemory(elf->scp[i_4]->data, "lllllllllls", 1);
@@ -352,7 +353,7 @@ elf_file *read_elf(const char *filename)
352353
elf->scp[i_4]->data = (uint8_t *)malloc(size);
353354
if ( fread(elf->scp[i_4]->data, size, 1, fp) != 1 )
354355
{
355-
fprintf(stderr, "%s: Could not read ELF section contents\n", filename);
356+
fprintf(stderr, "%s: Could not read ELF section contents (errno=%d)\n", filename, errno);
356357
exit(1);
357358
}
358359
break;
@@ -389,7 +390,7 @@ static void read_symtab(elf_file *elf, int sctindex, FILE *fp)
389390
result[i] = (elf_syment *)calloc(1, sizeof(elf_syment));
390391
if ( fread(result[i], sp_x->shr.sh_entsize, 1, fp) != 1 )
391392
{
392-
fprintf(stderr, "elflib: Could not read ELF symbol table\n");
393+
fprintf(stderr, "elflib: Could not read ELF symbol table (errno=%d)\n", errno);
393394
exit(1);
394395
}
395396
swapmemory(result[i], "lllccs", 1);
@@ -428,7 +429,7 @@ static void read_rel(elf_file *elf, int sctindex, FILE *fp)
428429
{
429430
if ( fread(&result[i], sp_x->shr.sh_entsize, 1, fp) != 1 )
430431
{
431-
fprintf(stderr, "elflib: Could not read ELF relocations\n");
432+
fprintf(stderr, "elflib: Could not read ELF relocations (errno=%d)\n", errno);
432433
exit(1);
433434
}
434435
swapmemory(&result[i], "ll", 1);
@@ -449,7 +450,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
449450
sycb = (elf_mips_symbolic_data *)malloc(sizeof(elf_mips_symbolic_data));
450451
if ( fread(sycb, sizeof(hdrr), 1, fp) != 1 )
451452
{
452-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
453+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
453454
exit(1);
454455
}
455456
swapmemory(sycb, "sslllllllllllllllllllllll", 1);
@@ -462,7 +463,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
462463
fseek(fp, sycb->head.cbLineOffset, SEEK_SET);
463464
if ( fread(sycb->cbLine_Ptr, size_1, 1, fp) != 1 )
464465
{
465-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
466+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
466467
exit(1);
467468
}
468469
}
@@ -475,7 +476,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
475476
fseek(fp, sycb->head.cbDnOffset, SEEK_SET);
476477
if ( fread(sycb->cbDn_Ptr, size_2, 1, fp) != 1 )
477478
{
478-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
479+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
479480
exit(1);
480481
}
481482
swapmemory(sycb->cbDn_Ptr, "ll", sycb->head.idnMax);
@@ -489,7 +490,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
489490
fseek(fp, sycb->head.cbPdOffset, SEEK_SET);
490491
if ( fread(sycb->cbPd_Ptr, size_3, 1, fp) != 1 )
491492
{
492-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
493+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
493494
exit(1);
494495
}
495496
swapmemory(sycb->cbPd_Ptr, "lllllllllsslll", sycb->head.ipdMax);
@@ -503,7 +504,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
503504
fseek(fp, sycb->head.cbSymOffset, SEEK_SET);
504505
if ( fread(sycb->cbSym_Ptr, size_4, 1, fp) != 1 )
505506
{
506-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
507+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
507508
exit(1);
508509
}
509510
swapmemory(sycb->cbSym_Ptr, "lll", sycb->head.isymMax);
@@ -517,7 +518,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
517518
fseek(fp, sycb->head.cbOptOffset, SEEK_SET);
518519
if ( fread(sycb->cbOpt_Ptr, size_5, 1, fp) != 1 )
519520
{
520-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
521+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
521522
exit(1);
522523
}
523524
swapmemory(sycb->cbOpt_Ptr, "lll", sycb->head.ioptMax);
@@ -531,7 +532,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
531532
fseek(fp, sycb->head.cbAuxOffset, SEEK_SET);
532533
if ( fread(sycb->cbAux_Ptr, size_6, 1, fp) != 1 )
533534
{
534-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
535+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
535536
exit(1);
536537
}
537538
swapmemory(sycb->cbAux_Ptr, "l", sycb->head.iauxMax);
@@ -545,7 +546,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
545546
fseek(fp, sycb->head.cbSsOffset, SEEK_SET);
546547
if ( fread(sycb->cbSs_Ptr, size_7, 1, fp) != 1 )
547548
{
548-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
549+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
549550
exit(1);
550551
}
551552
}
@@ -558,7 +559,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
558559
fseek(fp, sycb->head.cbSsExtOffset, SEEK_SET);
559560
if ( fread(sycb->cbSsExt_Ptr, size_8, 1, fp) != 1 )
560561
{
561-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
562+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
562563
exit(1);
563564
}
564565
}
@@ -571,7 +572,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
571572
fseek(fp, sycb->head.cbFdOffset, SEEK_SET);
572573
if ( fread(sycb->cbFd_Ptr, size_9, 1, fp) != 1 )
573574
{
574-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
575+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
575576
exit(1);
576577
}
577578
swapmemory(sycb->cbFd_Ptr, "llllllllllsslllllll", sycb->head.ifdMax);
@@ -585,7 +586,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
585586
fseek(fp, sycb->head.cbRfdOffset, SEEK_SET);
586587
if ( fread(sycb->cbRfd_Ptr, size_A, 1, fp) != 1 )
587588
{
588-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
589+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
589590
exit(1);
590591
}
591592
swapmemory(sycb->cbRfd_Ptr, "l", sycb->head.crfd);
@@ -599,7 +600,7 @@ static elf_mips_symbolic_data *read_mips_symbolic(FILE *fp)
599600
fseek(fp, sycb->head.cbExtOffset, SEEK_SET);
600601
if ( fread(sycb->cbExt_Ptr, size_B, 1, fp) != 1 )
601602
{
602-
fprintf(stderr, "elflib: Could not read ELF debug info contents\n");
603+
fprintf(stderr, "elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
603604
exit(1);
604605
}
605606
swapmemory(sycb->cbExt_Ptr, "sslll", sycb->head.iextMax);
@@ -628,7 +629,7 @@ int write_elf(elf_file *elf, const char *filename)
628629
fp = fopen(filename, "wbe");
629630
if ( !fp )
630631
{
631-
perror(filename);
632+
fprintf(stderr, "\"%s\" can't open (errno=%d)\n", filename, errno);
632633
return 1;
633634
}
634635
renumber_symtab(elf);

tools/srxfixup/src/readconf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "srxfixup_internal.h"
1212
#include <ctype.h>
13+
#include <errno.h>
1314
#include <stdio.h>
1415
#include <stdlib.h>
1516
#include <string.h>
@@ -95,7 +96,7 @@ Srx_gen_table *read_conf(const char *indata, const char *infile, int dumpopt)
9596
fp = fopen(infile, "re");
9697
if ( !fp )
9798
{
98-
fprintf(stderr, "\"%s\" can't open\n", infile);
99+
fprintf(stderr, "\"%s\" can't open (errno=%d)\n", infile, errno);
99100
return 0;
100101
}
101102
fseek(fp, 0, SEEK_END);

0 commit comments

Comments
 (0)