how to add segment at address 0?

Vladimir Kondratiev vladimir.kondratiev@intel.com
Tue Feb 4 22:03:00 GMT 2003


Nick,
sorry for stupid question regarding where to put .note.ABI-tag section. 
Following quite simple fragment do work.
The only excuse for me is that after 16 hours of debug it's hard to 
concentrate.
[]$ cat zero_data.c       
int z_i=123;
[]$ cat main.c    
#include <stdio.h>

extern int z_i;

int main(int argc, char* argv[])
{
  printf("zero segment: z_i=%d at %p\n",z_i,&z_i);
  return 0;
}
[]$ ./zero
zero segment: z_i=123 at (nil)

Vladimir.

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

SECTIONS
{
  . = 0;
  .data_at_zero   : { zero_data.o(.data) } :data_zero
  /* Read-only sections, merged into text segment: */
  . = 0x08048000 + SIZEOF_HEADERS;
  .interp         : { *(.interp) } :text :interp
  .note.ABI-tag   : { *(.note*) } :text :note
  .hash           : { *(.hash) } :text

Nick Clifton wrote:

> 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
>




More information about the Binutils mailing list