3

Suppose I have the following source file:

// file.c:
void f() {}
void g() {}

I compile it into object file using gcc -ffunction-sections:

$ gcc -c -ffunction-sections file.c -o file.o
# It now has at least two sections: `.text.f' (with `f'), `.text.g' (with `g').

Then I try to remove section .text.g (with g) from object file:

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

So, is there way to remove function-specific section from object file (compiled with -ffunction-sections)?

Extra info:

  1. Full list of symbols in file.o is:

    $ objdump -t file.o 
    
    file.o:     file format elf64-x86-64
    
    SYMBOL TABLE:
    0000000000000000 l    df *ABS*  0000000000000000 file.c
    0000000000000000 l    d  .text  0000000000000000 .text
    0000000000000000 l    d  .data  0000000000000000 .data
    0000000000000000 l    d  .bss   0000000000000000 .bss
    0000000000000000 l    d  .text.f        0000000000000000 .text.f
    0000000000000000 l    d  .text.g        0000000000000000 .text.g
    0000000000000000 l    d  .note.GNU-stack        0000000000000000 .note.GNU-stack
    0000000000000000 l    d  .eh_frame      0000000000000000 .eh_frame
    0000000000000000 l    d  .comment       0000000000000000 .comment
    0000000000000000 g     F .text.f        0000000000000007 f
    0000000000000000 g     F .text.g        0000000000000007 g
    
  2. My goal is to eliminate some sections from object file similarly to what ld --gc-sections does.

Or is there some theoretical reason why such task is absolutely out of the scope of objcopy and can only be performed with ld -r?

Sasha
  • 3,599
  • 1
  • 31
  • 52
  • Have you tried reporting this to [Bintools tracker](https://sourceware.org/bugzilla/)? – yugr Dec 20 '18 at 12:34
  • @yugr, no, IIRC, I didn't. Do you think it's really a bug and should be reported? – Sasha Dec 20 '18 at 16:06
  • I'd report it, `objcopy` behavior does not make sense (if you do, send them the `.s`, not the `.c`). – yugr Dec 20 '18 at 16:51
  • [This answer](https://stackoverflow.com/a/25216482/2170527) claims that `.text.FOO` can't be removed as it's referenced from some other section. Sounds too vague for me though. – yugr Dec 24 '18 at 07:56
  • 2
    @yugr, I submitted: https://sourceware.org/bugzilla/show_bug.cgi?id=24031. – Sasha Dec 24 '18 at 11:44
  • @yugr, [got answer](https://sourceware.org/bugzilla/show_bug.cgi?id=24031#c1): "There is a relocation for `.eh_frame` that references `.text.g`". – Sasha Dec 24 '18 at 15:05
  • 1
    Tried asking Andreas whether there's a workaround. – yugr Dec 25 '18 at 08:19
  • @yugr, doesn't `ld --gc-sections` act as partial workaround? – Sasha Dec 25 '18 at 11:14
  • 1
    It's not a workaround but rather a main flag to use with `-ffunction-sections`. But `objcopy` gives full control over what gets into final executable so it's unusability is depressing. – yugr Dec 25 '18 at 12:26

0 Answers0