public inbox for ecos-devel@sourceware.org
 help / color / mirror / Atom feed
From: Bert Thomas <bert@brothom.nl>
To: ecos-devel@ecos.sourceware.org
Subject: bug in RedBoot ELF loader?
Date: Tue, 06 Jun 2006 12:14:00 -0000	[thread overview]
Message-ID: <44857F82.1020302@brothom.nl> (raw)

Hi all,

I think I may have found a bug in the RedBoot ELF loader, but it may be 
just as well that I don't understand the code well enough. Here's the part:

*************
    for (phx = 0;  phx < ehdr.e_phnum;  phx++) {
         if (phdr[phx].p_type == PT_LOAD) {
             // Loadable segment
             addr = (unsigned char *)phdr[phx].p_vaddr;
             len = phdr[phx].p_filesz;
             if ((unsigned long)addr < lowest_address) {
                 lowest_address = (unsigned long)addr;
             }
             addr += addr_offset;
             if (offset > phdr[phx].p_offset) {
                 if ((phdr[phx].p_offset + len) < offset) {
                     diag_printf("Can't load ELF file - program headers 
out of order\n");
                     return 0;
                 }
                 addr += offset - phdr[phx].p_offset;
             } else {
                 while (offset < phdr[phx].p_offset) {
                     if ((*getc)() < 0) {
                         diag_printf(SHORT_DATA);
                         return 0;
                     }
                     offset++;
                 }
             }
*************

It appears that normally the first program segment header contains a 
file offset of 0. This is on purpose, to tell the loader to load the ELF 
header and the program segment headers as well I am told. However, this 
is where the redboot loader fails. It can't seek back to the beginning 
of the file. The destination address in memory is compensated for that 
by this line:

addr += offset - phdr[phx].p_offset;

(At least, that's what I think).

However, I know for sure that it is incorrect to read 'len' bytes from 
the current offset instead of the real offset. This results in trouble 
when the next segment to be loaded is smaller then the "offset error". 
In some of my experiments the next segment was a "comment" segment that 
I didn't really need to have in memory. However, the loader failed 
loading my segment, saying "Can't load ELF file - program headers out of 
order". They weren't out of order, but I can see why it comes to that 
conclusion.

Example:

An ELF file with 2 segments:
segment 0: offset in file = 0, length = 0xac
segment 1: offset in file = 0xac, length = 15

First iteration, to load segment 0, offset=84
len = 0xac (172 bytes), so start reading 172 bytes from offset 84
Second iteration,to load segment 1, offset=256 (84+172)
len = 15, expected offset in file=172 (0xac), expected end in file = 
172+15=187
The current offset is 256 and thus larger then 187 -> error!

My suggested fix would be to adjust 'len' to the remaining bytes, say:

len -= offset;

Regards,
Bert

             reply	other threads:[~2006-06-06 12:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-06 12:14 Bert Thomas [this message]
2006-06-06 14:53 ` David Vrabel
2006-06-06 17:23   ` Bert Thomas
2006-06-06 17:34     ` Gary Thomas
     [not found]       ` <4485EE86.9040909@brothom.nl>
     [not found]         ` <1149624902.15359.55.camel@hermes>
     [not found]           ` <4485F487.6080000@brothom.nl>
2006-06-06 20:49             ` Gary Thomas
2006-06-07  8:39               ` Bert Thomas
2006-06-07 11:55                 ` Gary Thomas
2006-06-07 10:04 Daly, Jeffrey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=44857F82.1020302@brothom.nl \
    --to=bert@brothom.nl \
    --cc=ecos-devel@ecos.sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).