From fdbba42a549386c2c4172c8c25ad45810b7a158b Mon Sep 17 00:00:00 2001 From: Greg Ungerer Date: Wed, 1 Aug 2007 03:40:12 +0000 Subject: [PATCH] lf2flt crashes on Linux/amd64: (gdb) run -a -o links -p links.gdb links.gdb Starting program: /home/stsp/dslinux/toolchain/prefix/bin/arm-linux-elf-elf2flt -a -o links -p links.gdb links.gdb Program received signal SIGSEGV, Segmentation fault. _bfd_elf_canonicalize_reloc (abfd=, section=0x5f6900, relptr=0xffffffffa6360010, symbols=) at /home/stsp/dslinux/toolchain/src/binutils-2.17/bfd/elf.c:6367 6367 *relptr++ = tblptr++; (gdb) bt #0 _bfd_elf_canonicalize_reloc (abfd=, section=0x5f6900, relptr=0xffffffffa6360010, symbols=) at /home/stsp/dslinux/toolchain/src/binutils-2.17/bfd/elf.c:6367 #1 0x00000000004006dd in output_relocs (abs_bfd=0x5f5570, symbols=0x2b30a5e99010, number_of_symbols=16585, n_relocs=0x7fff04c0fe58, text=0x2b30a6102010 "", text_len=, text_vma=0, data=0x2b30a627b010 "", data_len=934480, data_vma=1541824, rel_bfd=0x5f4400) at /home/stsp/dslinux/toolchain/src/elf2flt-20051225/elf2flt.c:587 #2 0x0000000000401180 in main (argc=, argv=) at /home/stsp/dslinux/toolchain/src/elf2flt-20051225/elf2flt.c:1949 The problem seems to be that the one and only call to xmalloc() in elf2flt.c does not return a valid pointer for some reason. I'm wondering why xmalloc() is used exactly once in elf2flt.c. All other heap allocations in elf2flt are done with plain malloc(). The attached patch fixes the segfault by replacing the call to xmalloc() with a call to malloc(). It also makes elf2flt check for return values of malloc() calls, providing the equivalent behaviour of using xmalloc(). Patch submitted by Stefan Sperling --- elf2flt.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/elf2flt.c b/elf2flt.c index 17a8b69..82015fc 100644 --- a/elf2flt.c +++ b/elf2flt.c @@ -236,6 +236,10 @@ get_symbols (bfd *abfd, long *num) return NULL; symbol_table = (asymbol **) malloc (storage_needed); + if (symbol_table == NULL) { + perror("malloc"); + exit(1); + } number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); @@ -492,7 +496,12 @@ dump_symbols(symbols, number_of_symbols); } symb = get_symbols(rel_bfd, &nsymb); - relpp = (arelent **) xmalloc(relsize); + relpp = (arelent **) malloc(relsize); + if (relpp == NULL) { + perror("malloc"); + exit(1); + } + relcount = bfd_canonicalize_reloc(rel_bfd, r, relpp, symb); if (relcount <= 0) { if (verbose) @@ -1975,6 +1984,10 @@ int main(int argc, char *argv[]) } text = malloc(text_len); + if (text == NULL) { + perror("malloc"); + exit(1); + } if (verbose) printf("TEXT -> vma=0x%x len=0x%x\n", text_vma, text_len); @@ -1995,6 +2008,10 @@ int main(int argc, char *argv[]) exit (2); } data = malloc(data_len); + if (data == NULL) { + perror("malloc"); + exit(1); + } if (verbose) printf("DATA -> vma=0x%x len=0x%x\n", data_vma, data_len); @@ -2079,6 +2096,10 @@ int main(int argc, char *argv[]) if (!ofile) { ofile = malloc(strlen(fname) + 5 + 1); /* 5 to add suffix */ + if (ofile == NULL) { + perror("malloc"); + exit(1); + } strcpy(ofile, fname); strcat(ofile, ".bflt"); } -- 2.39.5