public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* How to set alignment and READONLY of a brand new section
@ 2024-05-18 22:34 Massimiliano Cialdi
  2024-05-20 14:54 ` Nick Clifton
  0 siblings, 1 reply; 2+ messages in thread
From: Massimiliano Cialdi @ 2024-05-18 22:34 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 2675 bytes --]

Hi,
I apologize in advance if this is not the right place for this question.

In a software project of mine, on linux, I need to create a special section
that needs to be populated with 40 bytes large structures. Then I would
like to treat this section as an array.
For that I added this linker script with the -T option:

SECTIONS
{
     /* section for CLI cmd array */
    .CLI_cmd_section : ALIGN(8)
    {
        PROVIDE(__CLI_cmd_start = .);
        KEEP(*(.CLI_cmd*));
       . = ALIGN(8) ;
        PROVIDE(__CLI_cmd_end = .);
    }
}
INSERT AFTER .data;

Though, inspecting with objdump I see that the section is aligned to 32
bytes:

Sections:
Idx Name               Size      VMA               LMA               File
off  Algn  Flags
[...]
 25 .data              00000008  0000000000009000  0000000000009000
 00008000  2**3  CONTENTS, ALLOC, LOAD, DATA
 26 .CLI_cmd_section   00000068  0000000000009020  0000000000009020
 00008020  2**5  CONTENTS, ALLOC, LOAD, DATA
 27 .bss               00000078  00000000000090a0  00000000000090a0
 00008088  2**5  ALLOC
[...]

I thought it might be because some of the adjacent sections have such
alignment, but even moving it doesn't change

Sections:
Idx Name               Size      VMA               LMA               File
off  Algn  Flags
[...]
 23 .dynamic           000001f0  0000000000008cf0  0000000000008cf0
 00007cf0  2**3  CONTENTS, ALLOC, LOAD, DATA
 24 .CLI_cmd_section   00000068  0000000000008ee0  0000000000008ee0
 00007ee0  2**5  CONTENTS, ALLOC, LOAD, DATA
 25 .got               000000a8  0000000000008f48  0000000000008f48
 00007f48  2**3  CONTENTS, ALLOC, LOAD, DATA
 26 .data              00000008  0000000000009000  0000000000009000
 00008000  2**3  CONTENTS, ALLOC, LOAD, DATA
[...]

I wanted to try to see what happened to impose a higher alignment (64) and
in this case it sets it. So it seems that for some reason right now I can't
get below 32 on that section.

this is very annoying, because in the array the compiler considers the
entries offset by 40 bytes, while the linker has them offset by 64 bytes.

How should I get my array indexed well, and then impose alignment 8?

Also I would like this section to be READONLY, but if I try to impose it I
get a warning on the link which I don't know how "serious" it is:

/usr/bin/ld: /tmp/ccDmg9Dd.o: warning: relocation in read-only section
`.CLI_cmd.__CLI_cmd_help'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE

Is there a safe, reliable and "clean" way to impose READONLY the
.CLI_cmd_section without having errors/warning from the linker?

best regard
Max


-- 
Et nunc, auxilium solis, vincam!
Oppugnatio solaris!
VIS!

Massimiliano Cialdi

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

* Re: How to set alignment and READONLY of a brand new section
  2024-05-18 22:34 How to set alignment and READONLY of a brand new section Massimiliano Cialdi
@ 2024-05-20 14:54 ` Nick Clifton
  0 siblings, 0 replies; 2+ messages in thread
From: Nick Clifton @ 2024-05-20 14:54 UTC (permalink / raw)
  To: Massimiliano Cialdi, binutils

Hi Massimiliano,

> I apologize in advance if this is not the right place for this question.

No this is the right place. :-)


>      .CLI_cmd_section : ALIGN(8)

> Though, inspecting with objdump I see that the section is aligned to 32 bytes:

>   26 .CLI_cmd_section   00000068  0000000000009020  0000000000009020  00008020  2**5  CONTENTS, ALLOC, LOAD, DATA

The ALIGN() directive can only increase an output section's alignment.
It does not force a specified alignment.  So if an input section has
an alignment greater than 8 this will make the output section also
be aligned to this larger value.

You can however override an input section's alignment by using the
SUBALIGN() directive.  So this might work:

   .CLI_cmd_section : ALIGN(8) SUBALIGN(8)

(Without an actual testcase I cannot be sure of this.  For all I know
there may be a linker bug here as well...)


> Also I would like this section to be READONLY, but if I try to impose it I get a warning on the link which I don't know how "serious" it is:
> 
> /usr/bin/ld: /tmp/ccDmg9Dd.o: warning: relocation in read-only section `.CLI_cmd.__CLI_cmd_help'
> /usr/bin/ld: warning: creating DT_TEXTREL in a PIE

It is pretty serious.  The linker is telling you that it needs
to create a run-time relocation that will affect the contents of
a read-only section.  Which is bad because it means that the loader
will have to remove the read-only protection from that section in
order to be able to implement the relocation.

You need to investigate and find out what information this relocation
is trying to store in the .CLI_cmd_section and then decide if a) the
information is not needed and so can be removed from the input or b)
the information is needed and so the .CLI_cmd_section should not be
marked as READONLY.

Cheers
   Nick


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

end of thread, other threads:[~2024-05-20 14:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-18 22:34 How to set alignment and READONLY of a brand new section Massimiliano Cialdi
2024-05-20 14:54 ` Nick Clifton

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