public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "Radouch, Zdenek" <zradouch@irobot.com>
To: "binutils@sourceware.org" <binutils@sourceware.org>
Subject: extract ELF load address with binutils?
Date: Tue, 18 Mar 2014 17:02:00 -0000	[thread overview]
Message-ID: <7ADBB2DB4DC7CF4CB641E9ADA826E5E2E1FC2052@hq-mbx-02.wardrobe.irobot.com> (raw)
In-Reply-To: <m3k3brwvew.fsf@pepe.airs.com>

I am writing a firmware updater that takes an ELF executable and needs to extract the RAM data
and the address to where the data should be loaded. I create the data chunk with objcopy -O binary,
and need the address of the first section that went into that chunk. I'd like to do that
from a shell script invoking binutils (rather than writing my own version of a binutil),
but can't figure out how. My first intuitive solution "readelf -l" does not work at all.

Here is an example file (b2.axf) I get from my vendor.
[the file represents a RAM image with 3032 bytes @ 0x15f000]
                        
$ file b2.axf             
b2.axf: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
$ arm-none-eabi-size b2.axf
   text	   data	    bss	    dec	    hex	filename
   3028	      4	   2052	   5084	   13dc	b2.axf
$ arm-none-eabi-objcopy -O binary b2.axf xxx
$ wc -c xxx
3032 xxx
$     

So far, all is well. I got my 3032-byte chunk of data and confirmed
its size (3028 text + 4 data). The question is where is this chunk loaded?

$ arm-none-eabi-readelf -l b2.axf

Elf file type is EXEC (Executable file)
Entry point 0x15f001
There are 2 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x00158000 0x00158000 0x07bd8 0x07bd8 RWE 0x8000
  LOAD           0x00f000 0x2001f000 0x2001f000 0x00000 0x00804 RW  0x8000

 Section to Segment mapping:
  Segment Sections...
   00     .text .data 
   01     .bss .main_stack 
$                                

I don't understand the purpose of this output; it appears (certainly from the loading
perspective) wrong, as the second segment should not be loaded at all, and the first
one includes some 28k of alignment-related padding loaded at addresses that may not
even exist (0x158000) within the hardware.

Clearly, the ELF file has what I need: the Addr field of the .text section ([1])
is the load address. See below
  
$ 
$ arm-none-eabi-readelf -S b2.axf
There are 18 section headers, starting at offset 0x246a4:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        0015f000 007000 000bd4 00  AX  0   0  4
  [ 2] .data             PROGBITS        0015fbd4 007bd4 000004 00  WA  0   0  4
  [ 3] .bss              NOBITS          2001f000 00f000 000004 00  WA  0   0  4
  [ 4] .main_stack       NOBITS          2001f004 00f000 000800 00  WA  0   0  1
  [ 5] .debug_info       PROGBITS        00000000 007bd8 00e4e3 00      0   0  1
  [ 6] .debug_abbrev     PROGBITS        00000000 0160bb 001a2a 00      0   0  1
  [ 7] .debug_loc        PROGBITS        00000000 017ae5 002e67 00      0   0  1
  [ 8] .debug_aranges    PROGBITS        00000000 01a94c 000690 00      0   0  1
  [ 9] .debug_ranges     PROGBITS        00000000 01afdc 000688 00      0   0  1
  [10] .debug_line       PROGBITS        00000000 01b664 0025eb 00      0   0  1
  [11] .debug_str        PROGBITS        00000000 01dc4f 005aeb 01  MS  0   0  1
  [12] .comment          PROGBITS        00000000 02373a 000030 01  MS  0   0  1
  [13] .ARM.attributes   ARM_ATTRIBUTES  00000000 02376a 000033 00      0   0  1
  [14] .debug_frame      PROGBITS        00000000 0237a0 000e4c 00      0   0  4
  [15] .shstrtab         STRTAB          00000000 0245ec 0000b7 00      0   0  1
  [16] .symtab           SYMTAB          00000000 024974 000bf0 10     17 142  4
  [17] .strtab           STRTAB          00000000 025564 0003aa 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
$      

The question is can I somehow convince one of the binutils to give me the load address alone,
so that I don't have to invent an algorithm extracting the address from the section dump?

Thanks,
-Z



       reply	other threads:[~2014-03-18 17:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <7ADBB2DB4DC7CF4CB641E9ADA826E5E2AAC571BC@HQ-MBX-01.wardrobe.irobot.com>
     [not found] ` <m3a9i3s8s1.fsf@pepe.airs.com>
     [not found]   ` <7ADBB2DB4DC7CF4CB641E9ADA826E5E2E1FBDF7F@hq-mbx-02.wardrobe.irobot.com>
     [not found]     ` <m3siqgw32g.fsf@pepe.airs.com>
     [not found]       ` <7ADBB2DB4DC7CF4CB641E9ADA826E5E2E1FC0FF6@hq-mbx-02.wardrobe.irobot.com>
     [not found]         ` <m3k3brwvew.fsf@pepe.airs.com>
2014-03-18 17:02           ` Radouch, Zdenek [this message]
2014-03-18 17:13             ` Paul_Koning
2014-03-18 17:44               ` Radouch, Zdenek
2014-03-18 23:44                 ` Alan Modra
2014-03-19 12:05                   ` Radouch, Zdenek
2014-03-19 13:24                     ` Erik Christiansen
2014-03-20 18:12                     ` Radouch, Zdenek

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=7ADBB2DB4DC7CF4CB641E9ADA826E5E2E1FC2052@hq-mbx-02.wardrobe.irobot.com \
    --to=zradouch@irobot.com \
    --cc=binutils@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).