This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: how to add segment at address 0?


Hi Vladimir,

> I need to allocate some data at address 0. Otherwise, it is ordinal
> "C" program. All I need is one section starting at address 0.
> 
> I tried to tweak LD script to add one more segment. Here I faced with
> problem:
> 
> I printed "default" LD script using "--verbose" flag and started to
> modify it. But, since I need to add segment, I need to use PHDRS. LD
> "default" script have no PHDRS directive. On myself, I did not
> succeeded to write proper PHDRS and set up sections to use proper
> segments.
> 
> How should I do it? Let's say, I need to write "C" program where
> section ".zero_based" will start at address 0.

The linker manual can tell you all you need to know.

However you may find the following example helpful.

  % cat data.c
  int data_at_zero = 1;

  % cat text.c
  int data_not_at_zero = 2;
  extern int data_at_zero;

  int main (void) { data_not_at_zero = data_at_zero; return 0; }

  % gcc data.c text.c -Wl,-T -Wl,zero.ld
  % objdump -h a.out | head

  a.out:     file format elf32-i386
  
  Sections:
  Idx Name          Size      VMA       LMA       File off  Algn
    0 .data_at_zero 00000004  00000000  00000000  00001000  2**2
                    CONTENTS, ALLOC, LOAD, DATA
    1 .interp       00000013  080480f4  080480f4  000000f4  2**0
                    CONTENTS, ALLOC, LOAD, READONLY, DATA
    2 .note.ABI-tag 00000020  08048108  08048108  00000108  2**2

I have attached zero.ld which was made from the default linker script
and adding a section like this:

  . = 0; .data_at_zero : { data.o(.data) } :data_zero

and a set of PHDRS like this:

  PHDRS
  {
    headers    PT_PHDR PHDRS ;
    interp     PT_INTERP ;
    text       PT_LOAD FILEHDR PHDRS ;
    data       PT_LOAD ;
    data_zero  PT_LOAD ;
    dynamic    PT_DYNAMIC ;
  }

Cheers
        Nick

Attachment: zero.ld
Description: zero.ld


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]