Bug 2593 - Cannot set the CONTENTS or LOAD flags
Summary: Cannot set the CONTENTS or LOAD flags
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.18
: P2 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-04-21 15:43 UTC by Andrew Stubbs
Modified: 2006-04-26 13:38 UTC (History)
2 users (show)

See Also:
Host:
Target: sh-elf
Build:
Last reconfirmed:


Attachments
Input testcase for objcopy (330 bytes, application/octet-stream)
2006-04-21 16:49 UTC, Andrew Stubbs
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Stubbs 2006-04-21 15:43:24 UTC
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 have tested it with sh-elf, but the bug probably also occurs with other targets.
Comment 1 Andrew Stubbs 2006-04-21 16:05:32 UTC
Subject: Re:  New: Cannot set the CONTENTS or LOAD flags

[moving the conversation to bugzilla ...]

Thanks H.J., but this does not seem to work for me. I cannot see any 
difference.

> On Fri, Apr 21, 2006 at 01:58:08PM +0100, Andrew STUBBS wrote:
>> > Hi all,
>> > 
>> > I have encountered an error setting the CONTENTS and LOAD flags with 
>> > relatively recent versions of objcopy/bfd.
>> > 
> 
> Could you please open a bug report with a simple testcase? This patch
> should work.
> 
> Thanks.
> 
> 
> H.J.
> ----
> 2006-04-21  H.J. Lu  <hongjiu.lu@intel.com>
> 
> 	* elf.c (_bfd_elf_new_section_hook): Don't set section ELF type
> 	and flags if its BFD flags has been set.
> 
> --- bfd/elf.c.copy	2006-04-21 07:00:02.000000000 -0700
> +++ bfd/elf.c	2006-04-21 07:32:26.000000000 -0700
> @@ -2487,10 +2487,11 @@ _bfd_elf_new_section_hook (bfd *abfd, as
>    bed = get_elf_backend_data (abfd);
>    sec->use_rela_p = bed->default_use_rela_p;
>  
> -  /* When we read a file, we don't need section type and flags unless
> -     it is a linker created section.  They will be overridden in
> -     _bfd_elf_make_section_from_shdr anyway.  */
> -  if (abfd->direction != read_direction
> +  /* When we read a file or section BFD flags has been set, we don't
> +     need section type and flags unless it is a linker created section.
> +     They will be overridden in _bfd_elf_make_section_from_shdr
> +     anyway.  */
> +  if ((!sec->flags && abfd->direction != read_direction)
>        || (sec->flags & SEC_LINKER_CREATED) != 0)
>      {
>        ssect = (*bed->get_sec_type_attr) (abfd, sec);
> 

Comment 2 H.J. Lu 2006-04-21 16:22:32 UTC
Please provide a testcase I can reproduce with Linux/x86 or Linux/x86-64.
Comment 3 Andrew Stubbs 2006-04-21 16:49:17 UTC
Created attachment 977 [details]
Input testcase for objcopy

This is the binary file produced by the method in the original report.

Additionally, I have modified the magic number so that it *looks* like an 80386
binary even though it is really an SH binary. (This was easier than building a
whole x86 toolchain just to produce an elf file.)

This allows the x86 objcopy to reproduce the problem.
Comment 4 H.J. Lu 2006-04-21 23:22:57 UTC
A new patch is posted at

http://sourceware.org/ml/binutils/2006-04/msg00309.html
Comment 5 H.J. Lu 2006-04-25 22:30:15 UTC
A testcase patch is posted at

http://sourceware.org/ml/binutils/2006-04/msg00355.html
Comment 6 H.J. Lu 2006-04-26 13:38:09 UTC
Fixed.