public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Problem setting the CONTENTS flag
@ 2006-04-21 18:08 Andrew STUBBS
  2006-04-21 22:26 ` H. J. Lu
  0 siblings, 1 reply; 9+ messages in thread
From: Andrew STUBBS @ 2006-04-21 18:08 UTC (permalink / raw)
  To: binutils

Hi all,

I have encountered an error setting the CONTENTS and LOAD flags with 
relatively recent versions of objcopy/bfd.

Given the simplest program:

   int main()
   {
   }

and a simple linker script (not a real world example):

   SECTIONS
   {
     .text           :
     {
       *(.text .stub .text.* .gnu.linkonce.t.*)
     } =0
     .post_text_reserve :
     {
       /* Reserve some space so we can drop something in here later */
       . += 0x160;
     }
   }

compiled with:

   sh-elf-gcc test.c -T link2.ld -nostdlib

I get a file with the following properties:

   sh-elf-objdump -h a.out

   a.out:     file format elf32-shl

   Sections:
   Idx Name          Size      VMA       LMA       File off  Algn
     0 .text         0000000c  00000000  00000000  00000080  2**1
                     CONTENTS, ALLOC, LOAD, READONLY, CODE
     1 .post_text_reserve 00000160  0000000c  0000000c  0000008c  2**0
                     ALLOC
     2 .comment      00000043  00000000  00000000  0000008c  2**0
                     CONTENTS, READONLY

   sh-elf-readelf -S a.out

   There are 7 section headers, starting at offset 0x10c:

   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        00000000 000080 00000c 00 
AX  0   0  2
     [ 2] .post_text_reserv NOBITS          0000000c 00008c 000160 00 
WA  0   0  1
     [ 3] .comment          PROGBITS        00000000 00008c 000043 00 
    0   0  1
     [ 4] .shstrtab         STRTAB          00000000 0000cf 00003d 00 
    0   0  1
     [ 5] .symtab           SYMTAB          00000000 000224 0000b0 10 
    6   8  4
     [ 6] .strtab           STRTAB          00000000 0002d4 00001c 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)

All is good so far.

Now I want to add the missing flags to the .post_test_reserve (the next 
step would be to actually add some data).

So I do the following:

   sh-elf-objcopy --set-section-flags 
.post_text_reserve=contents,alloc,load,readonly,code a.out a2.out

Now I get:

   sh-elf-objdump -h a2.out

   a2.out:     file format elf32-shl

   Sections:
   Idx Name          Size      VMA       LMA       File off  Algn
     0 .text         0000000c  00000000  00000000  00000080  2**1
                     CONTENTS, ALLOC, LOAD, READONLY, CODE
     1 .post_text_reserve 00000160  0000000c  0000000c  0000008c  2**0
                     ALLOC, READONLY, CODE
     2 .comment      00000043  00000000  00000000  000001ec  2**0
                     CONTENTS, READONLY

   sh-elf-readelf -S a2.out

   There are 7 section headers, starting at offset 0x26c:

   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        00000000 000080 00000c 00 
AX  0   0  2
     [ 2] .post_text_reserv NOBITS          0000000c 00008c 000160 00 
AX  0   0  1
     [ 3] .comment          PROGBITS        00000000 0001ec 000043 00 
    0   0  1
     [ 4] .shstrtab         STRTAB          00000000 00022f 00003d 00 
    0   0  1
     [ 5] .symtab           SYMTAB          00000000 000384 0000b0 10 
    6   8  4
     [ 6] .strtab           STRTAB          00000000 000434 00001c 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)

The READONLY and CODE flags have been added, but the CONTENTS and LOAD 
flags remain absent. Also, the ELF section type remains NOBITS, where it 
ought to be PROGBITS.

After some examination of the source code and the ELF format I find that 
the CONTENTS and LOAD flags are in fact implicit in the PROGBITS section 
type, so all the above problems come down to the same issue.

AFAICT, the section type is set in elf_fake_sections(), but only if 
there is no existing type (sh_type != SHT_NULL). In this case the 
section already has a type - presumably loaded from the input file - and 
therefore does not have its type set. Thus the change to the CONTENTS 
and LOAD flags is ignored.

The documentation says:

  `--set-section-flags SECTION=FLAGS'
       Set the flags for the named section.  The FLAGS argument is a
       comma separated string of flag names.  The recognized names are
       `alloc', `contents', `load', `noload', `readonly', `code', `data',
       `rom', `share', and `debug'.  You can set the `contents' flag for
       a section which does not have contents, but it is not meaningful
       to clear the `contents' flag of a section which does have
       contents-just remove the section instead.  Not all flags are
       meaningful for all object file formats.

What I want to do - set the contents flag - is explicitly described.

This used to work in 2.15.94.0.1 from kernel.org, but it does not work 
in 2.16.91.0.5, also from kernel.org. Nor does it work in CVS HEAD.

I feel sure that I could bodge something up to fix this problem, but I 
do not know what the correct fix would be.

Any help would be appreciated.

Andrew Stubbs

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2006-04-26  9:38 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-21 18:08 Problem setting the CONTENTS flag Andrew STUBBS
2006-04-21 22:26 ` H. J. Lu
2006-04-21 23:32   ` Andreas Schwab
2006-04-23 22:13   ` H. J. Lu
2006-04-24 19:55     ` Andrew STUBBS
2006-04-25 16:37     ` Nick Clifton
2006-04-26  0:21       ` PATCH: binutils/2593: " H. J. Lu
2006-04-26 12:16         ` Nick Clifton
2006-04-26  0:39       ` H. J. Lu

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