llvm/llvm/test/tools/llvm-objcopy/ELF/keep-global-symbols.test

# RUN: yaml2obj %s -o %t.o

# Tests that only global symbols (via -G/--keep-global-symbols) are kept via
# the several different variants of -G/--keep-global-symbol(s).
#
# Local1: Local because "-G Local1" doesn't make symbols global.
# Local2: Global because of "--globalize-symbol Local2".
# Weak1: Weak because "-G Weak1" doesn't make symbols global.
# Weak2: Global because of "--globalize-symbol Weak2".
# Weak3: Local because no -G flag covers it.
# Global1: Global because of "-G Global1".
# Global2: Global because of "--keep-global-symbol Global2".
# Global3: Global because of "--keep-global-symbols %t-globals1.txt".
# Global4: Global because of "--keep-global-symbols %t-globals2.txt".
# Global5: Local, it appears in %t-globals2.txt but only in comments and as
#     part of another symbol
# Global6: Local, it appears in %t-globals2.txt but only part of another symbol
# "Global5 Global6": Global, because it appears in %t-globals2.txt, but we only
#     trim leading and trailing whitespace. We don't just take the first chunk
#     that looks like a symbol.
# Global7: Global, because even though it doesn't appear as any -G flags, does
#     not get demoted since it's undefined.

# RUN: echo Global2 > %t-globals1.txt
# RUN: echo "  Global3  " > %t-globals2.txt
# RUN: echo "Global4 # Global5" >> %t-globals2.txt
# RUN: echo "  Global5 Global6  " >> %t-globals2.txt
# RUN: echo "Unknown" >> %t-globals2.txt
# RUN: echo "   " >> %t-globals2.txt
# RUN: echo "# File with no symbols" > %t-globals3.txt
# RUN: echo "^Global[0-9]+([[:space:]]Global6)*$" > %t-globals-regexp.txt

# RUN: llvm-objcopy \
# RUN:   -G Global1 \
# RUN:   --keep-global-symbol Global2 \
# RUN:   --keep-global-symbols %t-globals1.txt \
# RUN:   --keep-global-symbols %t-globals2.txt \
# RUN:   -G Local1 \
# RUN:   --globalize-symbol Local2 \
# RUN:   -G Weak1 \
# RUN:   --globalize-symbol Weak2 \
# RUN:   %t.o %t.2.o
# RUN: llvm-readelf --symbols %t.2.o | FileCheck %s

# RUN: llvm-objcopy  --regex --keep-global-symbols %t-globals-regexp.txt %t.o %t.3.o
# RUN: llvm-readelf --symbols %t.3.o | FileCheck %s --check-prefix=REGEXP

# RUN: llvm-objcopy --regex --keep-global-symbol='^Global[0-9]+([[:space:]]Global6)*$' %t.o %t.4.o
# RUN: cmp %t.3.o %t.4.o

!ELF
FileHeader:
  Class:           ELFCLASS64
  Data:            ELFDATA2LSB
  Type:            ET_REL
  Machine:         EM_X86_64
Sections:
  - Name:          .text
    Type:          SHT_PROGBITS
    Flags:         [ SHF_ALLOC, SHF_EXECINSTR ]
Symbols:
  - Name:        Local1
    Section:     .text
  - Name:        Local2
    Section:     .text
  - Name:        Global1
    Section:     .text
    Binding:     STB_GLOBAL
  - Name:        Global2
    Section:     .text
    Binding:     STB_GLOBAL
  - Name:        Global3
    Section:     .text
    Binding:     STB_GLOBAL
  - Name:        Global4
    Section:     .text
    Binding:     STB_GLOBAL
  - Name:        Global5
    Section:     .text
    Binding:     STB_GLOBAL
  - Name:        Global6
    Section:     .text
    Binding:     STB_GLOBAL
  - Name:        "Global5 Global6"
    Section:     .text
    Binding:     STB_GLOBAL
  - Name:        Global7
    Binding:     STB_GLOBAL
  - Name:        Weak1
    Section:     .text
    Binding:     STB_WEAK
  - Name:        Weak2
    Section:     .text
    Binding:     STB_WEAK
  - Name:        Weak3
    Section:     .text
    Binding:     STB_WEAK

# CHECK:      Symbol table '.symtab' contains 14 entries:
# CHECK-NEXT:    Num: Value Size Type Bind Vis Ndx Name
# CHECK-NEXT:      0: {{.*}}  LOCAL  {{.*}}
# CHECK-NEXT:      1: {{.*}}  LOCAL  {{.*}} Local1
# CHECK-NEXT:      2: {{.*}}  LOCAL  {{.*}} Global5
# CHECK-NEXT:      3: {{.*}}  LOCAL  {{.*}} Global6
# CHECK-NEXT:      4: {{.*}}  LOCAL  {{.*}} Weak3
# CHECK-NEXT:      5: {{.*}}  GLOBAL {{.*}} Local2
# CHECK-NEXT:      6: {{.*}}  GLOBAL {{.*}} Global1
# CHECK-NEXT:      7: {{.*}}  GLOBAL {{.*}} Global2
# CHECK-NEXT:      8: {{.*}}  GLOBAL {{.*}} Global3
# CHECK-NEXT:      9: {{.*}}  GLOBAL {{.*}} Global4
# CHECK-NEXT:     10: {{.*}}  GLOBAL {{.*}} Global5 Global6
# CHECK-NEXT:     11: {{.*}}  GLOBAL {{.*}} UND Global7
# CHECK-NEXT:     12: {{.*}}  WEAK   {{.*}} Weak1
# CHECK-NEXT:     13: {{.*}}  GLOBAL {{.*}} Weak2

# REGEXP:           6: {{.*}}  GLOBAL {{.*}} Global1
# REGEXP-NEXT:      7: {{.*}}  GLOBAL {{.*}} Global2
# REGEXP-NEXT:      8: {{.*}}  GLOBAL {{.*}} Global3
# REGEXP-NEXT:      9: {{.*}}  GLOBAL {{.*}} Global4
# REGEXP-NEXT:     10: {{.*}}  GLOBAL {{.*}} Global5
# REGEXP-NEXT:     11: {{.*}}  GLOBAL {{.*}} Global6
# REGEXP-NEXT:     12: {{.*}}  GLOBAL {{.*}} Global5 Global6
# REGEXP-NEXT:     13: {{.*}}  GLOBAL {{.*}} UND Global7