Stop readelf from complaining about out of order PT_LOAD segments.
[binutils-gdb.git] / binutils / readelf.c
index 114486cc986d63d60d19feb198f4912c34d19f51..c5a628fc9365586c2ac85db0dd844070d56b3f7b 100644 (file)
@@ -3797,7 +3797,7 @@ get_segment_type (unsigned long p_type)
          if (result != NULL)
            return result;
 
-         sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
+         sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
        }
       else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
        {
@@ -3822,7 +3822,7 @@ get_segment_type (unsigned long p_type)
          if (result != NULL)
            return result;
 
-         sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
+         sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
        }
       else
        snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
@@ -4770,6 +4770,7 @@ process_program_headers (FILE * file)
 {
   Elf_Internal_Phdr * segment;
   unsigned int i;
+  Elf_Internal_Phdr * previous_load = NULL;
 
   if (elf_header.e_phnum == 0)
     {
@@ -4901,13 +4902,43 @@ process_program_headers (FILE * file)
                      (segment->p_flags & PF_X ? 'E' : ' '));
              print_vma (segment->p_align, HEX);
            }
-       }
 
-      if (do_segments)
-       putc ('\n', stdout);
+         putc ('\n', stdout);
+       }
 
       switch (segment->p_type)
        {
+       case PT_LOAD:
+#if 0 /* Do not warn about out of order PT_LOAD segments.  Although officially
+        required by the ELF standard, several programs, including the Linux
+        kernel, make use of non-ordered segments.  */
+         if (previous_load
+             && previous_load->p_vaddr > segment->p_vaddr)
+           error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
+#endif
+         if (segment->p_memsz < segment->p_filesz)
+           error (_("the segment's file size is larger than its memory size\n"));
+         previous_load = segment;
+         break;
+
+       case PT_PHDR:
+         /* PR 20815 - Verify that the program header is loaded into memory.  */
+         if (i > 0 && previous_load != NULL)
+           error (_("the PHDR segment must occur before any LOAD segment\n"));
+         if (elf_header.e_machine != EM_PARISC)
+           {
+             unsigned int j;
+
+             for (j = 1; j < elf_header.e_phnum; j++)
+               if (program_headers[j].p_vaddr <= segment->p_vaddr
+                   && (program_headers[j].p_vaddr + program_headers[j].p_memsz)
+                   >= (segment->p_vaddr + segment->p_filesz))
+                 break;
+             if (j == elf_header.e_phnum)
+               error (_("the PHDR segment is not covered by a LOAD segment\n"));
+           }
+         break;
+
        case PT_DYNAMIC:
          if (dynamic_addr)
            error (_("more than one dynamic segment\n"));
@@ -5978,7 +6009,7 @@ process_section_headers (FILE * file)
        case SHT_REL:
        case SHT_RELA:
          if (section->sh_link < 1
-             || section->sh_link > elf_header.e_shnum
+             || section->sh_link >= elf_header.e_shnum
              || (section_headers[section->sh_link].sh_type != SHT_SYMTAB
                  && section_headers[section->sh_link].sh_type != SHT_DYNSYM))
            warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
@@ -5992,7 +6023,7 @@ process_section_headers (FILE * file)
        case SHT_GNU_verdef:
        case SHT_GNU_LIBLIST:
          if (section->sh_link < 1
-             || section->sh_link > elf_header.e_shnum
+             || section->sh_link >= elf_header.e_shnum
              || section_headers[section->sh_link].sh_type != SHT_STRTAB)
            warn (_("[%2u]: Link field (%u) should index a string section.\n"),
                  i, section->sh_link);
@@ -6025,7 +6056,7 @@ process_section_headers (FILE * file)
        case SHT_REL:
        case SHT_RELA:
          if (section->sh_info < 1
-             || section->sh_info > elf_header.e_shnum
+             || section->sh_info >= elf_header.e_shnum
              || (section_headers[section->sh_info].sh_type != SHT_PROGBITS
                  && section_headers[section->sh_info].sh_type != SHT_NOBITS
                  && section_headers[section->sh_info].sh_type != SHT_NOTE
@@ -6068,11 +6099,13 @@ process_section_headers (FILE * file)
          if (section->sh_type == SHT_NOBITS)
            /* NOBITS section headers with non-zero sh_info fields can be
               created when a binary is stripped of everything but its debug
-              information.  The stripped sections have their headers preserved but their types set to SHT_NOBITS.  so do not check this type of section.  */
+              information.  The stripped sections have their headers
+              preserved but their types set to SHT_NOBITS.  So do not check
+              this type of section.  */
            ;
          else if (section->sh_flags & SHF_INFO_LINK)
            {
-             if (section->sh_info < 1 || section->sh_info > elf_header.e_shnum)
+             if (section->sh_info < 1 || section->sh_info >= elf_header.e_shnum)
                warn (_("[%2u]: Expected link to another section in info field"), i);
            }
          else if (section->sh_type < SHT_LOOS && section->sh_info != 0)
@@ -9937,7 +9970,7 @@ process_version_sections (FILE * file)
                int j;
                int isum;
 
-               /* Check for very large indicies.  */
+               /* Check for very large indices.  */
                if (idx > (size_t) (endbuf - (char *) edefs))
                  break;