[PATCH v2 08/28] gas/write.c: add missing flag SEC_HAS_CONTENT on object attributes section
Matthieu Longo
matthieu.longo@arm.com
Wed May 7 15:45:40 GMT 2025
On 2025-05-02 14:44, Jan Beulich wrote:
> On 02.05.2025 12:32, Matthieu Longo wrote:
>> A section with a content should have this flag set. bfd_set_section_contents,
>> which is used inside _bfd_elf_write_section_build_attributes, will ignore the
>> content of the section if the flag SEC_HAS_CONTENT is not set.
>
> How did things end up working so far, in the common case? Also, related to my
> question on v1, how is this assembler change related to something the linker
> does? There's no SHF_* counterpart to SEC_HAS_CONTENTS. The closest appears to
> be SHF_ALLOC, established from SEC_ALLOC afaict. Whereas e.g.
> _bfd_elf_make_section_from_shdr() derives SEC_HAS_CONTENTS from the section
> type not being SHT_NOBITS.
>
> IOW as of now everything looks to be correct. I can certainly imagine the
> change becoming important for subsequent patches, but then that's what you
> want to provide as justification.
>
> Jan
>
## The story behind this change.
First of all, let me explain you why I tried to add the same flags when
creating a section from the linker and from gas.
When I added a function to create a section from the linker, I
copy-pasted the flags from create_obj_attrs_section in gas (so
SEC_HAS_CONTENTS was not there).
When I ran the linker tests, I had a lot of failures because readelf
could not find the ARM.attributes section in the output file. I found
out that the issue was the lack of SEC_HAS_CONTENTS which was causing
the whole section to be skipped during the serialization.
Creating the same section but with different flags depending on where it
is created, seemed very strange to me. So I decided to add the flag
SEC_HAS_CONTENTS to create_obj_attrs_section in gas, even if it is
unused by gas.
## Explanations needed here
### SEC_ALLOC and SEC_LOAD
> There's no SHF_* counterpart to SEC_HAS_CONTENTS. Whereas e.g.
> _bfd_elf_make_section_from_shdr() derives SEC_HAS_CONTENTS from the
> section type not being SHT_NOBITS.
I am trying to make sense of what you mean here.
I found in https://refspecs.linuxbase.org/elf/gabi4+/ch4.sheader.html,
Figure 4.11 those SHF_* flags, and when I compare them to the list of
flags in bfd/bfd.h, I am really lost.
I don't understand what the following comments mean:
/* Tells the OS to allocate space for this section when loading.
This is clear for a section containing debug information only. */
#define SEC_ALLOC 0x1
/* Tells the OS to load the section from the file when loading.
This is clear for a .bss section. */
#define SEC_LOAD 0x2
...
What does "This is clear" mean here ? Because one thing is sure, it is
not clear at all.
What is the difference between SEC_ALLOC and SEC_LOAD ? Does SEC_LOAD
mean that the content is loaded from the file ? Does SEC_ALLOC mean that
the section is allocated but empty (=no content is loaded from the file)
? If this is correct, why are there 2 different flags because one seems
to mean the opposite so one flag would be enough ?
How do those two previous flags align with SHF_ALLOC which means that
the section occupies memory during process execution ?
### SEC_HAS_CONTENTS
/* The section has contents - a data section could be
<<SEC_ALLOC>> | <<SEC_HAS_CONTENTS>>; a debug section could be
<<SEC_HAS_CONTENTS>> */
#define SEC_HAS_CONTENTS 0x100
I find this comment very confusing and obscure.
Since SEC_HAS_CONTENTS derives from not(SHT_NOBITS), how is it wrong to
set this value inside the assembler ?
Please could you explain me more about the idea behind those flags ?
Matthieu.
>> --- a/gas/write.c
>> +++ b/gas/write.c
>> @@ -1936,7 +1936,7 @@ create_obj_attrs_section (void)
>> segT s = subseg_new (name, 0);
>> elf_section_type (s)
>> = get_elf_backend_data (stdoutput)->obj_attrs_section_type;
>> - bfd_set_section_flags (s, SEC_READONLY | SEC_DATA);
>> + bfd_set_section_flags (s, SEC_READONLY | SEC_HAS_CONTENTS | SEC_DATA);
>> frag_now_fix ();
>> char *p = frag_more (size);
>> bfd_elf_set_obj_attr_contents (stdoutput, (bfd_byte *)p, size);
>
More information about the Binutils
mailing list