Index: trunk/softs/tsar_boot/src/boot_elf_loader.c
===================================================================
--- trunk/softs/tsar_boot/src/boot_elf_loader.c	(revision 388)
+++ trunk/softs/tsar_boot/src/boot_elf_loader.c	(revision 412)
@@ -37,8 +37,8 @@
     unsigned int i;
     unsigned int segment_req;
-   
+
     /*
      * Loader state machine definition
-     */  
+     */
     enum
     {
@@ -48,5 +48,20 @@
         ELF_SEGMENT_STATE,
         ELF_END_STATE
-    } init_state;
+    } init_state
+#if (BOOT_DEBUG ==1)
+    , init_state_debug
+#endif
+        ;
+
+#if (BOOT_DEBUG == 1)
+    char* init_state_str[] = {
+        "ELF_HEADER_STATE",
+        "ELF_PROGRAM_HEADER_STATE",
+        "ELF_OFFSET_STATE",
+        "ELF_SEGMENT_STATE",
+        "ELF_END_STATE"
+    };
+#endif
+
 
     boot_puts("Starting boot_elf_loader function...\n\r");
@@ -63,4 +78,7 @@
 
     init_state         = ELF_HEADER_STATE;
+#if (BOOT_DEBUG == 1)
+    init_state_debug   = ELF_END_STATE;
+#endif
 
     while(init_state != ELF_END_STATE)
@@ -88,4 +106,14 @@
         offset  +=  nb_read;
 
+#if (BOOT_DEBUG == 1)
+        if (init_state != init_state_debug)
+        {
+            boot_puts("init_state = ");
+            boot_puts(init_state_str[init_state]);
+            boot_puts("\n");
+            init_state_debug = init_state;
+        }
+#endif
+
         switch(init_state)
         {
@@ -113,12 +141,12 @@
                             "Input file does not use ELF format"
                             "\n"
-                        );  
+                        );
 
                         boot_exit();
                     }
 
-                    /* 
+                    /*
                      * Verification of Program Headers table size. It must be
-                     * smaller than the work size allocated for the 
+                     * smaller than the work size allocated for the
                      * elf_pht[PHDR_ARRAY_SIZE] array
                      **/
@@ -142,5 +170,5 @@
             /**
              * Reading ELF program headers
-             */  
+             */
             case ELF_PROGRAM_HEADER_STATE:
                 boot_memcpy(elf_pht_ptr, buffer_ptr, nb_read);
@@ -160,10 +188,41 @@
                         if(elf_pht_ptr[pseg].p_type == PT_LOAD)
                         {
-                            nb_rest = elf_pht_ptr[pseg].p_offset - offset;
+#if (BOOT_DEBUG == 1)
+                            boot_puts("found a loadable segment:\n");
+                            boot_puts("- type   : "); boot_putx(elf_pht_ptr[pseg].p_type);    boot_puts("\n");
+                            boot_puts("- offset : "); boot_putx(elf_pht_ptr[pseg].p_offset);  boot_puts("\n");
+                            boot_puts("- vaddr  : "); boot_putx(elf_pht_ptr[pseg].p_vaddr);   boot_puts("\n");
+                            boot_puts("- paddr  : "); boot_putx(elf_pht_ptr[pseg].p_paddr);   boot_puts("\n");
+                            boot_puts("- filesz : "); boot_putx(elf_pht_ptr[pseg].p_filesz);  boot_puts("\n");
+                            boot_puts("- memsz  : "); boot_putx(elf_pht_ptr[pseg].p_memsz);   boot_puts("\n");
+                            boot_puts("- flags  : "); boot_putx(elf_pht_ptr[pseg].p_flags);   boot_puts("\n");
+                            boot_puts("- align  : "); boot_putx(elf_pht_ptr[pseg].p_align);   boot_puts("\n");
+#endif
+                            if (elf_pht_ptr[pseg].p_offset < offset)
+                            {
+                                /* case where the segment to load includes the elf and program headers */
+                                nb_rest = elf_pht_ptr[pseg].p_filesz - offset;
+                                init_state = ELF_SEGMENT_STATE;
+                            }
+                            else
+                            {
+                                /* segment to load is further away in memory */
+                                nb_rest = elf_pht_ptr[pseg].p_offset - offset;
+                                init_state = ELF_OFFSET_STATE;
+                            }
                             break;
                         }
                     }
 
-                    init_state = ELF_OFFSET_STATE;
+                    if (pseg == elf_header_ptr->e_phnum)
+                    {
+                        boot_puts(
+                            "ERROR: "
+                            "No PT_LOAD found"
+                            "\n"
+                        );
+                        boot_exit();
+                    }
+
                 }
 
@@ -191,5 +250,5 @@
                  * Copying ELF segment data in memory segments using the virtual
                  * address got from the ELF file
-                 */ 
+                 */
                 segment_req = ((elf_pht_ptr[pseg].p_vaddr & 0xBFC00000) != 0xBFC00000);
 
@@ -201,5 +260,5 @@
                                 nb_read);
                 }
-    
+
                 nb_rest -= nb_read;
 
@@ -237,5 +296,5 @@
                     /*
                      * Program loading finished
-                     */ 
+                     */
                     if(pseg == elf_header_ptr->e_phnum)
                     {
