On Thu, Jul 06, 2023 at 10:43:06PM -0700, Fangrui Song via Binutils wrote:
> include/
> * elf/common.h: Define SHF_X86_64_LARGE to be used by elf.c.
Don't do this.
> --- a/bfd/elf.c
> +++ b/bfd/elf.c
> @@ -1045,6 +1045,11 @@ _bfd_elf_make_section_from_shdr (bfd *abfd,
> if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
> flags |= SEC_EXCLUDE;
>
> + bed = get_elf_backend_data (abfd);
> + if (bed->elf_machine_code == EM_X86_64 &&
> + (hdr->sh_flags & SHF_X86_64_LARGE) != 0)
> + flags |= SEC_ELF_LARGE;
Or this. Instead write an elf_backend_section_flags for x86_64.
> @@ -3912,6 +3916,8 @@ elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
> }
> if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
> this_hdr->sh_flags |= SHF_EXCLUDE;
> + if (bed->elf_machine_code == EM_X86_64 && (asect->flags & SEC_ELF_LARGE) != 0)
> + this_hdr->sh_flags |= SHF_X86_64_LARGE;
>
Or this. Instead write elf_backend_fake_sections for x86_64.
> /* If the section has relocs, set up a section header for the
> SHT_REL[A] section. If two relocation sections are required for
> @@ -8508,6 +8514,10 @@ _bfd_elf_init_private_section_data (bfd *ibfd,
> elf_section_flags (osec) = (elf_section_flags (isec)
> & (SHF_MASKOS | SHF_MASKPROC));
>
> + /* objcopy --set-section-flags without "large" drops SHF_X86_64_LARGE. */
> + if (get_elf_backend_data (ibfd)->elf_machine_code == EM_X86_64)
> + elf_section_flags (osec) = (elf_section_flags (isec) & ~SHF_X86_64_LARGE);
> +
> /* Copy sh_info from input for mbind section. */
> if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
> && elf_section_flags (isec) & SHF_GNU_MBIND)
And this should be done in a bfd_elf64_bfd_copy_private_symbol_data
for x86_64.
@@ -633,6 +633,9 @@ typedef struct bfd_section
/* This section contains vliw code. This is for Toshiba MeP only. */
#define SEC_MEP_VLIW 0x20000000
+ /* This section has the SHF_X86_64_LARGE flag. This is ELF x86-64 only. */
+#define SEC_ELF_LARGE 0x20000000
+
/* All symbols, sizes and relocations in this section are octets
instead of bytes. Required for DWARF debug sections as DWARF
information is organized in octets, not bytes. */
@@ -1045,6 +1045,11 @@ _bfd_elf_make_section_from_shdr (bfd *abfd,
if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
flags |= SEC_EXCLUDE;
+ bed = get_elf_backend_data (abfd);
+ if (bed->elf_machine_code == EM_X86_64 &&
+ (hdr->sh_flags & SHF_X86_64_LARGE) != 0)
+ flags |= SEC_ELF_LARGE;
+
switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
{
/* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
@@ -1104,7 +1109,6 @@ _bfd_elf_make_section_from_shdr (bfd *abfd,
if (!bfd_set_section_flags (newsect, flags))
return false;
- bed = get_elf_backend_data (abfd);
if (bed->elf_backend_section_flags)
if (!bed->elf_backend_section_flags (hdr))
return false;
@@ -3912,6 +3916,8 @@ elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
}
if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
this_hdr->sh_flags |= SHF_EXCLUDE;
+ if (bed->elf_machine_code == EM_X86_64 && (asect->flags & SEC_ELF_LARGE) != 0)
+ this_hdr->sh_flags |= SHF_X86_64_LARGE;
/* If the section has relocs, set up a section header for the
SHT_REL[A] section. If two relocation sections are required for
@@ -8508,6 +8514,10 @@ _bfd_elf_init_private_section_data (bfd *ibfd,
elf_section_flags (osec) = (elf_section_flags (isec)
& (SHF_MASKOS | SHF_MASKPROC));
+ /* objcopy --set-section-flags without "large" drops SHF_X86_64_LARGE. */
+ if (get_elf_backend_data (ibfd)->elf_machine_code == EM_X86_64)
+ elf_section_flags (osec) = (elf_section_flags (isec) & ~SHF_X86_64_LARGE);
+
/* Copy sh_info from input for mbind section. */
if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
&& elf_section_flags (isec) & SHF_GNU_MBIND)
@@ -367,6 +367,9 @@ CODE_FRAGMENT
. {* This section contains vliw code. This is for Toshiba MeP only. *}
.#define SEC_MEP_VLIW 0x20000000
.
+. {* This section has the SHF_X86_64_LARGE flag. This is ELF x86-64 only. *}
+.#define SEC_ELF_LARGE 0x20000000
+.
. {* All symbols, sizes and relocations in this section are octets
. instead of bytes. Required for DWARF debug sections as DWARF
. information is organized in octets, not bytes. *}
@@ -1,5 +1,8 @@
-*- text -*-
+* objcopy's --set-section-flags now support "large" to set SHF_X86_64_LARGE
+ for ELF x86-64 objects.
+
Changes in 2.41:
* The MIPS port now supports the Sony Interactive Entertainment Allegrex
@@ -1745,13 +1745,14 @@ Set the flags for any sections matching @var{sectionpattern}. The
@var{flags} argument is a comma separated string of flag names. The
recognized names are @samp{alloc}, @samp{contents}, @samp{load},
@samp{noload}, @samp{readonly}, @samp{code}, @samp{data}, @samp{rom},
-@samp{exclude}, @samp{share}, and @samp{debug}. You can set the
-@samp{contents} flag for a section which does not have contents, but it
-is not meaningful to clear the @samp{contents} flag of a section which
-does have contents--just remove the section instead. Not all flags are
-meaningful for all object file formats. In particular the
-@samp{share} flag is only meaningful for COFF format files and not for
-ELF format files.
+@samp{exclude}, @samp{share}, @samp{debug}, and @samp{large}.
+You can set the @samp{contents} flag for a section which does not have
+contents, but it is not meaningful to clear the @samp{contents} flag of a
+section which does have contents--just remove the section instead. Not all
+flags are meaningful for all object file formats. In particular the
+@samp{share} flag is only meaningful for COFF format files and not for ELF
+format files. The ELF x86-64 specific flag @samp{large} corresponds to
+SHF_X86_64_LARGE.
@item --set-section-alignment @var{sectionpattern}=@var{align}
Set the alignment for any sections matching @var{sectionpattern}.
@@ -803,6 +803,7 @@ parse_flags (const char *s)
PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
PARSE_FLAG ("merge", SEC_MERGE);
PARSE_FLAG ("strings", SEC_STRINGS);
+ PARSE_FLAG ("large", SEC_ELF_LARGE);
#undef PARSE_FLAG
else
{
@@ -813,7 +814,8 @@ parse_flags (const char *s)
copy[len] = '\0';
non_fatal (_("unrecognized section flag `%s'"), copy);
fatal (_("supported flags: %s"),
- "alloc, load, noload, readonly, debug, code, data, rom, exclude, share, contents, merge, strings");
+ "alloc, load, noload, readonly, debug, code, data, rom, exclude, "
+ "share, contents, merge, strings, (ELF x86-64 specific) large");
}
s = snext;
new file mode 100644
@@ -0,0 +1,15 @@
+#source: large-sections.s
+#PROG: objcopy
+#as: --64
+#objcopy: --set-section-flags .ldata=alloc
+#readelf: -S -W
+
+#...
+ \[[ 0-9]+\] \.text.*[ \t]+PROGBITS[ \t0-9a-f]+AX[ \t]+.*
+#...
+ \[[ 0-9]+\] \.data.*[ \t]+PROGBITS[ \t0-9a-f]+WA[ \t]+.*
+#...
+ \[[ 0-9]+\] \.ltext.*[ \t]+PROGBITS[ \t0-9a-f]+AXl[ \t]+.*
+#...
+ \[[ 0-9]+\] \.ldata.*[ \t]+PROGBITS[ \t0-9a-f]+WA[ \t]+.*
+#pass
new file mode 100644
@@ -0,0 +1,4 @@
+ .section .text, "axl"
+ nop
+ .section .data, "awl"
+ .byte 1
new file mode 100644
@@ -0,0 +1,14 @@
+#PROG: objcopy
+#as: --64
+#objcopy: --set-section-flags .text=alloc,readonly,code,large --set-section-flags .data=alloc,large
+#readelf: -S -W
+
+#...
+ \[[ 0-9]+\] \.text.*[ \t]+PROGBITS[ \t0-9a-f]+AXl[ \t]+.*
+#...
+ \[[ 0-9]+\] \.data.*[ \t]+PROGBITS[ \t0-9a-f]+WAl[ \t]+.*
+#...
+ \[[ 0-9]+\] \.ltext.*[ \t]+PROGBITS[ \t0-9a-f]+AXl[ \t]+.*
+#...
+ \[[ 0-9]+\] \.ldata.*[ \t]+PROGBITS[ \t0-9a-f]+WAl[ \t]+.*
+#pass
new file mode 100644
@@ -0,0 +1,8 @@
+ .section .text, "ax"
+ nop
+ .section .data, "aw"
+ .byte 1
+ .section .ltext, "axl"
+ nop
+ .section .ldata, "awl"
+ .byte 1
@@ -588,6 +588,8 @@
#define SHF_GNU_MBIND 0x01000000 /* Mbind section. */
+#define SHF_X86_64_LARGE 0x10000000
+
/* Compression types. */
#define ELFCOMPRESS_ZLIB 1 /* Compressed with zlib. */
#define ELFCOMPRESS_ZSTD 2 /* Compressed with zstd */