Skip to content

Commit cffcafd

Browse files
committed
elfload: Fixup indentation, style and add some comments
1 parent 6914f22 commit cffcafd

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

cpu/common/parse.c

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,10 @@ readfile_elf (char *filename)
498498
struct elf32_hdr elfhdr;
499499
struct elf32_phdr *elf_phdata = NULL;
500500
struct elf32_shdr *elf_spstr, *elf_spnt, *elf_shdata;
501-
struct elf32_sym *sym_tbl = (struct elf32_sym *) 0;
501+
struct elf32_sym *sym_tbl = NULL;
502502
uint32_t syms = 0;
503-
char *str_tbl = (char *) 0;
504-
char *s_str = (char *) 0;
503+
char *str_tbl = NULL;
504+
char *s_str = NULL;
505505
uint32_t inputbuf;
506506
uint32_t padd;
507507
uint32_t insn;
@@ -513,12 +513,14 @@ readfile_elf (char *filename)
513513
exit (1);
514514
}
515515

516+
/* Load elf header */
516517
if (fread (&elfhdr, sizeof (elfhdr), 1, inputfs) != 1)
517518
{
518519
perror ("readfile_elf");
519520
exit (1);
520521
}
521522

523+
/* Read in elf section header */
522524
if ((elf_shdata =
523525
(struct elf32_shdr *) malloc (ELF_SHORT_H (elfhdr.e_shentsize) *
524526
ELF_SHORT_H (elfhdr.e_shnum))) == NULL)
@@ -542,6 +544,7 @@ readfile_elf (char *filename)
542544
exit (1);
543545
}
544546

547+
/* Read in elf program headers if available */
545548
if (ELF_LONG_H (elfhdr.e_phoff))
546549
{
547550
if ((elf_phdata =
@@ -569,18 +572,15 @@ readfile_elf (char *filename)
569572
}
570573
}
571574

575+
/* Look for symbol table section and load string table. Used
576+
for inserting symbol labels. */
572577
for (i = 0, elf_spnt = elf_shdata; i < ELF_SHORT_H (elfhdr.e_shnum);
573578
i++, elf_spnt++)
574579
{
575-
576-
577580
if (ELF_LONG_H (elf_spnt->sh_type) == SHT_SYMTAB)
578581
{
579-
580582
if (NULL != sym_tbl)
581-
{
582-
free (sym_tbl);
583-
}
583+
free (sym_tbl);
584584

585585
if ((sym_tbl =
586586
(struct elf32_sym *) malloc (ELF_LONG_H (elf_spnt->sh_size)))
@@ -608,38 +608,37 @@ readfile_elf (char *filename)
608608
ELF_LONG_H (elf_spnt->sh_size) /
609609
ELF_LONG_H (elf_spnt->sh_entsize);
610610

611-
if (ELF_LONG_H (elf_spnt->sh_link) <= ELF_SHORT_H (elfhdr.e_shnum))
612-
{
613-
if (NULL != str_tbl)
614-
{
615-
free (str_tbl);
616-
}
611+
if (ELF_LONG_H (elf_spnt->sh_link) <= ELF_SHORT_H (elfhdr.e_shnum))
612+
{
613+
if (NULL != str_tbl)
614+
free (str_tbl);
617615

618-
elf_spstr = &elf_shdata[ELF_LONG_H (elf_spnt->sh_link)];
619-
if ((str_tbl =
620-
(char *) malloc (ELF_LONG_H (elf_spstr->sh_size))) == NULL)
621-
{
622-
perror ("readfile_elf");
623-
exit (1);
624-
}
616+
elf_spstr = &elf_shdata[ELF_LONG_H (elf_spnt->sh_link)];
617+
if ((str_tbl =
618+
(char *) malloc (ELF_LONG_H (elf_spstr->sh_size))) == NULL)
619+
{
620+
perror ("readfile_elf");
621+
exit (1);
622+
}
625623

626-
if (fseek (inputfs, ELF_LONG_H (elf_spstr->sh_offset), SEEK_SET) !=
627-
0)
628-
{
629-
perror ("readfile_elf");
630-
exit (1);
631-
}
624+
if (fseek (inputfs, ELF_LONG_H (elf_spstr->sh_offset), SEEK_SET) !=
625+
0)
626+
{
627+
perror ("readfile_elf");
628+
exit (1);
629+
}
632630

633-
if (fread (str_tbl, ELF_LONG_H (elf_spstr->sh_size), 1, inputfs) !=
634-
1)
635-
{
636-
perror ("readfile_elf");
637-
exit (1);
638-
}
631+
if (fread (str_tbl, ELF_LONG_H (elf_spstr->sh_size), 1, inputfs) !=
632+
1)
633+
{
634+
perror ("readfile_elf");
635+
exit (1);
639636
}
637+
}
640638
}
641639
}
642640

641+
/* Load section name string table. Used for printing section names. */
643642
if (ELF_SHORT_H (elfhdr.e_shstrndx) != SHN_UNDEF)
644643
{
645644
elf_spnt = &elf_shdata[ELF_SHORT_H (elfhdr.e_shstrndx)];
@@ -663,6 +662,7 @@ readfile_elf (char *filename)
663662
}
664663
}
665664

665+
/* Iterate over section headers and load program bits. */
666666
for (i = 0, elf_spnt = elf_shdata; i < ELF_SHORT_H (elfhdr.e_shnum);
667667
i++, elf_spnt++)
668668
{
@@ -672,6 +672,9 @@ readfile_elf (char *filename)
672672
{
673673

674674
padd = ELF_LONG_H (elf_spnt->sh_addr);
675+
/* Search if section is within program header segment, if
676+
so adjust the paddr to use the physical address of the
677+
segment. */
675678
for (j = 0; j < ELF_SHORT_H (elfhdr.e_phnum); j++)
676679
{
677680
if (ELF_LONG_H (elf_phdata[j].p_offset) &&
@@ -686,8 +689,6 @@ readfile_elf (char *filename)
686689
ELF_LONG_H (elf_phdata[j].p_offset);
687690
}
688691

689-
690-
691692
if (ELF_LONG_H (elf_spnt->sh_name) && s_str)
692693
{
693694
PRINTFQ ("Section: %s,", &s_str[ELF_LONG_H (elf_spnt->sh_name)]);
@@ -723,6 +724,7 @@ readfile_elf (char *filename)
723724
}
724725
}
725726

727+
/* Load up sym_tbl symbols and names from str_tbl into symbol to label hash. */
726728
if (str_tbl)
727729
{
728730
i = 0;
@@ -739,19 +741,18 @@ readfile_elf (char *filename)
739741
}
740742

741743
if (NULL != str_tbl)
742-
{
743-
free (str_tbl);
744-
}
744+
free (str_tbl);
745745

746746
if (NULL != sym_tbl)
747-
{
748-
free (sym_tbl);
749-
}
747+
free (sym_tbl);
750748

751-
free (s_str);
752-
free (elf_phdata);
753-
free (elf_shdata);
749+
if (NULL != s_str)
750+
free (s_str);
751+
752+
if (NULL != elf_phdata)
753+
free (elf_phdata);
754754

755+
free (elf_shdata);
755756
}
756757

757758
/* Identify file type and call appropriate readfile_X routine. It only

0 commit comments

Comments
 (0)