Bug 24031 - Cannot remove section related to a function (even if the function is in separate section and isn't referenced elsewhere)
Summary: Cannot remove section related to a function (even if the function is in separ...
Status: RESOLVED INVALID
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.30
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-12-24 11:37 UTC by sasha1024
Modified: 2019-01-03 09:42 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
Samples: file.c, file.s and file.o (851 bytes, application/x-tar)
2018-12-24 11:37 UTC, sasha1024
Details

Note You need to log in before you can comment on or make changes to this bug.
Description sasha1024 2018-12-24 11:37:51 UTC
Created attachment 11491 [details]
Samples: file.c, file.s and file.o

Suppose, I have the following C-source file:

    $ cat file.c
    void f() {}
    void g() {}

I can compile it to an object-file with the -ffunction-sections flag:

    $ gcc -c -ffunction-sections file.c -o file.o

It corresponds to the following assembler code:

        .file "file.c"
        .text
        .section .text.f,"ax",@progbits
        .globl f
        .type f, @function
    f:
    .LFB0:
        .cfi_startproc
        pushq %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq %rsp, %rbp
        .cfi_def_cfa_register 6
        nop
        popq %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
    .LFE0:
        .size f, .-f
        .section .text.g,"ax",@progbits
        .globl g
        .type g, @function
    g:
    .LFB1:
        .cfi_startproc
        pushq %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq %rsp, %rbp
        .cfi_def_cfa_register 6
        nop
        popq %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
    .LFE1:
        .size g, .-g
        .ident "GCC: (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0"
        .section .note.GNU-stack,"",@progbits

Then I try to remove the .text.g section (with the g function) from object file, I get the following error:

    $ objcopy --remove-section .text.g file.o
    objcopy: stReXCNA: symbol `.text.g' required but not present
    objcopy:stReXCNA: No symbols

Is there reason why such task can't be performed by objcopy?

See also: https://stackoverflow.com/q/39873148
Comment 1 Andreas Schwab 2018-12-24 12:14:14 UTC
There is a relocation for .eh_frame that references .text.g.
Comment 2 Yuri Gribov 2018-12-25 08:18:52 UTC
(In reply to Andreas Schwab from comment #1)
> There is a relocation for .eh_frame that references .text.g.

Andreas, is there a way to remove .text symbols under -ffunction-sections? This might be in some embedded use-cases.
Comment 3 Eric Botcazou 2019-01-03 09:42:29 UTC
> Andreas, is there a way to remove .text symbols under -ffunction-sections?
> This might be in some embedded use-cases.

The only way is to link with --gc-sections.