Shrink get_extracopy() by moving portions into separate functions.

Signed-off-by: Steve Fox <[EMAIL PROTECTED]>
---

This probably requires the 'add/fix comments' patch sent earlier today.

diff --git a/elflink.c b/elflink.c
index 3ae28cc..bfc69bf 100644
--- a/elflink.c
+++ b/elflink.c
@@ -249,65 +249,48 @@ static void check_bss(unsigned long *sta
        }
 }
 
-/* 
- * Subtle:  Since libhugetlbfs depends on glibc, we allow it
- * it to be loaded before us.  As part of its init functions, it
- * initializes stdin, stdout, and stderr in the bss.  We need to
- * include these initialized variables in our copy.
- */
-
-static void get_extracopy(struct seg_info *seg, void *p, 
-                                                void **extra_start, void 
**extra_end)
-{
-       Elf_Dyn *dyntab;        /* dynamic segment table */
-       Elf_Phdr *phdr;         /* program header table */
-       Elf_Sym *symtab = NULL; /* dynamic symbol table */
-       Elf_Sym *sym;           /* a symbol */
-       char *strtab = NULL;    /* string table for dynamic symbols */
-       int i, found_sym = 0;
-       int numsyms;            /* number of symbols in dynamic symbol table */
-       void *start, *end, *start_orig, *end_orig;
-       void *sym_start, *sym_end;
-
-       end_orig = seg->vaddr + seg->memsz;
-       start_orig = seg->vaddr + seg->filesz;
-       if (seg->filesz == seg->memsz)
-               goto bail;
-       if (!minimal_copy)
-               goto bail;
+/* Find the .dynamic program header */   
+static int find_dynamic(Elf_Dyn **dyntab) {
+       Elf_Phdr *phdr; /* program header table */
+       int i = 1;
 
-       /* Find dynamic section */
-       i = 1;
        phdr = (Elf_Phdr *)((char *)ehdr + ehdr->e_phoff);
        while ((phdr[i].p_type != PT_DYNAMIC) && (i < ehdr->e_phnum)) {
                ++i;
        }
        if (phdr[i].p_type == PT_DYNAMIC) {
-               dyntab = (Elf_Dyn *)phdr[i].p_vaddr;
+               *dyntab = (Elf_Dyn *)phdr[i].p_vaddr;
+               return 0;
        } else {
                DEBUG("No dynamic segment found\n");
-               goto bail;
+               return -1;
        }
+}
 
-       /* Find symbol and string tables */
-       i = 1;
+/* Find the dynamic string and symbol tables */
+static int find_tables(Elf_Dyn *dyntab, Elf_Sym **symtab, char **strtab) {
+       int i = 1;
        while ((dyntab[i].d_tag != DT_NULL)) {
                if (dyntab[i].d_tag == DT_SYMTAB)
-                       symtab = (Elf_Sym *)dyntab[i].d_un.d_ptr;
+                       *symtab = (Elf_Sym *)dyntab[i].d_un.d_ptr;
                else if (dyntab[i].d_tag == DT_STRTAB)
-                       strtab = (char *)dyntab[i].d_un.d_ptr;
+                       *strtab = (char *)dyntab[i].d_un.d_ptr;
                i++;
        }
                        
-       if (!symtab) {
+       if (!*symtab) {
                DEBUG("No symbol table found\n");
-               goto bail;
+               return -1;
        }
-       if (!strtab) {
+       if (!*strtab) {
                DEBUG("No string table found\n");
-               goto bail;
+               return -1;
        }
+       return 0;
+}
 
+/* Find the number of symbol table entries */
+static int find_numsyms(Elf_Sym *symtab, char *strtab) {
        /*
         * WARNING - The symbol table size calculation does not follow the ELF
         *           standard, but rather exploits an assumption we enforce in
@@ -317,9 +300,49 @@ static void get_extracopy(struct seg_inf
         */
        if ((void *)strtab <= (void *)symtab) {
                DEBUG("Could not calculate dynamic symbol table size\n");
-               goto bail;
+               return -1;
        }
-       numsyms = ((void *)strtab - (void *)symtab) / sizeof(Elf_Sym);
+       return ((void *)strtab - (void *)symtab) / sizeof(Elf_Sym);
+}
+
+/* 
+ * Subtle:  Since libhugetlbfs depends on glibc, we allow it
+ * it to be loaded before us.  As part of its init functions, it
+ * initializes stdin, stdout, and stderr in the bss.  We need to
+ * include these initialized variables in our copy.
+ */
+
+static void get_extracopy(struct seg_info *seg, void *p, 
+                                                void **extra_start, void 
**extra_end)
+{
+       Elf_Dyn *dyntab;        /* dynamic segment table */
+       Elf_Sym *symtab = NULL; /* dynamic symbol table */
+       Elf_Sym *sym;           /* a symbol */
+       char *strtab = NULL;    /* string table for dynamic symbols */
+       int ret, numsyms, found_sym = 0;
+       void *start, *end, *start_orig, *end_orig;
+       void *sym_start, *sym_end;
+
+       end_orig = seg->vaddr + seg->memsz;
+       start_orig = seg->vaddr + seg->filesz;
+       if (seg->filesz == seg->memsz)
+               goto bail;
+       if (!minimal_copy)
+               goto bail;
+
+       /* Find dynamic program header */
+       ret = find_dynamic(&dyntab);
+       if (ret < 0)
+               goto bail;
+
+       /* Find symbol and string tables */
+       ret = find_tables(dyntab, &symtab, &strtab);
+       if (ret < 0)
+               goto bail;
+
+       numsyms = find_numsyms(symtab, strtab);
+       if (numsyms < 0)
+               goto bail;
 
        /* 
         * We must ensure any returns done hereafter have sane start and end 

-- 

Steve Fox
IBM Linux Technology Center

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Libhugetlbfs-devel mailing list
Libhugetlbfs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel

Reply via email to