[bitlug] Re: ELF??

  • From: Pavan T C <Pavan.Chandrashekar@xxxxxxx>
  • To: bitlug@xxxxxxxxxxxxx
  • Date: Sat, 12 Nov 2005 20:05:48 +0530

Peeyush Prasad wrote:

On Fri, 11 Nov 2005, Pavan T C wrote:

yeah, am using an ADSP blackfin processor here, got a gcc port for that... and no shared objects...
I'm interested in creating an executable that can be loaded at an address that I specify... looked at the --entry option in ld, that changed the entry address, but a readelf of the executable shows the section addresses have not changed...
For example, if you create an exe on your PC gcc and have a look and the disassembled output, you see that the start address is always 0x8048244, and that the other sections have addresses wrt to this start address.
Not so in my case.. I see the start address change, but not the section addrs.
In my case, I can use the --section-start <section name> <my addr> option to ld and specify addresses of my sections, but there has to be an easier way... maybe I have to specify somewhere that I want absolute code or

One better way to do it is to use a mapfile for the linker.

Hi all,
This is precisely what I dont want to get into: learning Linkder Command Language!

I dont think the relocatable objects are the defaults when you compile. On solaris (I dont have a linux box here), i need to use ld -r to specify that I need a relocatable object/exe. To verify that, I did a gcc -c and then used objdump -d to check the addresses and they were all relative to zero.

On gcc, just compilation gives you relocatable code... what else could it be?? you can check the e_type field of the ELF header to confirm that...
quoting ld :
Generate relocatable output---i.e., generate an output file that
can in turn serve as input to ld. This is often called partial
if this option is not specified, an absolute file is produced.

  which implies that what goes into ld has to be relocatable...

To see what gcc is using to build you executable, use gcc -v.
That should give you a fair idea of what you should override by passing the linker options via gcc in the command line to get what you want.
I'll see if I can play around with the linker during the weekend.

Now that's a good idea.. I spent long hours yesterday looking into Makefiles for that option...

Anyways , I managed to get what I wanted,here's my code:
#include "cdefBF532.h"

void blink()
   int i;
   *pFIO_DIR = 0x2;
     *pFIO_FLAG_T = 0x2;
     for(i=0; i<200000000; i++) continue;

int main()
   return 0;

command line:

gcc -fno-builtin -ffreestanding -nostdinc -c blink.c
gcc -fno-builtin -ffreestanding -nostdinc -c main.c
ld -Bstatic -Ttext 0xffa08000 -o bld blink.o main.o -Map blink.map

blink.map?? So you finally had to go for it, huh?

How difficult was it to put things into the mapfile? Can you post it? Hope the file size isnt too much.

...and here is my disassembled executable: (objdump -d -r bld) (Caution: The mnemonics are for the Blackfin DSP) bld: file format elf32-bfin

Disassembly of section .text:

ffa08000 <_blink>:
ffa08000:       00 e8 01 00     LINK 0x4;
ffa08004:       4a e1 c0 ff     P2.H=ffc0 <_blink-0xff9f8040>;
ffa08008:       0a e1 30 07     P2.L=730 <_blink-0xffa078d0>;
ffa0800c:       10 60           R0=0x2(x);
ffa0800e:       10 97           W[P2]=R0;
ffa08010:       4a e1 c0 ff     P2.H=ffc0 <_blink-0xff9f8040>;
ffa08014:       0a e1 0c 07     P2.L=70c <_blink-0xffa078f4>;
ffa08018:       10 60           R0=0x2(x);
ffa0801a:       10 97           W[P2]=R0;
ffa0801c:       00 60           R0=0x0(x);
ffa0801e:       f0 bb           [FP-4]=R0;
ffa08020:       f0 b9           R0=[FP-4];
ffa08022:       41 e1 eb 0b     R1.H=0xbeb;
ffa08026:       01 e1 ff c1     R1.L=-15873;
ffa0802a:       08 09           CC=R0<=R1;
ffa0802c:       f2 13           IF ! CC JUMP ffa08010 <_blink+0x10>;
ffa0802e:       f0 b9           R0=[FP-4];
ffa08030:       08 64           R0+=0x1;
ffa08032:       f0 bb           [FP-4]=R0;
ffa08034:       f6 2f           JUMP.S  ffa08020 <_blink+0x20>;
ffa08038 <_main>:
ffa08038:       00 e8 03 00     LINK 0xc;
ffa0803c:       ff e3 e2 ff     CALL  ffa08000 <_blink>;
ffa08040:       00 60           R0=0x0(x);
ffa08042:       01 e8 00 00     UNLINK;
ffa08046:       10 00           RTS;

Also, here's the 'readelf -a bld' output: (See comments inline)

ELF Header:
   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
   Class:                             ELF32
   Data:                              2's complement, little endian
   Version:                           1 (current)
   OS/ABI:                            UNIX - System V
   ABI Version:                       0
   Type:                              EXEC (Executable file)
   Machine:                           <unknown>: 6a
   Version:                           0x1
   Entry point address:               0xffa08000
  ^^^^^^^^^^^^^^^^^^^^   // I Think I need to change this to point to main
                         // starting (see symtab) 0xffa08038
   Start of program headers:          52 (bytes into file)
   Start of section headers:          4264 (bytes into file)
   Flags:                             0x0
   Size of this header:               52 (bytes)
   Size of program headers:           32 (bytes)
   Number of program headers:         2
   Size of section headers:           40 (bytes)
   Number of section headers:         9
   Section header string table index: 6

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 ffa08000 001000 000048 00 AX 0 0 4
[ 2] .data PROGBITS ffa09048 001048 000000 00 WA 0 0 4
[ 3] .sbss PROGBITS ffa09048 001048 000000 00 W 0 0 1
[ 4] .bss NOBITS ffa09048 001048 000000 00 WA 0 0 4
[ 5] .comment PROGBITS 00000000 001048 000024 00 0 0 1
[ 6] .shstrtab STRTAB 00000000 00106c 00003b 00 0 0 1
[ 7] .symtab SYMTAB 00000000 001210 000100 10 8 b 4
[ 8] .strtab STRTAB 00000000 001310 000035 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)

Program Headers:
   Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
   LOAD           0x001000 0xffa08000 0xffa08000 0x00048 0x00048 R E 0x1000
   LOAD           0x001048 0xffa09048 0xffa09048 0x00000 0x00000 RW  0x1000
   ^^^^^^^^^^^^^^ // What is this second load address??

The first is clearly text and the second is data (In the table above, you can see that .data,.sbss (dont know what that is) and .bss start at 0xffa09048). Am I missing something grossly here?

If your dilemma is 'why two loads when everything is static?', I feel that you need separate setup for pages (sections) with different permissions. I am only guessing. We need to better understand the LOAD linker command.


Section to Segment mapping: Segment Sections... 00 .text 01

There is no dynamic segment in this file.

There are no relocations in this file.

There are no unwind sections in this file.

Symbol table '.symtab' contains 16 entries:
    Num:    Value  Size Type    Bind   Vis      Ndx Name
      0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
      1: ffa08000     0 SECTION LOCAL  DEFAULT    1
      2: ffa09048     0 SECTION LOCAL  DEFAULT    2
      3: ffa09048     0 SECTION LOCAL  DEFAULT    3
      4: ffa09048     0 SECTION LOCAL  DEFAULT    4
      5: 00000000     0 SECTION LOCAL  DEFAULT    5
      6: 00000000     0 SECTION LOCAL  DEFAULT    6
      7: 00000000     0 SECTION LOCAL  DEFAULT    7
      8: 00000000     0 SECTION LOCAL  DEFAULT    8
      9: 00000000     0 FILE    LOCAL  DEFAULT  ABS blink.c
     10: 00000000     0 FILE    LOCAL  DEFAULT  ABS main.c
     11: ffa08000    54 FUNC    GLOBAL DEFAULT    1 _blink
     12: ffa09048     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
     13: ffa09048     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
     14: ffa09048     0 NOTYPE  GLOBAL DEFAULT  ABS _end
     15: ffa08038    16 FUNC    GLOBAL DEFAULT    1 _main

No version information found in this file.

.. or maybe I have to get position-independent-code and then just specify the start address ?

I dont know how that will work when you have multiple objects being linked. I feel it is better to go with the default placement and use the mapfile to tweak what is impt for you.

I was under the impression that the linker(loader??) will change the symbol addresses when he sees that the code is relocatable, and I have given him an explicit start address... ie, *somebody* should take care of all the relocatable sections and assign them addresses. Of course I can do this with the map file, but I was hoping I could give a start addr and get away with not writing a map file...

Another interesting thing I will try is to change the default load address of linux executables, I mean, why 0x8048244 ?? :-)

-- P

Other related posts: