xref: /llvm-project/llvm/test/tools/llvm-objcopy/ELF/auto-remove-add-symtab-shndx.test (revision 8bb74d16ef04d83f71b1873e7c2e652fb8287b29)
1# This test makes sure that the .symtab_shndx section is automatically removed
2# when it is not needed and added when it is.
3
4RUN: %python %p/../Inputs/ungzip.py %p/Inputs/many-sections.o.gz > %t.0
5RUN: echo 'foo' > %t
6# many-sections.o contains sections from s1 to s65535, each with a symbol.
7# Remove enough so that the last section with a symbol has index
8# SHN_LORESERVE (i.e. s65280), and show the section hasn't been removed.
9RUN: llvm-objcopy -R 's65[3-9][0-9][0-9]' -R 's6529[0-9]' -R 's6528[1-9]' %t.0 %t2
10RUN: llvm-readobj --sections %t2 | FileCheck --check-prefix=SHNDX %s
11# Remove one more to cause removal of the section.
12RUN: llvm-objcopy -R s65280 %t2 %t3
13RUN: llvm-readobj --sections %t3 | FileCheck --check-prefix=NOSHNDX %s
14
15# Add additional sections. No .symtab_shndx should be added, as they contain no
16# symbols.
17RUN: llvm-objcopy --add-section=newsec1=%t --add-section=newsec2=%t --add-section=newsec3=%t %t3 %t4
18RUN: llvm-readobj --sections %t4 | FileCheck --check-prefix=NOSHNDX %s
19
20# Add a symbol to one of these sections and show that the .symtab_shndx
21# section has been created and that the new symbol has the right section index.
22RUN: llvm-objcopy --add-symbol=newsym=newsec1:0 %t4 %t5
23RUN: llvm-readobj --symbols --sections %t5 | \
24RUN:   FileCheck --check-prefixes=SHNDX,SHNDX-SYM -DSECTION=newsec1 %s
25
26# Show that removing and adding symbols, but not their sections, has the same
27# effect regarding removal/addition of the .symtab_shndx section.
28# FIXME: llvm-objcopy does not currently remove this section in these
29# circumstances. See https://bugs.llvm.org/show_bug.cgi?id=49364
30# Test is left here to document the current behaviour.
31RUN: llvm-objcopy -N 'sym65[3-9][0-9][0-9]' -N 'sym652[89][0-9]' -w %t.0 %t6
32RUN: llvm-readobj --sections %t6 | not FileCheck --check-prefix=NOSHNDX %s
33# FIXME: Remove these test lines once the bug has been fixed. They simply show
34# that the section is removed by a run without any further operations.
35RUN: llvm-objcopy %t6 %t6b
36RUN: llvm-readobj --sections %t6b | FileCheck --check-prefix=NOSHNDX %s
37
38# Add a symbol to ensure the section is reinstated.
39RUN: llvm-objcopy --add-symbol=newsym=s65280:0 -w %t6 %t7
40RUN: llvm-readobj --sections --symbols %t7 | \
41RUN:   FileCheck --check-prefixes=SHNDX,SHNDX-SYM -DSECTION=s65280 %s
42
43NOSHNDX-NOT:    Name: .symtab_shndx
44SHNDX:          Name: .symtab_shndx
45SHNDX-SYM:      Name: newsym
46SHNDX-SYM:      Section:
47SHNDX-SYM-SAME:           [[SECTION]]
48