On Fri, 2007-02-09 at 16:04 +1100, David Gibson wrote:
> On Thu, Feb 08, 2007 at 08:50:56PM -0800, Nishanth Aravamudan wrote:
> > 
> > Then why don't we remove ehdr from global scope? Would mean passing it
> > around perhaps more, but would also make the code more explicit.
>  
> That sounds like a much better idea.

Unfortunately that would mean passing a pointer around several layers of
sub-functions which don't even need the pointer.. really ugly (see
below). We really only need this pointer inside parse_phdrs() and
find_dynamic(). Maybe someone can think of clever way to get it there?


diff --git a/elflink.c b/elflink.c
index 9e4f6a0..93d7a90 100644
--- a/elflink.c
+++ b/elflink.c
@@ -198,7 +198,6 @@ static int htlb_num_segs;
 static int minimal_copy = 1;
 static int sharing; /* =0 */
 int __debug = 0;
-static Elf_Ehdr *ehdr;
 
 /**
  * assemble_path - handy wrapper around snprintf() for building paths
@@ -406,7 +405,7 @@ static int get_shared_file_name(struct seg_info 
*htlb_seg_info, char *file_path)
 }
 
 /* Find the .dynamic program header */
-static int find_dynamic(Elf_Dyn **dyntab)
+static int find_dynamic(Elf_Ehdr *ehdr, Elf_Dyn **dyntab)
 {
        Elf_Phdr *phdr; /* program header table */
        int i = 1;
@@ -497,7 +496,7 @@ static inline int keep_symbol(Elf_Sym *s, void *start, void 
*end)
  * include these initialized variables in our copy.
  */
 
-static void get_extracopy(struct seg_info *seg)
+static void get_extracopy(Elf_Ehdr *ehdr, struct seg_info *seg)
 {
        Elf_Dyn *dyntab;        /* dynamic segment table */
        Elf_Sym *symtab = NULL; /* dynamic symbol table */
@@ -515,7 +514,7 @@ static void get_extracopy(struct seg_info *seg)
                goto bail2;
 
        /* Find dynamic program header */
-       ret = find_dynamic(&dyntab);
+       ret = find_dynamic(ehdr, &dyntab);
        if (ret < 0)
                goto bail;
 
@@ -577,7 +576,7 @@ bail2:
  * smallest amount of data possible, unless the user disables this 
  * optimization via the HUGETLB_ELFMAP environment variable.
  */
-static int prepare_segment(struct seg_info *seg)
+static int prepare_segment(Elf_Ehdr *ehdr, struct seg_info *seg)
 {
        int hpage_size = gethugepagesize();
        void *p;
@@ -588,7 +587,7 @@ static int prepare_segment(struct seg_info *seg)
         * Calculate the BSS size that we must copy in order to minimize
         * the size of the shared mapping.
         */
-       get_extracopy(seg);
+       get_extracopy(ehdr, seg);
        if (seg->extra_vaddr) {
                size = ALIGN((unsigned long)seg->extra_vaddr +
                                seg->extrasz - (unsigned long)seg->vaddr,
@@ -665,7 +664,7 @@ static int prepare_segment(struct seg_info *seg)
  *   -1, on failure
  *   0, on success
  */
-static int find_or_prepare_shared_file(struct seg_info *htlb_seg_info)
+static int find_or_prepare_shared_file(Elf_Ehdr *ehdr, struct seg_info 
*htlb_seg_info)
 {
        int fdx, fds;
        int errnox, errnos;
@@ -714,7 +713,7 @@ static int find_or_prepare_shared_file(struct seg_info 
*htlb_seg_info)
                        htlb_seg_info->fd = fdx;
 
                        DEBUG("Got unpopulated shared fd -- Preparing\n");
-                       ret = prepare_segment(htlb_seg_info);
+                       ret = prepare_segment(ehdr, htlb_seg_info);
                        if (ret < 0)
                                goto fail;
 
@@ -760,7 +759,7 @@ static int find_or_prepare_shared_file(struct seg_info 
*htlb_seg_info)
  *  -1, on error
  *  0, on success
  */
-static int obtain_prepared_file(struct seg_info *htlb_seg_info)
+static int obtain_prepared_file(Elf_Ehdr *ehdr, struct seg_info *htlb_seg_info)
 {
        int fd = -1;
        int ret;
@@ -768,7 +767,7 @@ static int obtain_prepared_file(struct seg_info 
*htlb_seg_info)
        /* Share only read-only segments */
        if (sharing && !(htlb_seg_info->prot & PROT_WRITE)) {
                /* first, try to share */
-               ret = find_or_prepare_shared_file(htlb_seg_info);
+               ret = find_or_prepare_shared_file(ehdr, htlb_seg_info);
                if (ret == 0)
                        return 0;
                /* but, fall through to unlinked files, if sharing fails */
@@ -779,7 +778,7 @@ static int obtain_prepared_file(struct seg_info 
*htlb_seg_info)
                return -1;
        htlb_seg_info->fd = fd;
 
-       ret = prepare_segment(htlb_seg_info);
+       ret = prepare_segment(ehdr, htlb_seg_info);
        if (ret < 0) {
                DEBUG("Failed to prepare segment\n");
                return -1;
@@ -939,7 +938,7 @@ static int check_env(void)
 static void __attribute__ ((constructor)) setup_elflink(void)
 {
        extern Elf_Ehdr __executable_start __attribute__((weak));
-       ehdr = &__executable_start;
+       Elf_Ehdr *ehdr = &__executable_start;
        int ret, i;
 
        if (! ehdr) {
@@ -967,7 +966,7 @@ static void __attribute__ ((constructor)) 
setup_elflink(void)
 
        /* Step 1.  Obtain hugepage files with our program data */
        for (i = 0; i < htlb_num_segs; i++) {
-               ret = obtain_prepared_file(&htlb_seg_table[i]);
+               ret = obtain_prepared_file(ehdr, &htlb_seg_table[i]);
                if (ret < 0) {
                        DEBUG("Failed to setup hugetlbfs file\n");
                        return;

 
-- 

Steve Fox
IBM Linux Technology Center


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Libhugetlbfs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel

Reply via email to