Bug 25380 - Support section linkage 'unique' (multiple sections with the same name)
Summary: Support section linkage 'unique' (multiple sections with the same name)
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: 2.35
: P2 normal
Target Milestone: 2.35
Assignee: Not yet assigned to anyone
URL: https://sourceware.org/ml/binutils/20...
Keywords:
Depends on:
Blocks: 25381
  Show dependency treegraph
 
Reported: 2020-01-14 01:22 UTC by Fangrui Song
Modified: 2020-03-26 00:35 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2020-02-01 00:00:00


Attachments
A patch (2.52 KB, patch)
2020-02-01 20:23 UTC, H.J. Lu
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2020-01-14 01:22:18 UTC
Clang's integrated assembler supports multiple section with the same name.

% cat a.s
.section        .text,"ax",@progbits,unique,1
nop
.section        .text,"ax",@progbits,unique,2
nop

This is useful with -fno-unique-section-names -ffunction-sections.
-ffunction-sections by default generates .text.foo, .text.bar, etc. Using the same string can save lots of space in .strtab


% cat a.c
void foo(){}
void bar(){}
% clang -fno-unique-section-names -ffunction-sections -no-integrated-as -c a.c
/tmp/a-d736bc.s: Assembler messages:
/tmp/a-d736bc.s:3: Error: junk at end of line, first unrecognized character is `,'
/tmp/a-d736bc.s:22: Error: junk at end of line, first unrecognized character is `,'
clang-10: error: assembler command failed with exit code 1 (use -v to see invocation)
Comment 1 H.J. Lu 2020-02-01 15:04:32 UTC
(In reply to Fangrui Song from comment #0)
> Clang's integrated assembler supports multiple section with the same name.
> 
> % cat a.s
> .section        .text,"ax",@progbits,unique,1
> nop
> .section        .text,"ax",@progbits,unique,2
> nop

How does it work together with comdat group like

        .section .text.bar,"axG",@progbits,bar,comdat
	.globl	bar
	.type	bar, @function
bar:
         ret

Section name is used to specify sections in assembly codes.  Will


       .section .text,N,"axG",@progbits,bar,comdat
	.globl	bar
	.type	bar, @function
bar:
         ret

and

       .section .text,N,"ax",@progbits
	.globl	bar
	.type	bar, @function
bar:
         ret

work better?  .section .text,N can be used to identify the Nth .text
section, which may or may not belong to a comdat group.
Comment 2 H.J. Lu 2020-02-01 20:23:41 UTC
Created attachment 12245 [details]
A patch
Comment 3 H.J. Lu 2020-02-02 02:47:04 UTC
(In reply to Fangrui Song from comment #0)
> Clang's integrated assembler supports multiple section with the same name.
> 
> % cat a.s
> .section        .text,"ax",@progbits,unique,1
> nop
> .section        .text,"ax",@progbits,unique,2
> nop
> 

What is the valid range for ID?  I am using unsigned int for ID and I
need to reserve one value for non-unique section.  I can use 0 or
0xffffffff.
Comment 4 H.J. Lu 2020-02-02 03:03:08 UTC
(In reply to H.J. Lu from comment #3)
> (In reply to Fangrui Song from comment #0)
> > Clang's integrated assembler supports multiple section with the same name.
> > 
> > % cat a.s
> > .section        .text,"ax",@progbits,unique,1
> > nop
> > .section        .text,"ax",@progbits,unique,2
> > nop
> > 
> 
> What is the valid range for ID?  I am using unsigned int for ID and I
> need to reserve one value for non-unique section.  I can use 0 or
> 0xffffffff.

Since I saw

.section __patchable_function_entries,"awo",@progbits,foo,unique,0

the valid section ID is from 0 to 0xfffffffe (4294967294).
Comment 5 Fangrui Song 2020-02-02 03:32:37 UTC
In clang's integrated assembler, a unique ID is an `unsigned` (0~0xffffffff). I think any sufficiently large upper bound (larger than reasonable numbers of sections) should work.

Note,

.section .foo,"ax",@progbits
and
.section .foo,"ax",@progbits,unique,0

are different.
Comment 6 H.J. Lu 2020-02-02 17:56:54 UTC
A patch is posted at

https://sourceware.org/ml/binutils/2020-02/msg00020.html
Comment 7 cvs-commit@gcc.gnu.org 2020-02-03 01:09:08 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a8c4d40b578378be3b12575d127d4c7bd9972f32

commit a8c4d40b578378be3b12575d127d4c7bd9972f32
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sun Feb 2 17:07:51 2020 -0800

    ELF: Add support for unique section ID to assembler
    
    Clang's integrated assembler supports multiple section with the same
    name:
    
    	.section .text,"ax",@progbits,unique,1
    	nop
    	.section .text,"ax",@progbits,unique,2
    	nop
    
    "unique,N" assigns the number, N, as the section ID, to a section.  The
    valid values of the section ID are between 0 and 4294967295.  It can be
    used to distinguish different sections with the same section name.
    
    This is useful with -fno-unique-section-names -ffunction-sections.
    -ffunction-sections by default generates .text.foo, .text.bar, etc.
    Using the same string can save lots of space in .strtab.
    
    This patch adds section_id to bfd_section and reuses the linker
    internal bit in BFD section flags, SEC_LINKER_CREATED, for assmebler
    internal use to mark valid section_id.  It also updates objdump to
    compare section pointers if 2 sections comes from the same file since
    2 different sections can have the same section name.
    
    bfd/
    
    	PR gas/25380
    	* bfd-in2.h: Regenerated.
    	* ecoff.c (bfd_debug_section): Add section_id.
    	* section.c (bfd_section): Add section_id.
    	(SEC_ASSEMBLER_SECTION_ID): New.
    	(BFD_FAKE_SECTION): Add section_id.
    
    binutils/
    
    	PR gas/25380
    	* objdump.c (sym_ok): Return FALSE if 2 sections are in the
    	same file with different section pointers.
    
    gas/
    
    	PR gas/25380
    	* config/obj-elf.c (section_match): Removed.
    	(get_section): Also match SEC_ASSEMBLER_SECTION_ID and
    	section_id.
    	(obj_elf_change_section): Replace info and group_name arguments
    	with match_p.  Also update the section ID and flags from match_p.
    	(obj_elf_section): Handle "unique,N".  Update call to
    	obj_elf_change_section.
    	* config/obj-elf.h (elf_section_match): New.
    	(obj_elf_change_section): Updated.
    	* config/tc-arm.c (start_unwind_section): Update call to
    	obj_elf_change_section.
    	* config/tc-ia64.c (obj_elf_vms_common): Likewise.
    	* config/tc-microblaze.c (microblaze_s_data): Likewise.
    	(microblaze_s_sdata): Likewise.
    	(microblaze_s_rdata): Likewise.
    	(microblaze_s_bss): Likewise.
    	* config/tc-mips.c (s_change_section): Likewise.
    	* config/tc-msp430.c (msp430_profiler): Likewise.
    	* config/tc-rx.c (parse_rx_section): Likewise.
    	* config/tc-tic6x.c (tic6x_start_unwind_section): Likewise.
    	* doc/as.texi: Document "unique,N" in .section directive.
    	* testsuite/gas/elf/elf.exp: Run "unique,N" tests.
    	* testsuite/gas/elf/section15.d: New file.
    	* testsuite/gas/elf/section15.s: Likewise.
    	* testsuite/gas/elf/section16.s: Likewise.
    	* testsuite/gas/elf/section16a.d: Likewise.
    	* testsuite/gas/elf/section16b.d: Likewise.
    	* testsuite/gas/elf/section17.d: Likewise.
    	* testsuite/gas/elf/section17.l: Likewise.
    	* testsuite/gas/elf/section17.s: Likewise.
    	* testsuite/gas/i386/unique.d: Likewise.
    	* testsuite/gas/i386/unique.s: Likewise.
    	* testsuite/gas/i386/x86-64-unique.d: Likewise.
    	* testsuite/gas/i386/i386.exp: Run unique and x86-64-unique.
    
    ld/
    
    	PR gas/25380
    	* testsuite/ld-i386/pr22001-1c.S: Use "unique,N" in .section
    	directives.
    	* testsuite/ld-i386/tls-gd1.S: Likewise.
    	* testsuite/ld-x86-64/pr21481b.S: Likewise.
Comment 8 H.J. Lu 2020-02-04 17:31:21 UTC
Fixed for 2.35.